/
scheduler.go
237 lines (188 loc) · 4.79 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
package scheduler
import (
"context"
"log"
"time"
"github.com/function61/gokit/logex"
"github.com/robfig/cron/v3"
)
type JobLastRun struct {
Started time.Time
Finished time.Time
Error string
}
type JobFn func(ctx context.Context, logger *log.Logger) error
type Job struct {
Spec JobSpec
Run JobFn
Schedule cron.Schedule
}
var cronParser = cron.NewParser(cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)
func ValidateSpec(spec JobSpec) (cron.Schedule, error) {
return cronParser.Parse(spec.Schedule)
}
func NewJob(spec JobSpec, run JobFn, now time.Time) (*Job, error) {
schedule, err := ValidateSpec(spec)
if err != nil {
return nil, err
}
if spec.NextRun.IsZero() {
spec.NextRun = schedule.Next(now)
}
return &Job{
Spec: spec,
Run: run,
Schedule: schedule,
}, nil
}
type JobSpec struct {
Id string
Description string
NextRun time.Time
Running bool
Schedule string
LastRun *JobLastRun
}
type snapshotRequest struct {
result chan []JobSpec
}
type jobResult struct {
job *Job
run *JobLastRun
}
type Controller struct {
snapshotRequest chan *snapshotRequest
triggerRequest chan string
jobFinished chan *jobResult
SnapshotReady chan []JobSpec
jobLogger *log.Logger
}
func New(
jobs []*Job,
jobLogger *log.Logger,
start func(func(context.Context) error),
) *Controller {
c := &Controller{
make(chan *snapshotRequest),
make(chan string),
make(chan *jobResult, 1),
make(chan []JobSpec, 2), // TODO: use mailbox pattern from gokit (or eventhorizon)?
jobLogger,
}
start(func(ctx context.Context) error {
return c.run(ctx, jobs)
})
return c
}
func (s *Controller) Trigger(jobId string) {
s.triggerRequest <- jobId
}
// gets an atomic snapshot of scheduler's internal state
func (s *Controller) Snapshot() []JobSpec {
result := make(chan []JobSpec, 1)
s.snapshotRequest <- &snapshotRequest{result}
return <-result
}
// the core of the scheduler runs single-threaded, but many interactions like task running and
// requesting snapshot of job state are in other goroutines and communication happens via channels
func (s *Controller) run(ctx context.Context, jobs []*Job) error {
defer func() {
close(s.SnapshotReady)
}()
nextEarliestCh := func() <-chan time.Time {
if len(jobs) == 0 {
return nil // channel that blocks forever
}
earliest := jobs[0].Spec.NextRun
for _, job := range jobs {
if job.Spec.NextRun.Before(earliest) {
earliest = job.Spec.NextRun
}
}
return time.After(time.Until(earliest))
}
makeSnapshot := func() []JobSpec {
jobCopies := []JobSpec{}
for _, job := range jobs {
jobCopies = append(jobCopies, copyJobSpec(job.Spec))
}
return jobCopies
}
recordJobFinished := func(jr *jobResult) {
jr.job.Spec.LastRun = jr.run
jr.job.Spec.Running = false
s.SnapshotReady <- makeSnapshot()
}
nextJobBecomesRunnableCh := nextEarliestCh()
for {
select {
case now := <-nextJobBecomesRunnableCh:
for _, job := range jobs {
if !job.Spec.NextRun.After(now) {
s.startJob(ctx, job)
}
}
nextJobBecomesRunnableCh = nextEarliestCh()
case snapshotReq := <-s.snapshotRequest:
snapshotReq.result <- makeSnapshot()
case jobResult := <-s.jobFinished:
recordJobFinished(jobResult)
case jobId := <-s.triggerRequest:
for _, job := range jobs {
if job.Spec.Id == jobId {
s.startJob(ctx, job)
break
}
}
case <-ctx.Done():
for _, job := range jobs {
if job.Spec.Running {
// wait for the first of the N running stops to finish - not necessarily
// the "job" variable we have. it's mainly used to count # of unfinished jobs
recordJobFinished(<-s.jobFinished)
}
}
return nil // stops scheduler
}
}
}
func (s *Controller) startJob(ctx context.Context, job *Job) {
job.Spec.NextRun = job.Schedule.Next(job.Spec.NextRun)
jlog := logex.Prefix("scheduler/"+job.Spec.Description, s.jobLogger)
jlogl := logex.Levels(jlog)
if job.Spec.Running {
jlogl.Error.Println("can't start job since previous instance is still running")
return
}
job.Spec.Running = true
jlogl.Info.Println("starting")
go func() {
started := time.Now()
errorStr := ""
if err := job.Run(ctx, jlog); err != nil {
errorStr = err.Error()
}
result := &jobResult{
job: job,
run: &JobLastRun{
Started: started,
Error: errorStr,
Finished: time.Now(),
},
}
duration := result.run.Finished.Sub(result.run.Started)
if errorStr != "" {
jlogl.Error.Printf("in %s: %s", duration, errorStr)
} else {
jlogl.Info.Printf("completed in %s", duration)
}
s.jobFinished <- result
}()
}
func copyJobSpec(copied JobSpec) JobSpec {
if copied.LastRun != nil {
lastRunCopied := *copied.LastRun
copied.LastRun = &lastRunCopied
}
return copied
}