forked from codnect/chrono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
251 lines (192 loc) · 6.28 KB
/
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package chrono
import (
"context"
"errors"
"sync"
"time"
)
type TaskExecutor interface {
Schedule(task Task, delay time.Duration) (ScheduledTask, error)
ScheduleWithFixedDelay(task Task, initialDelay time.Duration, delay time.Duration) (ScheduledTask, error)
ScheduleAtFixedRate(task Task, initialDelay time.Duration, period time.Duration) (ScheduledTask, error)
IsShutdown() bool
Shutdown() chan bool
}
type SimpleTaskExecutor struct {
nextSequence int
isShutdown bool
executorMu sync.RWMutex
timer *time.Timer
taskWaitGroup sync.WaitGroup
taskQueue ScheduledTaskQueue
newTaskChannel chan *ScheduledRunnableTask
rescheduleTaskChannel chan *ScheduledRunnableTask
taskRunner TaskRunner
shutdownChannel chan chan bool
}
func NewDefaultTaskExecutor() TaskExecutor {
return NewSimpleTaskExecutor(NewDefaultTaskRunner())
}
func NewSimpleTaskExecutor(runner TaskRunner) *SimpleTaskExecutor {
if runner == nil {
runner = NewDefaultTaskRunner()
}
executor := &SimpleTaskExecutor{
timer: time.NewTimer(1 * time.Hour),
taskQueue: make(ScheduledTaskQueue, 0),
newTaskChannel: make(chan *ScheduledRunnableTask),
rescheduleTaskChannel: make(chan *ScheduledRunnableTask),
taskRunner: runner,
shutdownChannel: make(chan chan bool),
}
executor.timer.Stop()
go executor.run()
return executor
}
func (executor *SimpleTaskExecutor) Schedule(task Task, delay time.Duration) (ScheduledTask, error) {
if task == nil {
return nil, errors.New("task cannot be nil")
}
executor.executorMu.Lock()
if executor.isShutdown {
executor.executorMu.Unlock()
return nil, errors.New("no new task won't be accepted because executor is already shut down")
}
executor.nextSequence++
scheduledTask, err := CreateScheduledRunnableTask(executor.nextSequence, task, executor.calculateTriggerTime(delay), 0, false)
executor.executorMu.Unlock()
if err != nil {
return nil, err
}
executor.addNewTask(scheduledTask)
return scheduledTask, nil
}
func (executor *SimpleTaskExecutor) ScheduleWithFixedDelay(task Task, initialDelay time.Duration, delay time.Duration) (ScheduledTask, error) {
if task == nil {
return nil, errors.New("task cannot be nil")
}
executor.executorMu.Lock()
if executor.isShutdown {
executor.executorMu.Unlock()
return nil, errors.New("no new task won't be accepted because executor is already shut down")
}
executor.nextSequence++
scheduledTask, err := CreateScheduledRunnableTask(executor.nextSequence, task, executor.calculateTriggerTime(initialDelay), delay, false)
executor.executorMu.Unlock()
if err != nil {
return nil, err
}
executor.addNewTask(scheduledTask)
return scheduledTask, nil
}
func (executor *SimpleTaskExecutor) ScheduleAtFixedRate(task Task, initialDelay time.Duration, period time.Duration) (ScheduledTask, error) {
if task == nil {
return nil, errors.New("task cannot be nil")
}
executor.executorMu.Lock()
if executor.isShutdown {
executor.executorMu.Unlock()
return nil, errors.New("no new task won't be accepted because executor is already shut down")
}
executor.nextSequence++
scheduledTask, err := CreateScheduledRunnableTask(executor.nextSequence, task, executor.calculateTriggerTime(initialDelay), period, true)
executor.executorMu.Unlock()
if err != nil {
return nil, err
}
executor.addNewTask(scheduledTask)
return scheduledTask, nil
}
func (executor *SimpleTaskExecutor) IsShutdown() bool {
executor.executorMu.Lock()
defer executor.executorMu.Unlock()
return executor.isShutdown
}
func (executor *SimpleTaskExecutor) Shutdown() chan bool {
executor.executorMu.Lock()
defer executor.executorMu.Unlock()
if executor.isShutdown {
panic("executor is already shut down")
}
executor.isShutdown = true
stoppedChan := make(chan bool)
executor.shutdownChannel <- stoppedChan
return stoppedChan
}
func (executor *SimpleTaskExecutor) calculateTriggerTime(delay time.Duration) time.Time {
if delay < 0 {
delay = 0
}
return time.Now().Add(delay)
}
func (executor *SimpleTaskExecutor) addNewTask(task *ScheduledRunnableTask) {
executor.newTaskChannel <- task
}
func (executor *SimpleTaskExecutor) run() {
for {
executor.taskQueue.SorByTriggerTime()
if len(executor.taskQueue) == 0 {
executor.timer.Stop()
} else {
executor.timer.Reset(executor.taskQueue[0].getDelay())
}
for {
select {
case clock := <-executor.timer.C:
executor.timer.Stop()
taskIndex := -1
for index, scheduledTask := range executor.taskQueue {
if scheduledTask.triggerTime.After(clock) || scheduledTask.triggerTime.IsZero() {
break
}
taskIndex = index
if scheduledTask.IsCancelled() {
continue
}
if scheduledTask.isPeriodic() && scheduledTask.isFixedRate() {
scheduledTask.triggerTime = scheduledTask.triggerTime.Add(scheduledTask.period)
}
executor.startTask(scheduledTask)
}
executor.taskQueue = executor.taskQueue[taskIndex+1:]
case newScheduledTask := <-executor.newTaskChannel:
executor.timer.Stop()
executor.taskQueue = append(executor.taskQueue, newScheduledTask)
case rescheduledTask := <-executor.rescheduleTaskChannel:
executor.timer.Stop()
executor.taskQueue = append(executor.taskQueue, rescheduledTask)
case stoppedChan := <-executor.shutdownChannel:
executor.timer.Stop()
executor.taskWaitGroup.Wait()
stoppedChan <- true
return
}
break
}
}
}
func (executor *SimpleTaskExecutor) startTask(scheduledRunnableTask *ScheduledRunnableTask) {
executor.taskWaitGroup.Add(1)
executor.taskRunner.Run(func(ctx context.Context) {
defer func() {
if executor.IsShutdown() {
scheduledRunnableTask.Cancel()
executor.taskWaitGroup.Done()
return
}
executor.taskWaitGroup.Done()
if !scheduledRunnableTask.isPeriodic() {
scheduledRunnableTask.Cancel()
} else {
if !scheduledRunnableTask.isFixedRate() {
scheduledRunnableTask.triggerTime = executor.calculateTriggerTime(scheduledRunnableTask.period)
executor.rescheduleTaskChannel <- scheduledRunnableTask
}
}
}()
if scheduledRunnableTask.isPeriodic() && scheduledRunnableTask.isFixedRate() {
executor.rescheduleTaskChannel <- scheduledRunnableTask
}
scheduledRunnableTask.task(ctx)
})
}