-
Notifications
You must be signed in to change notification settings - Fork 21
/
simple-job-scheduler.go
306 lines (252 loc) · 7.69 KB
/
simple-job-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
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
package main
//
// Defines a simple job scheduler that runs one (ideally short) job at a time.
// Each job has a current or future RunAt time when it will be run.
//
// Users of this implement Job, JobGetter, and JobRunner
//
import (
"fmt"
"github.com/beeker1121/goque"
"github.com/pkg/errors"
"log"
"sort"
"sync"
"time"
)
const (
JobSuccess = JobStatus("success")
JobReady = JobStatus("ready")
JobError = JobStatus("error")
intervalSeconds = 5
)
type JobStatus string
// ==========================================================
// Job, JobGetter and JobRunner are all implemented by the user of this library
// ==========================================================
// Job interface is implemented by the user to represent the job he wants us to run.
type Job interface {
GetId() uint64
SetId(id uint64)
GetStatus() JobStatus
SetStatus(status JobStatus)
GetRunAt() time.Time
}
// JobGetter is implemented by the user to retrieve the Job instance from a goque.Item.
// This is less than idea becuase the user shouldn't know we are using goque.
type JobGetter func(item *goque.Item) (Job, error)
// JobRunner is implemented by the user to run their job
type JobRunner func(job Job) (JobStatus, error)
//
// Jobs is a sortable slice of Job instances
//
type Jobs []Job
func (slice Jobs) Len() int {
return len(slice)
}
func (slice Jobs) Less(i, j int) bool {
return slice[i].GetRunAt().Before(slice[j].GetRunAt())
}
func (slice Jobs) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
//
// JobScheduler manages the jobs that need to be run
//
type JobScheduler struct {
Name string
getter JobGetter
runner JobRunner
readyJobs Jobs
mutex sync.Mutex // control access to readyJobs
}
func NewJobScheduler(name string, getter JobGetter, runner JobRunner) *JobScheduler {
job := &JobScheduler{Name: name, getter: getter, runner: runner}
err := job.initialize()
if err != nil {
log.Printf("Error initializing JobScheduler %s: %v", name, err)
return nil
}
return job
}
// initialize adds any persistent jobs so they can be run
func (js *JobScheduler) initialize() error {
js.mutex.Lock()
defer js.mutex.Unlock()
// Collect and sort all persisted "ready" jobs
q, err := goque.OpenQueue(js.Name)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Error opening queue %s", js.Name))
}
defer q.Close()
// Get each persisted job and add it to the readyJobs field
for i := uint64(0); i < q.Length(); i++ {
item, err := q.PeekByOffset(i)
if err != nil {
return errors.Wrap(err, "Error in inititialize calling PeekByOffset")
}
job, err := js.getter(item)
if err != nil {
return errors.Wrap(err, "Error in inititialize calling jobGetter")
}
if job.GetStatus() == JobReady {
job.SetId(item.ID)
js.readyJobs = append(js.readyJobs, job)
} else {
log.Println("Initialize is ignoring job:", job)
}
}
// This goroutine regularly runs the available jobs one by one
go func() {
log.Println("Started goroutine")
for {
time.Sleep(time.Duration(intervalSeconds) * time.Second)
log.Println("Woke up...")
if len(js.readyJobs) > 0 {
js.checkJobs()
}
}
}()
return nil
}
// AddJob persists a new job to be run
func (js *JobScheduler) AddJob(job Job) error {
js.mutex.Lock()
defer js.mutex.Unlock() // Persist the job
q, err := goque.OpenQueue(js.Name)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Error opening queue %s", js.Name))
}
defer q.Close()
// Set the initial state before we persist it
job.SetStatus(JobReady)
item, err := q.EnqueueObject(job)
if err != nil {
return errors.Wrap(err, "Error adding job to work queue")
}
job.SetId(item.ID)
// Add job to our in-memory list and select the next job to run
js.readyJobs = append(js.readyJobs, job)
log.Println("job added: ", item.ID, job)
return nil
}
// checkJobs is called every minute (or so) to see if there is any work to do
func (js *JobScheduler) checkJobs() {
// Lock for the duration of this method
js.mutex.Lock()
defer js.mutex.Unlock()
log.Printf("Checking %d job(s)\n", len(js.readyJobs))
if len(js.readyJobs) == 0 {
return
}
// Sort the jobs by ascending RunAt time
sort.Sort(js.readyJobs)
// Loop through each job in the list
for _, job := range js.readyJobs {
// Run the job only if it is time to run
if job.GetRunAt().Before(time.Now()) {
// Run the job
jobStatus, err := js.runner(job)
if err != nil {
log.Printf("Error running jobId %d in queue %s %v\n", job.GetId(), js.Name, err)
jobStatus = JobError
}
// Remove the first one (the one we just ran)
js.readyJobs = js.readyJobs[1:]
// Update persistent version of job with new status
job.SetStatus(jobStatus)
err = updateJob(js.Name, job)
if err != nil {
log.Println("Error updating jobId %d in queue %s %v\n", job.GetId(), js.Name, err)
return
}
} else {
log.Printf("JobId %d is not ready to run until %v\n", job.GetId(), job.GetRunAt())
}
}
}
// updateJob puts the new job state back into the database
func updateJob(queueName string, job Job) error {
q, err := goque.OpenQueue(queueName)
if err != nil {
return errors.Wrap(err, "Error opening queue:")
}
defer q.Close()
_, err = q.UpdateObject(job.GetId(), job)
if err != nil {
return errors.Wrap(err, "Error updating job in queue:")
}
log.Println("Updated job with new status:", job)
return nil
}
// ===========================================================================
//
// Everything above is the "library" and not to be modified by the user
// Everything below is the user implementation
//
// ===========================================================================
func main() {
js := NewJobScheduler("sleep", SleepJobGetter, SleepJobRunner)
err := js.AddJob(&SleepJob{SleepSeconds: 3, Status: JobReady, RunAt: time.Now().Add(time.Duration(20) * time.Second)})
checkErr(err)
time.Sleep(time.Duration(5) * time.Second)
err = js.AddJob(&SleepJob{SleepSeconds: 7, Status: JobReady, RunAt: time.Now()})
checkErr(err)
time.Sleep(time.Duration(2) * time.Second)
err = js.AddJob(&SleepJob{SleepSeconds: 1})
checkErr(err)
time.Sleep(time.Duration(30) * time.Second)
log.Println("Done")
}
func checkErr(err error) {
if err != nil {
log.Fatalln("Error adding Job:", err)
}
}
// SleepJob requires public fields and public getters/setters.
// The methods on SleepJob implement the Job interface
type SleepJob struct {
Id uint64
Status JobStatus
RunAt time.Time
SleepSeconds int64
}
func (my *SleepJob) GetId() uint64 {
return my.Id
}
func (my *SleepJob) SetId(id uint64) {
my.Id = id
}
func (my *SleepJob) GetStatus() JobStatus {
return my.Status
}
func (my *SleepJob) SetStatus(status JobStatus) {
my.Status = status
}
func (my *SleepJob) GetRunAt() time.Time {
return my.RunAt
}
// SleepJobGetter decodes the SleepJob from the goque Item value.
// This is less than idea becuase the user shouldn't know we are using goque.
// It's really just boilerplate code to specify the type of job stored in the queue
// The gob package requires the exact type when decoding
func SleepJobGetter(item *goque.Item) (Job, error) {
var job SleepJob
err := item.ToObject(&job)
if err != nil {
return &job, errors.Wrap(err, "Error getting job from item")
}
return &job, nil
}
// SleepJobRunner runs the sleep job
func SleepJobRunner(job Job) (JobStatus, error) {
// Ensure it's really a SleepJob
sleepJob, ok := job.(*SleepJob)
if !ok {
return JobError, errors.New("Not running a SleepJob")
}
log.Printf("SleepJob %d is sleeping for %d seconds", sleepJob.Id, sleepJob.SleepSeconds)
time.Sleep(time.Duration(sleepJob.SleepSeconds) * time.Second)
log.Printf("Job %d sleeping for %d seconds", sleepJob.Id, sleepJob.SleepSeconds)
return JobSuccess, nil
}