/
scheduler.go
243 lines (213 loc) · 5.23 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
package dalga
import (
"database/sql"
"fmt"
"log"
"math/rand"
"net/http"
"strings"
"sync"
"time"
"github.com/cenkalti/dalga/dalga/Godeps/_workspace/src/github.com/cenkalti/backoff"
"github.com/cenkalti/dalga/dalga/Godeps/_workspace/src/github.com/go-sql-driver/mysql"
)
type scheduler struct {
table *table
client http.Client
baseURL string
randomizationFactor float64
// to stop scheduler goroutine
stop chan struct{}
// will be closed when scheduler goroutine is stopped
stopped chan struct{}
// to wake up scheduler when a new job is scheduled or cancelled
wakeUp chan struct{}
runningJobs map[JobKey]struct{}
m sync.Mutex
wg sync.WaitGroup
}
func newScheduler(t *table, baseURL string, clientTimeout time.Duration, randomizationFactor float64) *scheduler {
s := &scheduler{
table: t,
baseURL: baseURL,
randomizationFactor: randomizationFactor,
stop: make(chan struct{}),
stopped: make(chan struct{}),
wakeUp: make(chan struct{}, 1),
runningJobs: make(map[JobKey]struct{}),
}
s.client.Timeout = clientTimeout
return s
}
func (s *scheduler) WakeUp(debugMessage string) {
select {
case s.wakeUp <- struct{}{}:
debug("notifying scheduler:", debugMessage)
default:
}
}
func (s *scheduler) NotifyDone() <-chan struct{} {
return s.stopped
}
func (s *scheduler) Stop() {
close(s.stop)
}
func (s *scheduler) Running() int {
s.m.Lock()
defer s.m.Unlock()
return len(s.runningJobs)
}
// Run runs a loop that reads the next Job from the queue and executees it in it's own goroutine.
func (s *scheduler) Run() {
defer close(s.stopped)
for {
debug("---")
var after <-chan time.Time
job, err := s.table.Front()
if err != nil {
if err == sql.ErrNoRows {
debug("no scheduled jobs in the table")
} else if myErr, ok := err.(*mysql.MySQLError); ok && myErr.Number == 1146 {
// Table doesn't exist
log.Fatal(myErr)
} else {
log.Print(err)
time.Sleep(time.Second)
continue
}
} else {
remaining := job.Remaining()
after = time.After(remaining)
debug("next job:", job, "remaining:", remaining)
}
// Sleep until the next job's run time or the webserver's wakes us up.
select {
case <-after:
debug("job sleep time finished")
if err = s.execute(job); err != nil {
log.Print(err)
time.Sleep(time.Second)
}
case <-s.wakeUp:
debug("woken up from sleep by notification")
continue
case <-s.stop:
debug("came quit message")
s.wg.Wait()
return
}
}
}
func randomize(d time.Duration, f float64) time.Duration {
delta := time.Duration(f * float64(d))
return d - delta + time.Duration(float64(2*delta)*rand.Float64())
}
// execute makes a POST request to the endpoint and updates the Job's next run time.
func (s *scheduler) execute(j *Job) error {
debug("execute", *j)
var add time.Duration
if j.OneOff() {
add = s.client.Timeout
} else {
add = j.Interval
if s.randomizationFactor > 0 {
// Add some randomization to periodic tasks.
add = randomize(add, s.randomizationFactor)
}
}
j.NextRun = time.Now().UTC().Add(add)
if err := s.table.UpdateNextRun(j); err != nil {
return err
}
s.wg.Add(1)
go func() {
defer s.wg.Done()
// Do not do multiple POSTs for the same job at the same time.
s.m.Lock()
if _, ok := s.runningJobs[j.JobKey]; ok {
debug("job is already running", j.JobKey)
s.m.Unlock()
return
}
s.runningJobs[j.JobKey] = struct{}{}
s.m.Unlock()
defer func() {
s.m.Lock()
delete(s.runningJobs, j.JobKey)
s.m.Unlock()
}()
code := s.retryPostJob(j)
if code == 0 {
return
}
if j.OneOff() {
debug("deleting one-off job")
s.retryDeleteJob(j)
s.WakeUp("deleted one-off job")
return
}
if code == 204 {
debug("deleting not found job")
if err := s.deleteJob(j); err != nil {
log.Print(err)
return
}
s.WakeUp("deleted not found job")
return
}
}()
return nil
}
func (s *scheduler) postJob(j *Job) (code int, err error) {
url := s.baseURL + j.Path
debug("POSTing to ", url)
resp, err := s.client.Post(url, "text/plain", strings.NewReader(j.Body))
if err != nil {
return
}
switch resp.StatusCode {
case 200, 204:
code = resp.StatusCode
default:
err = fmt.Errorf("endpoint error: %d", resp.StatusCode)
}
return
}
func (s *scheduler) retryPostJob(j *Job) interface{} {
b := backoff.NewExponentialBackOff()
b.MaxElapsedTime = 0 // retry forever
if j.Interval > 0 {
b.MaxInterval = j.Interval
}
f := func() (interface{}, error) { return s.postJob(j) }
return retry(b, f, s.stop)
}
func (s *scheduler) retryDeleteJob(j *Job) {
b := backoff.NewConstantBackOff(time.Second)
f := func() (interface{}, error) { return nil, s.deleteJob(j) }
retry(b, f, nil)
}
func (s *scheduler) deleteJob(j *Job) error {
err := s.table.Delete(j.Path, j.Body)
if err == ErrNotExist {
return nil
}
return err
}
func retry(b backoff.BackOff, f func() (result interface{}, err error), stop chan struct{}) (result interface{}) {
ticker := backoff.NewTicker(b)
defer ticker.Stop()
for {
select {
case <-ticker.C:
var err error
if result, err = f(); err != nil {
log.Print(err)
continue
}
return
case <-stop:
return
}
}
}