-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathclockwork.go
More file actions
471 lines (410 loc) · 11.1 KB
/
clockwork.go
File metadata and controls
471 lines (410 loc) · 11.1 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Package clockwork enables simple and intuitive scheduling in Go.
//
// Examples:
// sched.Schedule().Every(10).Seconds().Do(something)
// sched.Schedule().Every(3).Minutes().Do(something)
// sched.Schedule().Every(4).Hours().Do(something)
// sched.Schedule().Every(2).Days().At("12:32").Do(something)
// sched.Schedule().Every(12).Weeks().Do(something)
// sched.Schedule().Every(1).Monday().Do(something)
// sched.Schedule().Every(1).Saturday().At("8:00").Do(something)
package clockwork
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/google/uuid"
)
// TimeUnit is an numeration used for handling
// time units internally.
type TimeUnit int
const (
none = iota
second
minute
hour
day
week
monday
tuesday
wednesday
thursday
friday
saturday
sunday
)
var timeNow = func() time.Time {
return time.Now()
}
// Job struct handles all the data required to
// schedule and run jobs.
type Job struct {
identifier string
scheduler *Scheduler
unit TimeUnit
frequency int
useAt bool
atHour int
atMinute int
workFunc func()
nextScheduledRun time.Time
}
// Every is a method that fills the given Job struct with the given frequency
func (j *Job) Every(frequencies ...int) *Job {
l := len(frequencies)
switch l {
case 0:
j.frequency = 1
case 1:
if frequencies[0] <= 0 {
panic("Every expects frequency to be greater than of equal to 1")
}
j.frequency = frequencies[0]
default:
panic("Every expects 0 or 1 arguments")
}
return j
}
// EverySingle is deprecated predecessor to Every()
func (j *Job) EverySingle() *Job {
return j.Every()
}
// At method fills the given Job struct atHour and atMinute fields
// with the provided information
func (j *Job) At(t string) *Job {
j.useAt = true
j.atHour, _ = strconv.Atoi(strings.Split(t, ":")[0])
j.atMinute, _ = strconv.Atoi(strings.Split(t, ":")[1])
return j
}
// Do method fills the given job struct with the function pointer
// to the job (user provided task) itself.
func (j *Job) Do(function func()) string {
j.workFunc = function
j.scheduleNextRun()
j.scheduler.jobs = append(j.scheduler.jobs, *j)
return j.identifier
}
func (j *Job) due() bool {
return timeNow().After(j.nextScheduledRun)
}
// Generally, At() can only be used then unit is day or WEEKDAY
func (j *Job) isAtUsedIncorrectly() bool {
return j.useAt == true && (j.unit == second || j.unit == minute ||
j.unit == hour || j.unit == week)
}
// Returns false when job unit is Day or any of the weekdays, vice versa.
// Used for scheduling when job frequency is 1, because day and WEEKDAY
// can be used with At() function which requires different scheduling approach.
func (j *Job) unitNotDayOrWEEKDAY() bool {
return j.unit == second || j.unit == minute ||
j.unit == hour || j.unit == week
}
// Returns false when job unit is or any of the weekdays, vice versa.
// Used for scheduling when job frequency is > 1, because we need to
// manually check for unit since we can't schedule WEEKDAYS with
// frequency > 1 .
func (j *Job) unitNotWEEKDAY() bool {
return j.unit == second || j.unit == minute ||
j.unit == hour || j.unit == day ||
j.unit == week
}
func (j *Job) scheduleNextRun() {
// If Every(frequency) == 1, unit can be anything .
// At() can be used only with day and WEEKDAY
if j.frequency == 1 {
// Panic if usage of "At()" is incorrect
if j.isAtUsedIncorrectly() {
panic(
`Cannot schedule Every(1) with At()
when unit is not day or WEEKDAY`,
) // TODO: Turn this into err
}
// Handle everything except day and WEEKDAY -- these guys don't use At()
if j.unitNotDayOrWEEKDAY() {
if j.nextScheduledRun == (time.Time{}) {
j.nextScheduledRun = timeNow()
}
switch j.unit {
case second:
j.nextScheduledRun = j.nextScheduledRun.Add(1 * time.Second)
case minute:
j.nextScheduledRun = j.nextScheduledRun.Add(1 * time.Minute)
case hour:
j.nextScheduledRun = j.nextScheduledRun.Add(1 * time.Hour)
case week:
// 168 hours in a week
j.nextScheduledRun = j.nextScheduledRun.Add(168 * time.Hour)
}
} else {
// Handle Day and WEEKDAY -- these guys use At()
switch j.unit {
case day:
if j.nextScheduledRun == (time.Time{}) {
now := timeNow()
lastMidnight := time.Date(
now.Year(),
now.Month(),
now.Day(),
0, 0, 0, 0,
time.Local,
)
if j.useAt == true {
j.nextScheduledRun = lastMidnight.Add(
time.Duration(j.atHour)*time.Hour +
time.Duration(j.atMinute)*time.Minute,
)
} else {
// If At is not specified, move the next scheduled run to next midnight
j.nextScheduledRun = lastMidnight.Add(24 * time.Hour)
}
} else {
j.nextScheduledRun = j.nextScheduledRun.Add(24 * time.Hour)
}
case monday:
j.scheduleWeekday(time.Monday)
case tuesday:
j.scheduleWeekday(time.Tuesday)
case wednesday:
j.scheduleWeekday(time.Wednesday)
case thursday:
j.scheduleWeekday(time.Thursday)
case friday:
j.scheduleWeekday(time.Friday)
case saturday:
j.scheduleWeekday(time.Saturday)
case sunday:
j.scheduleWeekday(time.Sunday)
}
}
fmt.Println("Scheduled for ", j.nextScheduledRun)
} else {
// If Every(frequency) > 1, unit has to be either
// second, minute, hour, day, week - not a WEEKDAY .
// At() can be used only with day
// Panic if usage of "At()" is incorrect
if j.isAtUsedIncorrectly() {
panic("Cannot schedule Every(>1) with At() when unit is not day")
// TODO: Turn this into err
}
// Unlike when frequency = 1, here unit can't be anyhing.
// We have to check that it isn't a WEEKDAY
if j.unitNotWEEKDAY() {
// Handle everything except day -- these guys don't use At()
if j.unit != day {
if j.nextScheduledRun == (time.Time{}) {
j.nextScheduledRun = timeNow()
}
switch j.unit {
case second:
j.nextScheduledRun = j.nextScheduledRun.Add(
time.Duration(j.frequency) * time.Second,
)
case minute:
j.nextScheduledRun = j.nextScheduledRun.Add(
time.Duration(j.frequency) * time.Minute,
)
case hour:
j.nextScheduledRun = j.nextScheduledRun.Add(
time.Duration(j.frequency) * time.Hour,
)
case week:
j.nextScheduledRun = j.nextScheduledRun.Add(
time.Duration(j.frequency*168) * time.Hour,
) // 168 hours in a week
}
} else {
// Handle Day -- these guy uses At()
if j.nextScheduledRun == (time.Time{}) {
now := timeNow()
lastMidnight := time.Date(
now.Year(),
now.Month(),
now.Day(),
0, 0, 0, 0,
time.Local,
)
if j.useAt == true {
j.nextScheduledRun = lastMidnight.Add(
time.Duration(j.atHour)*time.Hour +
time.Duration(j.atMinute)*time.Minute,
)
} else {
j.nextScheduledRun = lastMidnight
}
}
j.nextScheduledRun = j.nextScheduledRun.Add(
time.Duration(j.frequency*24) * time.Hour,
)
}
} else {
panic("Cannot schedule Every(>1) when unit is WEEKDAY")
// TODO: Turn this into err
}
fmt.Println("Scheduled for ", j.nextScheduledRun)
// TODO: Turn this into a log
}
return
}
func (j *Job) scheduleWeekday(dayOfWeek time.Weekday) {
if j.nextScheduledRun == (time.Time{}) {
now := timeNow()
lastWeekdayMidnight := time.Date(
now.Year(),
now.Month(),
now.Day()-int(now.Weekday()-dayOfWeek),
0, 0, 0, 0,
time.Local)
if j.useAt == true {
j.nextScheduledRun = lastWeekdayMidnight.Add(
time.Duration(j.atHour)*time.Hour +
time.Duration(j.atMinute)*time.Minute,
)
} else {
j.nextScheduledRun = lastWeekdayMidnight
}
}
j.nextScheduledRun = j.nextScheduledRun.Add(7 * 24 * time.Hour)
}
// Second method fills the given job struct with seconds
func (j *Job) Second() *Job {
j.unit = second
return j
}
// Seconds method fills the given job struct with seconds
func (j *Job) Seconds() *Job {
j.unit = second
return j
}
// Minute method fills the given job struct with minutes
func (j *Job) Minute() *Job {
j.unit = minute
return j
}
// Minutes method fills the given job struct with minutes
func (j *Job) Minutes() *Job {
j.unit = minute
return j
}
// Hour method fills the given job struct with hours
func (j *Job) Hour() *Job {
j.unit = hour
return j
}
// Hours method fills the given job struct with hours
func (j *Job) Hours() *Job {
j.unit = hour
return j
}
// Day method fills the given job struct with days
func (j *Job) Day() *Job {
j.unit = day
return j
}
// Days method fills the given job struct with days
func (j *Job) Days() *Job {
j.unit = day
return j
}
// Week method fills the given job struct with weeks
func (j *Job) Week() *Job {
j.unit = week
return j
}
// Weeks method fills the given job struct with weeks
func (j *Job) Weeks() *Job {
j.unit = week
return j
}
// Monday method fills the given job struct with monday
func (j *Job) Monday() *Job {
j.unit = monday
return j
}
// Tuesday method fills the given job struct with tuesday
func (j *Job) Tuesday() *Job {
j.unit = tuesday
return j
}
// Wednesday method fills the given job struct with wednesday
func (j *Job) Wednesday() *Job {
j.unit = wednesday
return j
}
// Thursday method fills the given job struct with thursday
func (j *Job) Thursday() *Job {
j.unit = thursday
return j
}
// Friday method fills the given job struct with friday
func (j *Job) Friday() *Job {
j.unit = friday
return j
}
// Saturday method fills the given job struct with saturday
func (j *Job) Saturday() *Job {
j.unit = saturday
return j
}
// Sunday method fills the given job struct with sunday
func (j *Job) Sunday() *Job {
j.unit = sunday
return j
}
// Scheduler type is used to store a group of jobs (Job structs)
type Scheduler struct {
identifier string
jobs []Job
polling_interval int
}
// NewScheduler creates and returns a new Scheduler
func NewScheduler() Scheduler {
return Scheduler{
identifier: uuid.New().String(),
jobs: make([]Job, 0),
polling_interval: 333,
}
}
// SetPollingInterval sets the time (in milliseconds) which scheduler will spend
// in sleep during each cycle in the Run method
func (s *Scheduler) SetPollingInterval(milliseconds int) {
s.polling_interval = milliseconds
}
// activateTestMode method sets the timeNow func for testing,
// by setting the current time to a fixed value
func (s *Scheduler) activateTestMode() {
timeNow = func() time.Time {
return time.Date(1, 1, 1, 1, 1, 0, 0, time.Local)
}
}
// Run method on the Scheduler type runs the scheduler.
// This is a blocking method, and should be run as a goroutine.
func (s *Scheduler) Run() {
for {
for jobIdx := range s.jobs {
job := &s.jobs[jobIdx]
if job.due() {
job.scheduleNextRun()
go job.workFunc()
}
}
time.Sleep(time.Duration(s.polling_interval) * time.Millisecond)
}
}
// Schedule method on the Scheduler creates a new Job
// and prepares is for "filling"
func (s *Scheduler) Schedule() *Job {
newJob := Job{
identifier: uuid.New().String(),
scheduler: s,
unit: none,
frequency: 1,
useAt: false,
atHour: 0,
atMinute: 0,
workFunc: nil,
nextScheduledRun: time.Time{}, // zero value
}
return &newJob
}