forked from bytedance/dddfirework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.go
65 lines (49 loc) · 1.67 KB
/
timer.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
package dddfirework
import (
"context"
"fmt"
"time"
)
var ErrNoEventTimerFound = fmt.Errorf("no event_timer found")
type TimerHandler func(ctx context.Context, key, cron string, data []byte) error
// ITimer 分布式定时器协议
type ITimer interface {
// RegisterTimerHandler 注册定时任务,定时到来时候调用该回调函数
RegisterTimerHandler(cb TimerHandler)
// RunCron 按照 cron 语法设置定时,并在定时到达后作为参数调用定时任务回调
// key: 定时任务唯一标识,重复调用时不覆盖已有计时; cron: 定时配置; data: 透传数据,回调函数传入
RunCron(key, cron string, data []byte) error
// RunOnce 指定时间单次运行
// key: 定时任务唯一标识,重复调用时不覆盖已有计时; t: 执行时间; data: 透传数据,回调函数传入
RunOnce(key string, t time.Time, data []byte) error
// Cancel 删除某个定时
Cancel(key string) error
}
type noTimer struct {
}
func (d *noTimer) RunCron(key, cron string, data []byte) error {
return ErrNoEventTimerFound
}
func (d *noTimer) RunOnce(key string, t time.Time, data []byte) error {
return ErrNoEventTimerFound
}
func (d *noTimer) RegisterTimerHandler(cb TimerHandler) {
}
func (d *noTimer) Cancel(key string) error {
return nil
}
// TimerEvent 定时器专用的事件
type TimerEvent struct {
Key string
Cron string
Payload []byte
}
func (e *TimerEvent) GetType() EventType {
return EventType(e.Key)
}
func (e *TimerEvent) GetSender() string {
return ""
}
func onTimer(ctx context.Context, key, cron string, data []byte) error {
return onEvent(ctx, NewDomainEvent(&TimerEvent{Key: key, Cron: cron, Payload: data}))
}