-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
229 lines (182 loc) · 3.91 KB
/
api.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
// Package chronos is a scheduling tool for Go based on:
// https://github.com/carlescere/scheduler
package chronos
import (
"sync"
"time"
)
type Job struct {
task func() // Task to be scheduled
times, // Times that it can be executed, -1 means no limit
n int // Times that it has been executed
aux auxiliar // Holds the values for following API calls
schedule scheduler // Scheduler to determine when to run the job
quit, // Channel for quitting the scheduled job
skip chan struct{} // Channel for executing the task inmediately
mutex sync.Mutex // Mutex to avoid concurrent executions of the same task
}
// Job construction with task assignment
func Schedule(f func()) *Job {
return &Job{task: f, times: -1, quit: make(chan struct{}, 1),
skip: make(chan struct{}, 1)}
}
// Defining the number of times
func (j *Job) NTimes(n int) *Job {
j.times = n
return j
}
func (j *Job) Once() *Job {
return j.NTimes(1)
}
func (j *Job) Twice() *Job {
return j.NTimes(2)
}
// Defining the period size in units
func (j *Job) Every(times ...int) *Job {
switch len(times) {
case 0:
j.aux.ammount = 1
case 1:
j.aux.ammount = times[0]
default:
panic("Too many arguments in Job.Every()")
}
return j
}
// Defining the period's unit duration
func (j *Job) duration(d time.Duration) *Job {
j.aux.kind = periodicKind
j.aux.unit = d
return j
}
func (j *Job) Nanosecond() *Job {
return j.duration(time.Nanosecond)
}
func (j *Job) Nanoseconds() *Job {
return j.Nanosecond()
}
func (j *Job) Microsecond() *Job {
return j.duration(time.Microsecond)
}
func (j *Job) Microseconds() *Job {
return j.Microsecond()
}
func (j *Job) Millisecond() *Job {
return j.duration(time.Millisecond)
}
func (j *Job) Milliseconds() *Job {
return j.Millisecond()
}
func (j *Job) Second() *Job {
return j.duration(time.Second)
}
func (j *Job) Seconds() *Job {
return j.Second()
}
func (j *Job) Minute() *Job {
return j.duration(time.Minute)
}
func (j *Job) Minutes() *Job {
return j.Minute()
}
func (j *Job) Hour() *Job {
return j.duration(time.Hour)
}
func (j *Job) Hours() *Job {
return j.Hour()
}
func (j *Job) Day() *Job {
return j.duration(Day)
}
func (j *Job) Days() *Job {
return j.Day()
}
func (j *Job) Week() *Job {
return j.duration(Week)
}
func (j *Job) Weeks() *Job {
return j.Week()
}
func (j *Job) Month() *Job {
j.aux.kind = monthlyKind
return j
}
func (j *Job) Months() *Job {
return j.Month()
}
func (j *Job) Year() *Job {
j.aux.kind = yearlyKind
return j
}
func (j *Job) Years() *Job {
return j.Year()
}
// Defining if it should run at the start of the cycle
func (j *Job) NotInmediately() *Job {
j.aux.notInmediately = true
return j
}
// Defining the starting and ending times
func (j *Job) At(t time.Time) *Job {
j.aux.start = t
return j
}
func (j *Job) In(d time.Duration) *Job {
return j.At(time.Now().Add(d))
}
func (j *Job) Until(t time.Time) *Job {
j.aux.end = t
return j
}
// Scheduling the task
func (j *Job) Done() (error, chan struct{}, chan struct{}) {
var (
err error
schedule scheduler
)
switch j.aux.kind {
case periodicKind:
schedule, err = newPeriodic(j.aux.start, j.aux.end, j.aux.ammount,
j.aux.unit, j.aux.notInmediately)
case monthlyKind:
schedule, err = newMonthly(j.aux.start, j.aux.end, j.aux.ammount,
j.aux.notInmediately)
case yearlyKind:
schedule, err = newYearly(j.aux.start, j.aux.end, j.aux.ammount,
j.aux.notInmediately)
}
if err == nil {
j.schedule = schedule
go func(j *Job) {
var (
ok bool
next time.Duration
timer *time.Timer
)
for {
ok, next = j.schedule.next()
if !ok {
return
}
timer = time.NewTimer(next)
select {
case <-j.quit:
return
case <-j.skip:
go j.run()
case <-timer.C:
go j.run()
}
}
}(j)
}
return err, j.skip, j.quit
}
func (j *Job) run() {
j.mutex.Lock()
defer j.mutex.Unlock()
if j.times == -1 || j.n < j.times {
j.n++
j.task()
}
}