forked from codnect/chrono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
246 lines (193 loc) · 5.64 KB
/
task.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
package chrono
import (
"context"
"errors"
"fmt"
"sort"
"sync"
"time"
)
type Task func(ctx context.Context)
type SchedulerTask struct {
task Task
startTime time.Time
location *time.Location
}
func CreateSchedulerTask(task Task, options ...Option) (*SchedulerTask, error) {
if task == nil {
return nil, errors.New("task cannot be nil")
}
runnableTask := &SchedulerTask{
task: task,
startTime: time.Time{},
location: time.Local,
}
for _, option := range options {
err := option(runnableTask)
if err != nil {
return nil, err
}
}
return runnableTask, nil
}
func (task *SchedulerTask) GetInitialDelay() time.Duration {
if task.startTime.IsZero() {
return 0
}
now := time.Now().In(task.location)
diff := time.Date(task.startTime.Year(), task.startTime.Month(), task.startTime.Day(), task.startTime.Hour(), task.startTime.Minute(), task.startTime.Second(), 0, time.Local).Sub(
time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), 0, time.Local))
if diff < 0 {
return 0
}
return diff
}
type Option func(task *SchedulerTask) error
func WithTime(t time.Time) Option {
return func(task *SchedulerTask) error {
task.startTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
if t.Location() != nil && t.Location() != time.Local {
task.location = t.Location()
}
return nil
}
}
// Deprecated: Use WithTime instead.
func WithStartTime(year int, month time.Month, day, hour, min, sec int) Option {
return func(task *SchedulerTask) error {
task.startTime = time.Date(year, month, day, hour, min, sec, 0, time.Local)
return nil
}
}
func WithLocation(location string) Option {
return func(task *SchedulerTask) error {
loadedLocation, err := time.LoadLocation(location)
if err != nil {
return fmt.Errorf("location not loaded : %s", location)
}
task.location = loadedLocation
return nil
}
}
type ScheduledTask interface {
Cancel()
IsCancelled() bool
}
type ScheduledRunnableTask struct {
id int
task Task
taskMu sync.RWMutex
triggerTime time.Time
period time.Duration
fixedRate bool
cancelled bool
}
func CreateScheduledRunnableTask(id int, task Task, triggerTime time.Time, period time.Duration, fixedRate bool) (*ScheduledRunnableTask, error) {
if task == nil {
return nil, errors.New("task cannot be nil")
}
if period < 0 {
period = 0
}
return &ScheduledRunnableTask{
id: id,
task: task,
triggerTime: triggerTime,
period: period,
fixedRate: fixedRate,
}, nil
}
func (scheduledRunnableTask *ScheduledRunnableTask) Cancel() {
scheduledRunnableTask.taskMu.Lock()
defer scheduledRunnableTask.taskMu.Unlock()
scheduledRunnableTask.cancelled = true
}
func (scheduledRunnableTask *ScheduledRunnableTask) IsCancelled() bool {
scheduledRunnableTask.taskMu.Lock()
defer scheduledRunnableTask.taskMu.Unlock()
return scheduledRunnableTask.cancelled
}
func (scheduledRunnableTask *ScheduledRunnableTask) getDelay() time.Duration {
return scheduledRunnableTask.triggerTime.Sub(time.Now())
}
func (scheduledRunnableTask *ScheduledRunnableTask) isPeriodic() bool {
return scheduledRunnableTask.period != 0
}
func (scheduledRunnableTask *ScheduledRunnableTask) isFixedRate() bool {
return scheduledRunnableTask.fixedRate
}
type ScheduledTaskQueue []*ScheduledRunnableTask
func (queue ScheduledTaskQueue) Len() int {
return len(queue)
}
func (queue ScheduledTaskQueue) Swap(i, j int) {
queue[i], queue[j] = queue[j], queue[i]
}
func (queue ScheduledTaskQueue) Less(i, j int) bool {
return queue[i].triggerTime.Before(queue[j].triggerTime)
}
func (queue ScheduledTaskQueue) SorByTriggerTime() {
sort.Sort(queue)
}
type TriggerTask struct {
task Task
currentScheduledTask *ScheduledRunnableTask
executor TaskExecutor
triggerContext *SimpleTriggerContext
triggerContextMu sync.RWMutex
trigger Trigger
nextTriggerTime time.Time
}
func CreateTriggerTask(task Task, executor TaskExecutor, trigger Trigger) (*TriggerTask, error) {
if task == nil {
return nil, errors.New("task cannot be nil")
}
if executor == nil {
return nil, errors.New("executor cannot be nil")
}
if trigger == nil {
return nil, errors.New("trigger cannot be nil")
}
return &TriggerTask{
task: task,
executor: executor,
triggerContext: NewSimpleTriggerContext(),
trigger: trigger,
}, nil
}
func (task *TriggerTask) Cancel() {
task.triggerContextMu.Lock()
defer task.triggerContextMu.Unlock()
task.currentScheduledTask.Cancel()
}
func (task *TriggerTask) IsCancelled() bool {
task.triggerContextMu.Lock()
defer task.triggerContextMu.Unlock()
return task.currentScheduledTask.IsCancelled()
}
func (task *TriggerTask) Schedule() (ScheduledTask, error) {
task.triggerContextMu.Lock()
defer task.triggerContextMu.Unlock()
task.nextTriggerTime = task.trigger.NextExecutionTime(task.triggerContext)
if task.nextTriggerTime.IsZero() {
return nil, errors.New("could not schedule task because of the fact that schedule time is zero")
}
initialDelay := task.nextTriggerTime.Sub(time.Now())
currentScheduledTask, err := task.executor.Schedule(task.Run, initialDelay)
if err != nil {
return nil, err
}
task.currentScheduledTask = currentScheduledTask.(*ScheduledRunnableTask)
return task, nil
}
func (task *TriggerTask) Run(ctx context.Context) {
task.triggerContextMu.Lock()
executionTime := time.Now()
task.task(ctx)
completionTime := time.Now()
task.triggerContext.Update(completionTime, executionTime, task.nextTriggerTime)
task.triggerContextMu.Unlock()
if !task.IsCancelled() {
task.Schedule()
}
}