-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhook.go
More file actions
46 lines (36 loc) · 841 Bytes
/
Copy pathhook.go
File metadata and controls
46 lines (36 loc) · 841 Bytes
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
package croner
import "sort"
type CronHook struct {
order int
f func(runReturn *JobRunReturnWithEid)
}
func OnJobReturn(f func(runReturn *JobRunReturnWithEid), order ...int) {
jobReturnHooks = jobReturnHooks.Add(f, order...)
}
type CronHooks []CronHook
var jobReturnHooks CronHooks
func (h CronHooks) Run(runReturn *JobRunReturnWithEid) {
sort.Sort(h)
for _, hook := range h {
hook.f(runReturn)
}
}
func (h CronHooks) Add(fn func(runReturn *JobRunReturnWithEid), order ...int) CronHooks {
o := 1
if len(order) > 0 {
o = order[0]
}
return append(h, CronHook{o, fn})
}
// Sorting function
func (h CronHooks) Len() int {
return len(h)
}
// Sorting function
func (h CronHooks) Less(i, j int) bool {
return h[i].order < h[j].order
}
// Sorting function
func (h CronHooks) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}