-
Notifications
You must be signed in to change notification settings - Fork 43
/
task_callback.go
72 lines (54 loc) · 1.55 KB
/
task_callback.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
package sched
import (
"github.com/pkg/errors"
"sync"
)
var (
callbackLock = new(sync.Mutex)
)
type TaskCallback func(t *Task)
func (t *Task) OnFinished(tag string, callback TaskCallback) error {
callbackLock.Lock()
defer callbackLock.Unlock()
if _, ok := t.onFinished[tag]; ok {
return errors.Errorf("existed tag: %s in on_finished callbacks", tag)
}
t.onFinished[tag] = callback
return nil
}
func (t *Task) OnBeforeExecuting(tag string, callback TaskCallback) error {
callbackLock.Lock()
defer callbackLock.Unlock()
if _, ok := t.onBeforeExecuting[tag]; ok {
return errors.Errorf("existed tag: %s in on_before_executing callbacks", tag)
}
t.onBeforeExecuting[tag] = callback
return nil
}
func (t *Task) OnCanceled(tag string, callback TaskCallback) error {
callbackLock.Lock()
defer callbackLock.Unlock()
if _, ok := t.onCanceled[tag]; ok {
return errors.Errorf("existed tag: %s in on_cancel callbacks", tag)
}
t.onCanceled[tag] = callback
return nil
}
func (t *Task) OnScheduleStart(tag string, callback TaskCallback) error {
callbackLock.Lock()
defer callbackLock.Unlock()
if _, ok := t.onScheduleStart[tag]; ok {
return errors.Errorf("existed tag: %s in on_working callbacks", tag)
}
t.onScheduleStart[tag] = callback
return nil
}
func (t *Task) OnEveryExecuted(tag string, callback TaskCallback) error {
callbackLock.Lock()
defer callbackLock.Unlock()
if _, ok := t.onEveryExecuted[tag]; ok {
return errors.Errorf("existed tag: %s in on_every_executed callbacks", tag)
}
t.onEveryExecuted[tag] = callback
return nil
}