-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduleMaker.go
532 lines (507 loc) · 13.7 KB
/
scheduleMaker.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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package jobrunner
import (
"fmt"
"sort"
"time"
)
type ScheduleMaker interface {
// OnSeconds sets up the schedule maker on second's level; if not called or called with no parameters, then every second is considered to be set up
OnSeconds(seconds ...int) ScheduleMaker
// OnMinutes sets up the schedule maker on minute's level; if not called or called with no parameters, then every minute is considered to be set up
OnMinutes(minutes ...int) ScheduleMaker
// AtHours sets up the schedule maker on hour's level; if not called or called with no parameters, then every hour is considered to be set up
AtHours(hours ...int) ScheduleMaker
// OnWeekdays sets up the schedule maker on weekday's level; if not called or called with no parameters, then every weekday is considered to be set up
OnWeekdays(weekdays ...time.Weekday) ScheduleMaker
// OnDays sets up the schedule maker on day's level; if not called or called with no parameters, then every day is considered to be set up
OnDays(days ...int) ScheduleMaker
// InMonths sets up the schedule maker on month's level; if not called or called with no parameters, then every month is considered to be set up
InMonths(months ...time.Month) ScheduleMaker
// InYears sets up the schedule maker on year's level; if not called or called with no parameters, then every year is considered to be set up
InYears(years ...int) ScheduleMaker
// From sets up the schedule maker for its start datetime; if not called or called with no parameters, then the schedule will start immediately
From(start time.Time) ScheduleMaker
// Till sets up the schedule maker for its end datetime; if not called or called with no parameters, then the schedule won't stop until its data increment
Till(end time.Time) ScheduleMaker
// Timezone sets up the schedule maker for its timezone location; if not called or called with no parameters, then the schedule takes time.Local as default
Timezone(timezone *time.Location) ScheduleMaker
// SkipOverdue sets up the schedule maker to skip an overdue schedule or not; if not called, then the schedule defaults to not skip overdues
SkipOverdue() ScheduleMaker
// Done returns a compiled schedule based on all previously configured settings
Schedule() (Schedule, error)
}
type scheduleMaker struct {
seconds []bool
minutes []bool
hours []bool
weekdays []bool
days []bool
months []bool
years map[int]bool
from *time.Time
till *time.Time
timezone *time.Location
skipOverdue bool
}
// NewScheduleMaker creates an empty scheduleMaker for consumer to manually configure a preferred schedule
func NewScheduleMaker() ScheduleMaker {
return &scheduleMaker{
[]bool{}, // seconds
[]bool{}, // minutes
[]bool{}, // hours
[]bool{}, // weekdays
[]bool{}, // days
[]bool{}, // months
map[int]bool{}, // years
nil, // from
nil, // till
time.Local, // timezone
false, // skipOverdue
}
}
func generateFlagsData(data []bool, total int, values ...int) []bool {
if len(values) == 0 {
data = make([]bool, total)
for value := 0; value < total; value++ {
data[value] = true
}
} else {
if len(data) < total {
for count := len(data); count < total; count++ {
data = append(data, false)
}
}
for _, value := range values {
data[value%total] = true
}
}
return data
}
// OnSeconds sets up the scheduleMaker on second's level; if not called or called with no parameters, then every second is considered to be set up
func (scheduleMaker *scheduleMaker) OnSeconds(seconds ...int) ScheduleMaker {
scheduleMaker.seconds = generateFlagsData(
scheduleMaker.seconds,
60,
seconds...,
)
return scheduleMaker
}
// OnMinutes sets up the scheduleMaker on minute's level; if not called or called with no parameters, then every minute is considered to be set up
func (scheduleMaker *scheduleMaker) OnMinutes(minutes ...int) ScheduleMaker {
scheduleMaker.minutes = generateFlagsData(
scheduleMaker.minutes,
60,
minutes...,
)
return scheduleMaker
}
// AtHours sets up the scheduleMaker on hour's level; if not called or called with no parameters, then every hour is considered to be set up
func (scheduleMaker *scheduleMaker) AtHours(hours ...int) ScheduleMaker {
scheduleMaker.hours = generateFlagsData(
scheduleMaker.hours,
24,
hours...,
)
return scheduleMaker
}
// OnSeconds sets up the scheduleMaker on weekday's level; if not called or called with no parameters, then every weekday is considered to be set up
func (scheduleMaker *scheduleMaker) OnWeekdays(weekdays ...time.Weekday) ScheduleMaker {
var weekdaysInInt = []int{}
for _, weekday := range weekdays {
weekdaysInInt = append(weekdaysInInt, int(weekday))
}
scheduleMaker.weekdays = generateFlagsData(
scheduleMaker.weekdays,
7,
weekdaysInInt...,
)
return scheduleMaker
}
// OnSeconds sets up the scheduleMaker on day's level; if not called or called with no parameters, then every day is considered to be set up
func (scheduleMaker *scheduleMaker) OnDays(days ...int) ScheduleMaker {
var daysInInt = []int{}
for _, day := range days {
// due to the fact that day value start from 1 instead of 0...
daysInInt = append(daysInInt, day-1)
}
scheduleMaker.days = generateFlagsData(
scheduleMaker.days,
31,
daysInInt...,
)
return scheduleMaker
}
// OnSeconds sets up the scheduleMaker on month's level; if not called or called with no parameters, then every month is considered to be set up
func (scheduleMaker *scheduleMaker) InMonths(months ...time.Month) ScheduleMaker {
var monthsInInt = []int{}
for _, month := range months {
// due to the fact that month value start from 1 instead of 0...
monthsInInt = append(monthsInInt, int(month-1))
}
scheduleMaker.months = generateFlagsData(
scheduleMaker.months,
12,
monthsInInt...,
)
return scheduleMaker
}
// OnSeconds sets up the scheduleMaker on year's level; if not called or called with no parameters, then every year is considered to be set up
func (scheduleMaker *scheduleMaker) InYears(years ...int) ScheduleMaker {
if len(years) > 0 {
for _, year := range years {
scheduleMaker.years[year] = true
}
}
return scheduleMaker
}
// OnSeconds sets up the scheduleMaker on second's level; if not called or called with no parameters, then every second is considered to be set up
func (scheduleMaker *scheduleMaker) From(start time.Time) ScheduleMaker {
scheduleMaker.from = &start
return scheduleMaker
}
// OnSeconds sets up the scheduleMaker on second's level; if not called or called with no parameters, then every second is considered to be set up
func (scheduleMaker *scheduleMaker) Till(end time.Time) ScheduleMaker {
scheduleMaker.till = &end
return scheduleMaker
}
// Timezone sets up the schedule maker for its timezone location; if not called or called with no parameters, then the schedule takes time.Local as default
func (scheduleMaker *scheduleMaker) Timezone(timezone *time.Location) ScheduleMaker {
if timezone != nil {
scheduleMaker.timezone = timezone
} else {
scheduleMaker.timezone = time.Local
}
return scheduleMaker
}
// SkipOverdue sets up the schedule maker to skip an overdue schedule or not; if not called, then the schedule defaults to not skip overdues
func (scheduleMaker *scheduleMaker) SkipOverdue() ScheduleMaker {
scheduleMaker.skipOverdue = true
return scheduleMaker
}
func constructValueSlice(values []bool, total int) []int {
var data = []int{}
if len(values) == 0 {
for index := 0; index < total; index++ {
data = append(data, index)
}
} else {
for index, valid := range values {
if valid {
data = append(data, index)
}
}
}
return data
}
func constructWeekdayMap(weekdays []bool) map[time.Weekday]bool {
var data = map[time.Weekday]bool{}
if len(weekdays) == 0 {
for weekday := 0; weekday < 7; weekday++ {
data[time.Weekday(weekday)] = true
}
} else {
for weekday, valid := range weekdays {
if valid {
data[time.Weekday(weekday)] = true
}
}
}
return data
}
func constructYearSlice(years map[int]bool) []int {
var data = []int{}
if len(years) == 0 {
// hard-code this to allow execution for 100 years if no year is specified
var currentTime = time.Now()
var currentYear = currentTime.Year()
for year := currentYear; year < currentYear+100; year++ {
data = append(data, year)
}
} else {
for year, valid := range years {
if valid {
data = append(data, year)
}
}
sort.Ints(data)
}
return data
}
func constructScheduleTemplate(scheduleMaker *scheduleMaker) *schedule {
var schedule = &schedule{
secondIndex: 0,
seconds: constructValueSlice(scheduleMaker.seconds, 60),
minuteIndex: 0,
minutes: constructValueSlice(scheduleMaker.minutes, 60),
hourIndex: 0,
hours: constructValueSlice(scheduleMaker.hours, 24),
dayIndex: 0,
days: constructValueSlice(scheduleMaker.days, 31),
monthIndex: 0,
months: constructValueSlice(scheduleMaker.months, 12),
yearIndex: 0,
years: constructYearSlice(scheduleMaker.years),
weekdays: constructWeekdayMap(scheduleMaker.weekdays),
till: scheduleMaker.till,
timezone: scheduleMaker.timezone,
skipOverdue: scheduleMaker.skipOverdue,
}
schedule.second = schedule.seconds[schedule.secondIndex]
schedule.minute = schedule.minutes[schedule.minuteIndex]
schedule.hour = schedule.hours[schedule.hourIndex]
schedule.day = schedule.days[schedule.dayIndex]
schedule.month = schedule.months[schedule.monthIndex]
schedule.year = schedule.years[schedule.yearIndex]
return schedule
}
func findValueMatch(value int, values []int) (int, int, bool, bool) {
var count = len(values)
if count == 0 {
return 0, 0, false, false
}
for index := 0; index < count; index++ {
if value > values[index] {
continue
}
return values[index], index, value < values[index], false
}
return values[0], 0, true, true
}
func isWeekdayMatch(year, month, day int, weekdays map[time.Weekday]bool) bool {
if len(weekdays) == 0 {
return true
}
var date = time.Date(
year,
time.Month(month+1),
day+1,
0,
0,
0,
0,
time.Local,
)
var valid, found = weekdays[date.Weekday()]
return found && valid
}
func determineScheduleIndex(
start time.Time,
schedule *schedule,
) (bool, time.Time, error) {
var increment bool
var overflow bool
schedule.year, schedule.yearIndex, increment, overflow = findValueMatch(
start.Year(),
schedule.years,
)
if overflow {
return false, start, fmt.Errorf("Invalid schedule configuration: no valid next execution time available")
} else if increment {
return false,
time.Date(
schedule.year,
time.January,
1,
0,
0,
0,
0,
schedule.timezone,
),
nil
}
schedule.month, schedule.monthIndex, increment, overflow = findValueMatch(
int(start.Month())-1,
schedule.months,
)
if overflow {
return false,
time.Date(
start.Year()+1,
time.January,
1,
0,
0,
0,
0,
schedule.timezone,
),
nil
} else if increment {
return false,
time.Date(
schedule.year,
time.Month(schedule.month+1),
1,
0,
0,
0,
0,
schedule.timezone,
),
nil
}
schedule.day, schedule.dayIndex, increment, overflow = findValueMatch(
start.Day()-1,
schedule.days,
)
if overflow {
return false,
time.Date(
start.Year(),
start.Month()+1,
1,
0,
0,
0,
0,
schedule.timezone,
),
nil
} else if increment {
return false,
time.Date(
schedule.year,
time.Month(schedule.month+1),
schedule.day+1,
0,
0,
0,
0,
schedule.timezone,
),
nil
}
if !isWeekdayMatch(
schedule.year,
schedule.month,
schedule.day,
schedule.weekdays,
) {
return false,
time.Date(
start.Year(),
start.Month(),
start.Day()+1,
0,
0,
0,
0,
schedule.timezone,
),
nil
}
schedule.hour, schedule.hourIndex, increment, overflow = findValueMatch(
start.Hour(),
schedule.hours,
)
if overflow {
return false,
time.Date(
start.Year(),
start.Month(),
start.Day()+1,
0,
0,
0,
0,
schedule.timezone,
),
nil
} else if increment {
return false,
time.Date(
schedule.year,
time.Month(schedule.month+1),
schedule.day+1,
schedule.hour,
0,
0,
0,
schedule.timezone,
),
nil
}
schedule.minute, schedule.minuteIndex, increment, overflow = findValueMatch(
start.Minute(),
schedule.minutes,
)
if overflow {
return false,
time.Date(
start.Year(),
start.Month(),
start.Day(),
start.Hour()+1,
0,
0,
0,
schedule.timezone,
),
nil
} else if increment {
return false,
time.Date(
schedule.year,
time.Month(schedule.month+1),
schedule.day+1,
schedule.hour,
schedule.minute,
0,
0,
schedule.timezone,
),
nil
}
schedule.second, schedule.secondIndex, _, overflow = findValueMatch(
start.Second(),
schedule.seconds,
)
if overflow {
return false,
time.Date(
start.Year(),
start.Month(),
start.Day(),
start.Hour(),
start.Minute()+1,
0,
0,
schedule.timezone,
),
nil
}
return true, start, nil
}
func initialiseSchedule(
start time.Time,
schedule *schedule,
) error {
var calcError error
for completed := false; !completed; {
completed, start, calcError = determineScheduleIndex(
start,
schedule,
)
if calcError != nil {
return calcError
}
}
return nil
}
// Schedule returns a compiled schedule based on all previously configured settings
func (scheduleMaker *scheduleMaker) Schedule() (Schedule, error) {
var start = time.Now()
if scheduleMaker.from != nil &&
scheduleMaker.from.After(start) {
start = *scheduleMaker.from
}
var schedule = constructScheduleTemplate(
scheduleMaker,
)
var calcError = initialiseSchedule(
start,
schedule,
)
return schedule, calcError
}