-
Notifications
You must be signed in to change notification settings - Fork 3
/
scheduler.go
205 lines (177 loc) · 4.22 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
package scheduler
import (
"github.com/GoFarsi/scheduler/helper"
"github.com/GoFarsi/scheduler/types"
"sort"
"time"
)
var (
defaultScheduler = New()
)
// Scheduler is the main struct of the scheduler
type Scheduler struct {
JobList [types.MaxJobs]*Job // Jobs is the array of jobs
JobSize int // JobSize is the number of jobs
Location *time.Location // Location is the location of the scheduler
}
// New creates a new scheduler
func New() *Scheduler {
return &Scheduler{
JobList: [types.MaxJobs]*Job{},
JobSize: 0,
Location: types.TimeZone,
}
}
// Jobs returns list of jobs from scheduler
func (s *Scheduler) Jobs() []*Job {
return s.JobList[:s.JobSize]
}
// Len returns the number of jobs in the scheduler
func (s *Scheduler) Len() int {
return s.JobSize
}
// Swap swaps the two jobs
func (s *Scheduler) Swap(i, j int) {
s.JobList[i], s.JobList[j] = s.JobList[j], s.JobList[i]
}
// Less returns true if the job at index i is less than the job at index j
func (s *Scheduler) Less(i, j int) bool {
return s.JobList[j].NextRun.Unix() >= s.JobList[i].NextRun.Unix()
}
// ChangeLocation changes the default time location of the scheduler
func (s *Scheduler) ChangeLocation(newLocation *time.Location) {
s.Location = newLocation
}
// GetRunnableJobs returns a list of jobs that are ready to run
func (s *Scheduler) GetRunnableJobs() (runningJobs [types.MaxJobs]*Job, n int) {
runnableJobs := [types.MaxJobs]*Job{}
n = 0
sort.Sort(s)
for i := 0; i < s.JobSize; i++ {
if s.JobList[i].JobIsRunNow() {
runnableJobs[n] = s.JobList[i]
n++
} else {
break
}
}
return runnableJobs, n
}
// NextRun returns the next run time of the job at index i
func (s *Scheduler) NextRun() (*Job, time.Time) {
if s.JobSize <= 0 {
return nil, time.Now()
}
sort.Sort(s)
return s.JobList[0], s.JobList[0].NextRun
}
// Every schedules a new job to run every duration
func (s *Scheduler) Every(interval uint64) *Job {
j := NewJob(interval).Location(s.Location)
s.JobList[s.JobSize] = j
s.JobSize++
return j
}
// RunPending runs all pending jobs
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].NextJobRun()
}
}
}
// RunAll runs all jobs
func (s *Scheduler) RunAll() {
s.RunAllWithDelay(0)
}
// RunAllWithDelay runs all jobs with a delay
func (s *Scheduler) RunAllWithDelay(d int) {
for i := 0; i < s.JobSize; i++ {
go s.JobList[i].Run()
if 0 != d {
time.Sleep(time.Duration(d))
}
}
}
// Remove job by function
func (s *Scheduler) Remove(j interface{}) {
s.RemoveByCondition(func(someJob *Job) bool {
return someJob.JobFunction == helper.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
})
}
// RemoveByCondition removes specific job j by condition
func (s *Scheduler) RemoveByCondition(remove func(*Job) bool) {
i := 0
// Delete jobs until no more match the criteria
for {
found := false
for ; i < s.JobSize; i++ {
if remove(s.JobList[i]) {
found = true
break
}
}
if !found {
return
}
for j := i + 1; j < s.JobSize; j++ {
s.JobList[i] = s.JobList[j]
i++
}
s.JobSize--
s.JobList[s.JobSize] = nil
}
}
// Scheduled returns true if job j is scheduled
func (s *Scheduler) Scheduled(j interface{}) bool {
for _, jb := range s.JobList {
if jb.JobFunction == helper.GetFunctionName(j) {
return true
}
}
return false
}
// Clear removes all scheduled jobs
func (s *Scheduler) Clear() {
for i := 0; i < s.JobSize; i++ {
s.JobList[i] = nil
}
s.JobSize = 0
}
// Start starts all pending jobs
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
}