-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
118 lines (97 loc) · 3.27 KB
/
task.go
File metadata and controls
118 lines (97 loc) · 3.27 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package svcinit
import (
"context"
"fmt"
)
type Task interface {
Run(ctx context.Context, step Step) error
}
type TaskFunc func(ctx context.Context, step Step) error
func (t TaskFunc) Run(ctx context.Context, step Step) error {
return t(ctx, step)
}
func (t TaskFunc) String() string {
return getDefaultTaskDescription(t)
}
// TaskHandler should call task.Run(ctx, step) and return its error. It can do any processing it needs before and
// after the call.
// WARNING: not calling the method, or calling it for any other step, will break the promise of never calling
// the steps out of order or multiple times.
type TaskHandler func(ctx context.Context, task Task, step Step) error
// TaskSteps returns the steps that the task implements. They will be the only ones called.
type TaskSteps interface {
TaskSteps() []Step
}
// TaskName allows tasks to have a name.
type TaskName interface {
TaskName() string
}
// DefaultTaskSteps returns the default value for [TaskSteps.TaskSteps], which is the list of all steps.
func DefaultTaskSteps() []Step {
return allSteps
}
// GetTaskName gets the name of task, or blank if it don't have one.
func GetTaskName(task Task) string {
if ts, ok := task.(TaskName); ok {
return ts.TaskName()
}
return ""
}
// GetTaskDescription returns the task description, be it the String method, a task name, or its variable type.
func GetTaskDescription(task Task) string {
if ts, ok := task.(fmt.Stringer); ok {
return ts.String()
}
if tn := GetTaskName(task); tn != "" {
return tn
}
return getDefaultTaskDescription(task)
}
// TaskWithOptions allows the task to set some of the task options. They have priority over options set via
// [Manager.AddTask].
type TaskWithOptions interface {
TaskOptions() []TaskInstanceOption
}
// TaskWithInitError allows a task to report an initialization error. The error might be nil.
type TaskWithInitError interface {
TaskInitError() error
}
// WithCancelContext sets whether to automatically cancel the task start step context when the first task finishes.
// The default is false, meaning that the stop step should handle to stop the task.
func WithCancelContext(cancelContext bool) TaskAndInstanceOption {
return taskGlobalOptionFunc(func(options *taskOptions) {
options.cancelContext = cancelContext
})
}
// WithStartStepManager sets whether to add a StartStepManager to the stop step context.
// This allows the stop step to cancel the start step context and/or wait for its completion.
func WithStartStepManager() TaskAndInstanceOption {
return taskGlobalOptionFunc(func(options *taskOptions) {
options.startStepManager = true
})
}
// WithHandler adds a task handler.
func WithHandler(handler TaskHandler) TaskOption {
return taskOptionFunc(func(options *taskOptions) {
options.handler = handler
})
}
// WithCallback adds callbacks for the task.
func WithCallback(callbacks ...TaskCallback) TaskOption {
return taskOptionFunc(func(options *taskOptions) {
options.callbacks = append(options.callbacks, callbacks...)
})
}
type TaskOption interface {
applyTaskOpt(options *taskOptions)
}
type TaskInstanceOption interface {
applyTaskInstanceOpt(options *taskOptions)
}
type TaskAndInstanceOption interface {
TaskOption
TaskInstanceOption
}
func getDefaultTaskDescription(task any) string {
return fmt.Sprintf("%T", task)
}