-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
323 lines (249 loc) · 7.02 KB
/
task.go
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package plumber
import (
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
"github.com/workanator/go-floc/v3"
"gitlab.kilic.dev/libraries/go-utils/v2/utils"
)
type Task[Pipe TaskListData] struct {
Plumber *Plumber
TL *TaskList[Pipe]
Log *logrus.Entry
Channel *AppChannel
Pipe *Pipe
Control floc.Control
Name string
Lock *sync.RWMutex
taskLock *sync.RWMutex
options TaskOptions[Pipe]
commands []*Command[Pipe]
parent *Task[Pipe]
subtask Job
emptyJob Job
fn TaskFn[Pipe]
shouldRunBeforeFn TaskFn[Pipe]
shouldRunAfterFn TaskFn[Pipe]
onTerminatorFn TaskFn[Pipe]
jobWrapperFn TaskJobWrapperFn[Pipe]
status TaskStatus
}
type TaskCtx struct {
Plumber *Plumber
TL *TaskListCtx
Log *logrus.Entry
Name string
Lock *sync.RWMutex
}
type TaskOptions[Pipe TaskListData] struct {
skipPredicateFn TaskPredicateFn[Pipe]
disablePredicateFn TaskPredicateFn[Pipe]
}
type TaskStatus struct {
stopCases StatusStopCases
}
type (
TaskFn[Pipe TaskListData] func(t *Task[Pipe]) error
TaskPredicateFn[Pipe TaskListData] func(t *Task[Pipe]) bool
TaskJobWrapperFn[Pipe TaskListData] func(job Job, t *Task[Pipe]) Job
TaskJobParserFn[Pipe TaskListData] func(t *Task[Pipe]) Job
)
// NewTask Creates a new task to be run as a job.
func NewTask[Pipe TaskListData](tl *TaskList[Pipe], name ...string) *Task[Pipe] {
t := &Task[Pipe]{
Name: strings.Join(utils.DeleteEmptyStringsFromSlice(name), tl.Plumber.options.delimiter),
TL: tl,
Plumber: tl.Plumber,
Lock: tl.Lock,
taskLock: &sync.RWMutex{},
Channel: tl.Channel,
Pipe: &tl.Pipe,
Control: tl.Control,
}
t.Log = tl.Log.WithField(LOG_FIELD_CONTEXT, t.Name)
t.emptyJob = tl.JobIf(tl.Predicate(func(_ *TaskList[Pipe]) bool {
return false
}),
func(_ floc.Context, _ floc.Control) error {
return nil
},
)
t.subtask = t.emptyJob
return t
}
// Sets the function that should run before the task.
func (t *Task[Pipe]) ShouldRunBefore(fn TaskFn[Pipe]) *Task[Pipe] {
t.shouldRunBeforeFn = fn
return t
}
// Sets the function that should run as task.
func (t *Task[Pipe]) Set(fn TaskFn[Pipe]) *Task[Pipe] {
t.fn = fn
return t
}
// Sets the function that should run after the task.
func (t *Task[Pipe]) ShouldRunAfter(fn TaskFn[Pipe]) *Task[Pipe] {
t.shouldRunAfterFn = fn
return t
}
// Sets the predicate that should conditionally disable the task depending on the pipe variables.
func (t *Task[Pipe]) ShouldDisable(fn TaskPredicateFn[Pipe]) *Task[Pipe] {
t.options.disablePredicateFn = fn
return t
}
// Checks whether the current task is disabled or not.
func (t *Task[Pipe]) IsDisabled() bool {
if t.options.disablePredicateFn == nil {
return false
}
return t.options.disablePredicateFn(t)
}
// Sets the predicate that should conditionally skip the task depending on the pipe variables.
func (t *Task[Pipe]) ShouldSkip(fn TaskPredicateFn[Pipe]) *Task[Pipe] {
t.options.skipPredicateFn = fn
return t
}
// Checks whether the current task is skipped or not.
func (t *Task[Pipe]) IsSkipped() bool {
if t.options.skipPredicateFn == nil {
return false
}
return t.options.skipPredicateFn(t)
}
// Enables global plumber terminator on this task.
func (t *Task[Pipe]) EnableTerminator() *Task[Pipe] {
t.Log.Tracef("Registered terminator.")
t.Plumber.RegisterTerminator()
go t.handleTerminator()
return t
}
// Sets the function that should fire whenever the application is globally terminated.
func (t *Task[Pipe]) SetOnTerminator(fn TaskFn[Pipe]) *Task[Pipe] {
t.onTerminatorFn = fn
return t
}
// Extend the job of the current task.
func (t *Task[Pipe]) SetJobWrapper(fn TaskJobWrapperFn[Pipe]) *Task[Pipe] {
t.jobWrapperFn = fn
return t
}
// Runs the current task.
func (t *Task[Pipe]) Run() error {
if stop := t.handleStopCases(); stop {
return nil
}
started := time.Now()
t.Log.WithField(LOG_FIELD_STATUS, log_status_run).Traceln(t.Name)
if t.shouldRunBeforeFn != nil {
if err := t.shouldRunBeforeFn(t); err != nil {
t.Log.Errorln(err)
return t.handleErrors(err)
}
}
if t.fn != nil {
if err := t.fn(t); err != nil {
t.Log.Errorln(err)
return t.handleErrors(err)
}
}
if t.shouldRunAfterFn != nil {
if err := t.shouldRunAfterFn(t); err != nil {
t.Log.Errorln(err)
return t.handleErrors(err)
}
}
t.Log.WithField(LOG_FIELD_STATUS, log_status_end).Tracef("%s -> %s", t.Name, time.Since(started).Round(time.Millisecond).String())
return nil
}
// Runs the current task as a job.
func (t *Task[Pipe]) Job() Job {
return t.TL.JobIfNot(
t.TL.Predicate(func(_ *TaskList[Pipe]) bool {
return t.handleStopCases()
}),
t.TL.CreateJob(func(tl *TaskList[Pipe]) error {
if t.jobWrapperFn != nil {
return tl.RunJobs(t.jobWrapperFn(
tl.CreateBasicJob(t.Run),
t,
))
}
return t.Run()
}),
t.TL.CreateJob(func(_ *TaskList[Pipe]) error {
return nil
}),
)
}
// Send the error message to plumber while running inside a routine.
func (t *Task[Pipe]) SendError(err error) *Task[Pipe] {
t.Plumber.SendCustomError(t.Log, err)
return t
}
// Send the fatal error message to plumber while running inside a routine.
func (t *Task[Pipe]) SendFatal(err error) *Task[Pipe] {
t.Control.Cancel(err)
t.Plumber.SendCustomFatal(t.Log, err)
return t
}
// Trigger the exit protocol of plumber.
func (t *Task[Pipe]) SendExit(code int) *Task[Pipe] {
t.Control.Cancel(fmt.Sprintf("Will exit with code: %d", code))
t.Plumber.SendExit(code)
return t
}
// Convert the task into task context.
func (t *Task[Pipe]) ToCtx() *TaskCtx {
return &TaskCtx{
TL: t.TL.ToCtx(),
Plumber: t.Plumber,
Log: t.Log,
Name: t.Name,
Lock: t.Lock,
}
}
// Handles the stop cases of the task.
func (t *Task[Pipe]) handleStopCases() bool {
if t.status.stopCases.handled {
return t.status.stopCases.result
}
t.status.stopCases.handled = true
if result := t.IsDisabled(); result {
t.Log.WithField(LOG_FIELD_CONTEXT, log_context_disable).
Debugf("%s", t.Name)
t.status.stopCases.result = true
return t.status.stopCases.result
} else if result := t.IsSkipped(); result {
t.Log.WithField(LOG_FIELD_CONTEXT, log_context_skipped).
Warnf("%s", t.Name)
t.status.stopCases.result = true
return t.status.stopCases.result
}
t.status.stopCases.result = false
return t.status.stopCases.result
}
// Handles the errors from the current task.
func (t *Task[Pipe]) handleErrors(err error) error {
t.SendFatal(err)
return err
}
// Handles the plumber terminator when terminator is triggered.
func (t *Task[Pipe]) handleTerminator() {
if t.IsDisabled() || t.IsSkipped() {
t.Log.Traceln("Sending terminated directly because the task is already not available.")
t.Plumber.DeregisterTerminator()
return
}
ch := make(chan os.Signal, 1)
t.Plumber.Terminator.ShouldTerminate.Register(ch)
sig := <-ch
t.Log.Tracef("Forwarding signal to task: %s", sig)
if t.onTerminatorFn != nil {
t.SendError(t.onTerminatorFn(t))
}
t.Log.Tracef("Registered as terminated.")
t.Plumber.RegisterTerminated()
}