-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_helpers.go
More file actions
84 lines (66 loc) · 1.53 KB
/
task_helpers.go
File metadata and controls
84 lines (66 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package svcinit
import (
"context"
"iter"
"slices"
)
var allSteps = []Step{StepSetup, StepStart, StepStop, StepTeardown} // order matters
func taskSteps(task Task) []Step {
if ts, ok := task.(TaskSteps); ok {
return ts.TaskSteps()
}
return allSteps
}
func taskOrderedSteps(steps []Step) iter.Seq[Step] {
return func(yield func(Step) bool) {
if len(steps) == 0 {
return
}
for _, step := range allSteps {
if slices.Contains(steps, step) {
if !yield(step) {
return
}
}
}
}
}
func taskHasStep(task Task, step Step) bool {
return slices.Contains(taskSteps(task), step)
}
type errorTask struct {
err error
}
var _ Task = (*errorTask)(nil)
func (n *errorTask) Run(_ context.Context, step Step) error {
return n.err
}
// checkNilTask returns errorTask if task is nil, otherwise return the task itself.
func checkNilTask(task Task) Task {
if task == nil {
return &errorTask{err: ErrNilTask}
}
return task
}
// task options
type taskOptions struct {
cancelContext bool
startStepManager bool
callbacks []TaskCallback
handler TaskHandler
}
type taskOptionFunc func(*taskOptions)
func (f taskOptionFunc) applyTaskOpt(opts *taskOptions) {
f(opts)
}
type taskInstanceOptionFunc func(*taskOptions)
func (f taskInstanceOptionFunc) applyTaskInstanceOpt(opts *taskOptions) {
f(opts)
}
type taskGlobalOptionFunc func(*taskOptions)
func (f taskGlobalOptionFunc) applyTaskOpt(options *taskOptions) {
f(options)
}
func (f taskGlobalOptionFunc) applyTaskInstanceOpt(opts *taskOptions) {
f(opts)
}