-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.go
61 lines (52 loc) · 1.45 KB
/
action.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
package yotei
import "time"
// Action is the result of a task.
//
// It allows applying a logic based
// on the task's output.
type Action func(scheduler *Scheduler, task Tasker)
// ThenAdd adds the given tasks to the scheduler
// when the action is run.
func (action Action) ThenAdd(tasks ...Tasker) Action {
return action.Then(func(scheduler *Scheduler, _ Tasker) {
scheduler.Add(tasks...)
})
}
// Then adds a new callback that will be executed when the action
// is run. The callback supports a specific action callback that
// gets the current scheduler and the executed task.
func (action Action) Then(callback Action) Action {
return func(scheduler *Scheduler, task Tasker) {
if action != nil {
action(scheduler, task)
}
callback(scheduler, task)
}
}
// Continue keeps the task in the scheduler.
func Continue() Action {
return Action(nil)
}
// Retry will remove the task from the scheduler
// and add it back again after the given duration.
//
// # Important
//
// This does not mean the task will begin executing
// after the duration exceeds but rather that the
// task will be in the scheduler again after that time.
func Retry(duration time.Duration) Action {
return func(scheduler *Scheduler, task Tasker) {
scheduler.Remove(task)
go func() {
time.Sleep(duration)
scheduler.Add(task)
}()
}
}
// Done removes the task from the scheduler
func Done() Action {
return func(scheduler *Scheduler, task Tasker) {
scheduler.Remove(task)
}
}