-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule_executor.go
128 lines (104 loc) · 3.08 KB
/
schedule_executor.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
package executors
import (
"context"
"errors"
"log/slog"
"sync"
"time"
"github.com/aptible/supercronic/cronexpr"
gxtime "github.com/dubbogo/timer"
"github.com/zhenzou/executors/cron"
"github.com/zhenzou/executors/routine"
)
func NewPoolScheduleExecutor(opts ..._PoolExecutorOption) ScheduledExecutor {
executor := _NewPoolExecutorService[any](opts...)
scheduleExecutor := PoolScheduleExecutor{
PoolExecutor: executor,
dispatcher: cron.NewDispatcher[Runnable](executor.opts.Logger),
}
scheduleExecutor.initTimerWheelOnce = sync.OnceFunc(scheduleExecutor.initTimerWheel)
return &scheduleExecutor
}
type PoolScheduleExecutor struct {
*PoolExecutor[any]
tw *gxtime.TimerWheel
initTimerWheelOnce func()
cronScheduleOnce sync.Once
dispatcher cron.Dispatcher[Runnable]
}
func (p *PoolScheduleExecutor) initTimerWheel() {
p.tw = gxtime.NewTimerWheel()
}
func (p *PoolScheduleExecutor) Schedule(r Runnable, delay time.Duration) (CancelFunc, error) {
p.opts.Logger.Debug("start to schedule new task", slog.Duration("delay", delay))
p.initTimerWheelOnce()
timer := p.tw.AfterFunc(delay, func() {
err := p.PoolExecutor.Execute(r)
if err != nil {
if errors.Is(err, ErrShutdown) {
return
}
p.opts.ErrorHandler.CatchError(r, err)
}
})
return timer.Stop, nil
}
func (p *PoolScheduleExecutor) ScheduleAtFixRate(r Runnable, period time.Duration) (CancelFunc, error) {
p.opts.Logger.Debug("start to schedule new task at fix rate", slog.Duration("period", period))
p.initTimerWheelOnce()
ticker := p.tw.TickFunc(period, func() {
p.opts.Logger.Debug("start to execute task at fix rate", slog.Duration("period", period))
err := p.PoolExecutor.Execute(r)
if err != nil {
if errors.Is(err, ErrShutdown) {
return
}
p.opts.ErrorHandler.CatchError(r, err)
}
})
return ticker.Stop, nil
}
func (p *PoolScheduleExecutor) ScheduleAtCronRate(r Runnable, rule CRONRule) (CancelFunc, error) {
expr, err := cronexpr.ParseStrict(rule.Expr)
if err != nil {
return nil, ErrInvalidCronExpr
}
location, err := time.LoadLocation(rule.Timezone)
if err != nil {
return nil, ErrInvalidCronTimezone
}
p.opts.Logger.Debug("start to schedule new task at cron rate", slog.Any("rule", rule))
removeFunc := p.dispatcher.AddTask(r, expr, location)
p.cronScheduleOnce.Do(p.dispatchCRON)
return removeFunc, nil
}
func (p *PoolScheduleExecutor) dispatchCRON() {
routine.GoWithRecovery(p.opts.Logger, func() {
p.opts.Logger.Debug("start to dispatch cron tasks")
ch := p.dispatcher.GetReadyTask()
for r := range ch {
p.opts.Logger.Debug("start to execute cron task")
err := p.Execute(r)
if err != nil {
if errors.Is(err, ErrShutdown) {
return
}
p.opts.Logger.Debug("fail to execute cron task")
p.opts.ErrorHandler.CatchError(r, err)
}
}
}, p.dispatchCRON)
}
func (p *PoolScheduleExecutor) Shutdown(ctx context.Context) error {
defer func() {
if p.tw != nil {
// wakeup tw
p.tw.Tick(1 * time.Millisecond)
p.tw.Close()
}
}()
defer func() {
p.dispatcher.Shutdown()
}()
return p.PoolExecutor.Shutdown(ctx)
}