-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.go
257 lines (217 loc) · 5.58 KB
/
jobs.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
package celerity
import (
"bytes"
"encoding/gob"
"sync"
"time"
)
// Transport the currently active job transport
var transport = newLocalTransport()
/*
Job is an interface that can be implemented to run work asyncronously using
the celerity job pool.
Impementing a job
To create a Job that can be executed via the Job pool it must impement the Run
function.
Run() error
The Job itself must be registered with the Job pool prior to being used.
celerity.RegisterJob(&MyJob{})
A simple example
The following is a simplified example of a running a Job.
type MyJob struct {}
func (j MyJob) Run() error {
return nil
}
celerity.RegisterJob(&MyJob{})
job := MyJob{}
celerity.Run(MyJob)
As a general pattern it is a good idea to put all your Jobs in a separate
package and register each one when you bootstrap the celerity server.
*/
type Job interface {
Run() error
}
// RegisterJob registers a new job for the job pool. Jobs must be registered
// before they can be added to the queue.
func RegisterJob(job Job) {
gob.Register(job)
}
// RunNow will queue a job to run immediately. It will be added to the queue and
// scheduled to run when the next worker is available.
func RunNow(job Job) {
ji := jobInstance{
Job: job,
StartAt: time.Now(),
RunCount: 1,
}
transport.Run(ji)
}
// RunAt will run the job at a specifc time. The job will be executed when a
// worker is available after the time has past.
func RunAt(job Job, t time.Time) {
ji := jobInstance{
Job: job,
StartAt: t,
RunCount: 1,
}
transport.Run(ji)
}
// RunLater schedules a job to execute after a specified amount of time. The job
// will be executed when a worker is available and the duration has ellapsed.
func RunLater(job Job, d time.Duration) {
ji := jobInstance{
Job: job,
StartAt: time.Now().Add(d),
RunCount: 1,
}
transport.Run(ji)
}
// JobInstance is a instance of a job scheduled to run.
type jobInstance struct {
StartAt time.Time
RunCount int
Interval time.Duration
Job Job
}
// ShouldRun checks if a JobInstance should run now.
func (ji *jobInstance) ShouldRun() bool {
return ji.StartAt.Before(time.Now())
}
// Tick sets the JobInstance up to for the next run
func (ji *jobInstance) Tick() {
if ji.RunCount == 1 {
return
}
ji.RunCount--
ji.StartAt = time.Now().Add(ji.Interval)
}
// JobPool manages the pool of available jobs to process
type jobPool struct {
WorkerCount int
PoolSize int
jobs chan Job
waitgroup sync.WaitGroup
}
// NewJobPool creates a new JobPool based on the passed configuration.
func newJobPool(workerCount, poolSize int) *jobPool {
pool := &jobPool{
WorkerCount: workerCount,
jobs: make(chan Job, poolSize),
}
pool.Start()
return pool
}
// WaitForAll waits for all jobs to be completed.
func (jp *jobPool) WaitForAll() {
jp.waitgroup.Wait()
}
// Worker executes a pending job
func (jp *jobPool) worker() {
for job := range jp.jobs {
job.Run()
jp.waitgroup.Done()
}
}
// Queue pends a job for execution
func (jp *jobPool) Queue(job Job) {
jp.jobs <- job
jp.waitgroup.Add(1)
}
// Start starts the workers for the pool
func (jp *jobPool) Start() {
for n := 0; n < jp.WorkerCount; n++ {
go jp.worker()
}
}
// JobManager Manages the execution and scheduling of jobs.
type jobManager struct {
Pool *jobPool
ScheduledJobs []jobInstance
scheduleTicker *time.Ticker
scheduleQuitChannel chan struct{}
}
// NewJobManager creates a new job manager
func newJobManager() *jobManager {
mgr := &jobManager{
Pool: newJobPool(3, 100),
ScheduledJobs: []jobInstance{},
scheduleQuitChannel: make(chan struct{}),
}
mgr.StartScheduler()
return mgr
}
// StartScheduler starts the schedule ticker to watch for scheduled jobs.
func (jm *jobManager) StartScheduler() {
jm.scheduleTicker = time.NewTicker(1 * time.Minute)
go func() {
for {
select {
case <-jm.scheduleTicker.C:
jm.CheckSchedule()
case <-jm.scheduleQuitChannel:
jm.scheduleTicker.Stop()
return
}
}
}()
}
// CheckSchedule checks if any scheduled jobs should be queued for processing.
func (jm *jobManager) CheckSchedule() {
for n := 0; n < len(jm.ScheduledJobs); n++ {
if time.Now().After(jm.ScheduledJobs[n].StartAt) {
continue
}
jm.Pool.Queue(jm.ScheduledJobs[n].Job)
if jm.ScheduledJobs[n].RunCount == 1 {
jm.ScheduledJobs = append(jm.ScheduledJobs[:n], jm.ScheduledJobs[n+1:]...)
continue
} else {
jm.ScheduledJobs[n].Tick()
}
}
}
func encodeJob(job Job) ([]byte, error) {
var buffer bytes.Buffer
enc := gob.NewEncoder(&buffer)
err := enc.Encode(&job)
if err != nil {
return []byte{}, err
}
return buffer.Bytes(), nil
}
func decodeJob(data []byte) (Job, error) {
buffer := bytes.NewBuffer(data)
dec := gob.NewDecoder(buffer)
var job Job
err := dec.Decode(&job)
if err != nil {
return nil, err
}
return job, nil
}
// JobTransport is used to connect to the JobManager by default it is setup to
// connect to a internal job manager.
type JobTransport interface {
Run(job jobInstance)
}
// LocalTransport local job manager transport
type localTransport struct {
JobManager *jobManager
}
// NewLocalTransport sets up a new local job transport
func newLocalTransport() *localTransport {
return &localTransport{
JobManager: newJobManager(),
}
}
// Run schedules a job to run
func (lt *localTransport) Run(job jobInstance) {
if job.ShouldRun() {
lt.JobManager.Pool.Queue(job.Job)
if job.RunCount == 1 {
lt.JobManager.ScheduledJobs = append(lt.JobManager.ScheduledJobs, job)
return
}
}
lt.JobManager.ScheduledJobs = append(lt.JobManager.ScheduledJobs, job)
}