-
Notifications
You must be signed in to change notification settings - Fork 345
/
scheduler.go
265 lines (226 loc) · 5.58 KB
/
scheduler.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
package gocron
import (
"sort"
"time"
)
// Scheduler struct, the only data member is the list of jobs.
// - implements the sort.Interface{} for sorting jobs, by the time nextRun
type Scheduler struct {
jobs [MAXJOBNUM]*Job // Array store jobs
size int // Size of jobs which jobs holding.
loc *time.Location // Location to use when scheduling jobs with specified times
}
var (
defaultScheduler = NewScheduler()
)
// NewScheduler creates a new scheduler
func NewScheduler() *Scheduler {
return &Scheduler{
jobs: [MAXJOBNUM]*Job{},
size: 0,
loc: loc,
}
}
// Jobs returns the list of Jobs from the Scheduler
func (s *Scheduler) Jobs() []*Job {
return s.jobs[:s.size]
}
func (s *Scheduler) Len() int {
return s.size
}
func (s *Scheduler) Swap(i, j int) {
s.jobs[i], s.jobs[j] = s.jobs[j], s.jobs[i]
}
func (s *Scheduler) Less(i, j int) bool {
return s.jobs[j].nextRun.Unix() >= s.jobs[i].nextRun.Unix()
}
// ChangeLoc changes the default time location
func (s *Scheduler) ChangeLoc(newLocation *time.Location) {
s.loc = newLocation
}
// Get the current runnable jobs, which shouldRun is True
func (s *Scheduler) getRunnableJobs() (runningJobs [MAXJOBNUM]*Job, n int) {
runnableJobs := [MAXJOBNUM]*Job{}
n = 0
sort.Sort(s)
for i := 0; i < s.size; i++ {
if s.jobs[i].shouldRun() {
runnableJobs[n] = s.jobs[i]
n++
} else {
break
}
}
return runnableJobs, n
}
// NextRun datetime when the next job should run.
func (s *Scheduler) NextRun() (*Job, time.Time) {
if s.size <= 0 {
return nil, time.Now()
}
sort.Sort(s)
return s.jobs[0], s.jobs[0].nextRun
}
// Every schedule a new periodic job with interval
func (s *Scheduler) Every(interval uint64) *Job {
job := NewJob(interval).Loc(s.loc)
s.jobs[s.size] = job
s.size++
return job
}
// RunPending runs all the jobs that are scheduled to run.
func (s *Scheduler) RunPending() {
runnableJobs, n := s.getRunnableJobs()
if n != 0 {
for i := 0; i < n; i++ {
go runnableJobs[i].run()
runnableJobs[i].lastRun = time.Now()
runnableJobs[i].scheduleNextRun()
}
}
}
// RunAll run all jobs regardless if they are scheduled to run or not
func (s *Scheduler) RunAll() {
s.RunAllwithDelay(0)
}
// RunAllwithDelay runs all jobs with delay seconds
func (s *Scheduler) RunAllwithDelay(d int) {
for i := 0; i < s.size; i++ {
go s.jobs[i].run()
if 0 != d {
time.Sleep(time.Duration(d))
}
}
}
// Remove specific job j by function
func (s *Scheduler) Remove(j interface{}) {
s.removeByCondition(func(someJob *Job) bool {
return someJob.jobFunc == getFunctionName(j)
})
}
// RemoveByRef removes specific job j by reference
func (s *Scheduler) RemoveByRef(j *Job) {
s.removeByCondition(func(someJob *Job) bool {
return someJob == j
})
}
// RemoveByTag removes specific job j by tag
func (s *Scheduler) RemoveByTag(t string) {
s.removeByCondition(func(someJob *Job) bool {
for _, a := range someJob.tags {
if a == t {
return true
}
}
return false
})
}
func (s *Scheduler) removeByCondition(shouldRemove func(*Job) bool) {
i := 0
// keep deleting until no more jobs match the criteria
for {
found := false
for ; i < s.size; i++ {
if shouldRemove(s.jobs[i]) {
found = true
break
}
}
if !found {
return
}
for j := (i + 1); j < s.size; j++ {
s.jobs[i] = s.jobs[j]
i++
}
s.size--
s.jobs[s.size] = nil
}
}
// Scheduled checks if specific job j was already added
func (s *Scheduler) Scheduled(j interface{}) bool {
for _, job := range s.jobs {
if job.jobFunc == getFunctionName(j) {
return true
}
}
return false
}
// Clear delete all scheduled jobs
func (s *Scheduler) Clear() {
for i := 0; i < s.size; i++ {
s.jobs[i] = nil
}
s.size = 0
}
// Start all the pending jobs
// Add seconds ticker
func (s *Scheduler) Start() chan bool {
stopped := make(chan bool, 1)
ticker := time.NewTicker(1 * time.Second)
go func() {
for {
select {
case <-ticker.C:
s.RunPending()
case <-stopped:
ticker.Stop()
return
}
}
}()
return stopped
}
// The following methods are shortcuts for not having to
// create a Scheduler instance
// Every schedules a new periodic job running in specific interval
func Every(interval uint64) *Job {
return defaultScheduler.Every(interval)
}
// RunPending run all jobs that are scheduled to run
//
// Please note that it is *intended behavior that run_pending()
// does not run missed jobs*. For example, if you've registered a job
// that should run every minute and you only call run_pending()
// in one hour increments then your job won't be run 60 times in
// between but only once.
func RunPending() {
defaultScheduler.RunPending()
}
// RunAll run all jobs regardless if they are scheduled to run or not.
func RunAll() {
defaultScheduler.RunAll()
}
// RunAllwithDelay run all the jobs with a delay in seconds
//
// A delay of `delay` seconds is added between each job. This can help
// to distribute the system load generated by the jobs more evenly over
// time.
func RunAllwithDelay(d int) {
defaultScheduler.RunAllwithDelay(d)
}
// Start run all jobs that are scheduled to run
func Start() chan bool {
return defaultScheduler.Start()
}
// Clear all scheduled jobs
func Clear() {
defaultScheduler.Clear()
}
// Remove a specific job
func Remove(j interface{}) {
defaultScheduler.Remove(j)
}
// Scheduled checks if specific job j was already added
func Scheduled(j interface{}) bool {
for _, job := range defaultScheduler.jobs {
if job.jobFunc == getFunctionName(j) {
return true
}
}
return false
}
// NextRun gets the next running time
func NextRun() (job *Job, time time.Time) {
return defaultScheduler.NextRun()
}