-
Notifications
You must be signed in to change notification settings - Fork 0
/
floc.go
448 lines (368 loc) · 11.1 KB
/
floc.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package plumber
import (
"fmt"
"time"
"github.com/workanator/go-floc/v3"
"github.com/workanator/go-floc/v3/guard"
"github.com/workanator/go-floc/v3/pred"
"github.com/workanator/go-floc/v3/run"
)
type (
Job = floc.Job
JobPredicate = floc.Predicate
Result = floc.Result
ResultMask = floc.ResultMask
GuardHandlerFn[Pipe TaskListData] func(*TaskList[Pipe])
)
const (
TASK_ANY Result = floc.None
TASK_COMPLETED Result = floc.Completed
TASK_CANCELLED Result = floc.Canceled
TASK_FAILED Result = floc.Failed
)
// Creates a new floc predicate out of the given conditions.
func (tl *TaskList[Pipe]) Predicate(fn TaskListPredicateFn[Pipe]) JobPredicate {
return func(_ floc.Context) bool {
return fn(tl)
}
}
/*
JobBackground starts the job in it's own goroutine. The function does not
track the lifecycle of the job started and does no synchronization with it
therefore the job running in background may remain active even if the flow
is finished. The function assumes the job is aware of the flow state and/or
synchronization and termination of it is implemented outside.
floc.Run(run.Background(
func(ctx floc.Context, ctrl floc.Control) error {
for !ctrl.IsFinished() {
fmt.Println(time.Now())
}
return nil
}
})
Summary:
- Run jobs in goroutines : YES
- Wait all jobs finish : NO
- Run order : SINGLE
Diagram:
--+----------->
|
+-->[JOB]
*/
func (tl *TaskList[Pipe]) JobBackground(job Job) Job {
return run.Background(job)
}
func (tl *TaskList[Pipe]) CreateJob(fn func(tl *TaskList[Pipe]) error) Job {
return func(_ floc.Context, _ floc.Control) error {
return fn(tl)
}
}
func (tl *TaskList[Pipe]) CreateBasicJob(fn func() error) Job {
return func(_ floc.Context, _ floc.Control) error {
return fn()
}
}
/*
JobLoop repeats running the job forever.
Summary:
- Run jobs in goroutines : NO
- Wait all jobs finish : YES
- Run order : SINGLE
Diagram:
+----------+
| |
V |
----->[JOB]--+
*/
func (tl *TaskList[Pipe]) JobLoop(job Job) Job {
return run.Loop(job)
}
func (tl *TaskList[Pipe]) JobLoopWithWaitAfter(job Job, delay time.Duration) Job {
return run.Loop(
run.Sequence(
job,
run.Delay(delay, tl.CreateBasicJob(func() error {
return nil
})),
),
)
}
/*
JobDelay does delay before starting the job.
Summary:
- Run jobs in goroutines : NO
- Wait all jobs finish : YES
- Run order : SINGLE
Diagram:
--(DELAY)-->[JOB]-->
*/
func (tl *TaskList[Pipe]) JobDelay(job Job, delay time.Duration) Job {
return run.Delay(delay, job)
}
/*
JobIf runs the first job if the condition is met and runs
the second job, if it's passed, if the condition is not met.
The function panics if no or more than two jobs are given.
For expressiveness Then() and Else() can be used.
flow := run.If(testSomething,
run.Then(doSomething),
run.Else(doSomethingElse),
)
Summary:
- Run jobs in goroutines : NO
- Wait all jobs finish : YES
- Run order : SINGLE
Diagram:
+----->[JOB_1]---+
| YES |
--(CONDITION MET?)--+ +-->
| NO |
+----->[JOB_2]---+
*/
func (tl *TaskList[Pipe]) JobIf(predicate JobPredicate, jobs ...Job) Job {
return run.If(predicate, jobs...)
}
/*
JobIfNot runs the first job if the condition is not met and runs
the second job, if it's passed, if the condition is met.
The function panics if no or more than two jobs are given.
For expressiveness Then() and Else() can be used.
flow := run.IfNot(testSomething,
run.Then(doSomething),
run.Else(doSomethingElse),
)
Summary:
- Run jobs in goroutines : NO
- Wait all jobs finish : YES
- Run order : SINGLE
Diagram:
+----->[JOB_1]---+
| NO |
--(CONDITION MET?)--+ +-->
| YES |
+----->[JOB_2]---+
*/
func (tl *TaskList[Pipe]) JobIfNot(predicate JobPredicate, jobs ...Job) Job {
return run.IfNot(predicate, jobs...)
}
/*
Then just returns the job unmodified. Then is used for expressiveness
and can be omitted.
Summary:
- Run jobs in goroutines : N/A
- Wait all jobs finish : N/A
- Run order : N/A
Diagram:
----[JOB]--->
*/
func (tl *TaskList[Pipe]) JobThen(job Job) Job {
return run.Then(job)
}
/*
JobElse just returns the job unmodified. Else is used for expressiveness
and can be omitted.
Summary:
- Run jobs in goroutines : N/A
- Wait all jobs finish : N/A
- Run order : N/A
Diagram:
----[JOB]--->
*/
func (tl *TaskList[Pipe]) JobElse(job Job) Job {
return run.Else(job)
}
/*
JobWait waits until the condition is met. The function falls into sleep with the
duration given between condition checks. The function does not run any job
actually and just repeatedly checks predicate's return value. When the predicate
returns true the function finishes.
Summary:
- Run jobs in goroutines : N/A
- Wait all jobs finish : N/A
- Run order : N/A
Diagram:
NO
+------(SLEEP)------+
| |
V | YES
----(CONDITION MET?)--+----->
*/
func (tl *TaskList[Pipe]) JobWait(predicate JobPredicate, sleep time.Duration) Job {
return run.Wait(predicate, sleep)
}
/*
JobWhile repeats running the job while the condition is met.
Summary:
- Run jobs in goroutines : NO
- Wait all jobs finish : YES
- Run order : SINGLE
Diagram:
YES
+-------[JOB]<------+
| |
V | NO
----(CONDITION MET?)--+---->
*/
func (tl *TaskList[Pipe]) JobWhile(predicate JobPredicate, job Job) Job {
return run.While(predicate, job)
}
/*
JobParallel runs jobs in their own goroutines and waits until all of them finish.
Summary:
- Run jobs in goroutines : YES
- Wait all jobs finish : YES
- Run order : PARALLEL
Diagram:
+-->[JOB_1]--+
| |
--+--> .. --+-->
| |
+-->[JOB_N]--+
*/
func (tl *TaskList[Pipe]) JobParallel(jobs ...Job) Job {
return run.Parallel(jobs...)
}
/*
JobSequence runs jobs sequentially, one by one.
Summary:
- Run jobs in goroutines : NO
- Wait all jobs finish : YES
- Run order : SEQUENCE
Diagram:
-->[JOB_1]-...->[JOB_N]-->
*/
func (tl *TaskList[Pipe]) JobSequence(jobs ...Job) Job {
return run.Sequence(jobs...)
}
/*
JobRepeat repeats running the job for N times.
Summary:
- Run jobs in goroutines : NO
- Wait all jobs finish : YES
- Run order : SINGLE
Diagram:
NO
+-----------[JOB]<---------+
| |
V | YES
----(ITERATED COUNT TIMES?)--+---->
*/
func (tl *TaskList[Pipe]) JobRepeat(job Job, times int) Job {
return run.Repeat(times, job)
}
func (tl *TaskList[Pipe]) JobWaitForTerminator() Job {
return tl.CreateBasicJob(func() error {
if !tl.Plumber.Terminator.Enabled {
return fmt.Errorf("Terminator is not enabled.")
}
tl.Log.Traceln("Waiting for the terminator signal...")
ch := make(chan bool, 1)
tl.Plumber.Terminator.Terminated.Register(ch)
defer tl.Plumber.Terminator.Terminated.Unregister(ch)
<-ch
return nil
})
}
// PredicateAnd returns a predicate which chains multiple predicates into a condition
// with AND logic. The result predicate finishes calculation of
// the condition as fast as the result is known. The function panics if
// the number of predicates is less than 2.
//
// The result predicate tests the condition as follows.
//
// [PRED_1] AND ... AND [PRED_N]
func (tl *TaskList[Pipe]) PredicateAnd(predicates ...JobPredicate) JobPredicate {
return pred.And(predicates...)
}
// PredicateOr returns a predicate which chains multiple predicates into a condition
// with OR logic. The result predicate finishes calculation of
// the condition as fast as the result is known.
//
// The result predicate tests the condition as follows.
//
// [PRED_1] OR ... OR [PRED_N]
func (tl *TaskList[Pipe]) PredicateOr(predicates ...JobPredicate) JobPredicate {
return pred.Or(predicates...)
}
// PredicateNot returns the negated value of the predicate.
//
// The result predicate tests the condition as follows.
//
// NOT [PRED]
func (tl *TaskList[Pipe]) PredicateNot(predicate JobPredicate) JobPredicate {
return pred.Not(predicate)
}
// Xor returns a predicate which chains multiple predicates into a condition
// with XOR logic. The result predicate finishes calculation of
// the condition as fast as the result is known.
//
// The result predicate tests the condition as follows.
//
// (([PRED_1] XOR [PRED_2]) ... XOR [PRED_N])
func (tl *TaskList[Pipe]) PredicateXor(predicates ...JobPredicate) JobPredicate {
return pred.Xor(predicates...)
}
// GuardTimeout protects the job from taking too much time on execution.
// The job is run in it's own goroutine while the current goroutine waits
// until the job finished or time went out or the flow is finished.
func (tl *TaskList[Pipe]) GuardTimeout(job Job, timeout time.Duration) Job {
return guard.Timeout(guard.ConstTimeout(timeout), nil, job)
}
// OnTimeout protects the job from taking too much time on execution.
// In addition it takes TimeoutTrigger func (t *TaskList[Pipe]) which called if time is out.
// The job is run in it's own goroutine while the current goroutine waits
// until the job finished or time went out or the flow is finished.
func (tl *TaskList[Pipe]) GuardOnTimeout(
job Job,
fn GuardHandlerFn[Pipe],
timeout time.Duration,
) Job {
return guard.OnTimeout(
guard.ConstTimeout(timeout),
nil,
job,
func(_ floc.Context, _ floc.Control, _ interface{}) {
fn(tl)
},
)
}
// Panic protects the job from falling into panic. On panic the flow will
// be canceled with the ErrPanic result. Guarding the job from falling into
// panic is effective only if the job runs in the current goroutine.
func (tl *TaskList[Pipe]) GuardPanic(job Job) Job {
return guard.Panic(
job,
)
}
func (tl *TaskList[Pipe]) GuardIgnorePanic(job Job) Job {
return guard.IgnorePanic(
job,
)
}
// OnPanic protects the job from falling into panic. In addition it
// takes PanicTrigger func which is called in case of panic. Guarding the job
// from falling into panic is effective only if the job runs in the current
// goroutine.
func (tl *TaskList[Pipe]) GuardOnPanic(job Job, fn GuardHandlerFn[Pipe]) Job {
return guard.OnPanic(
job,
func(_ floc.Context, _ floc.Control, _ interface{}) {
fn(tl)
},
)
}
// Resume resumes execution of the flow possibly finished by the job.
// If the mask is empty execution will be resumed regardless the reason
// it was finished. Otherwise execution will be resumed if the reason
// it finished with is masked.
func (tl *TaskList[Pipe]) GuardResume(job Job, mask Result) Job {
return guard.Resume(tl.NewResultMask(mask), job)
}
// Always run this job!
func (tl *TaskList[Pipe]) GuardAlways(job Job) Job {
return guard.Resume(tl.NewResultMask(TASK_ANY), job)
}
// NewResultMask constructs new instance from the mask given.
func (tl *TaskList[Pipe]) NewResultMask(mask Result) ResultMask {
return floc.NewResultMask(mask)
}