-
Notifications
You must be signed in to change notification settings - Fork 1
/
job.go
373 lines (327 loc) · 8.68 KB
/
job.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package crong
import (
"context"
"errors"
"log/slog"
"sync"
"sync/atomic"
"time"
)
type ScheduleState int64
const (
ScheduleStarted ScheduleState = iota + 1
ScheduleSuspended
ScheduleStopped
)
type ScheduledJobOptions struct {
// MaxConcurrent is the maximum number of concurrent job executions.
// If 0, there is no limit
MaxConcurrent int
// TickerReceiveTimeout is the maximum time the job's ticker will
// wait for the job to receive a tick on the Ticker.C channel
TickerReceiveTimeout time.Duration
// MaxFailures is the maximum number of times the job can fail
// before it is stopped. 0=no limit
MaxFailures int
// MaxConsecutiveFailures is the maximum number of consecutive
// times the job can fail before it is stopped. 0=no limit
MaxConsecutiveFailures int
}
func (s ScheduledJobOptions) LogValue() slog.Value {
return slog.GroupValue(
slog.Int("max_concurrent", s.MaxConcurrent),
slog.Int("max_failures", s.MaxFailures),
slog.Int("max_consecutive_failures", s.MaxConsecutiveFailures),
slog.Duration("ticker_receive_timeout", s.TickerReceiveTimeout),
)
}
// ScheduledJob is a function that runs on Ticker ticks
// for a Schedule
type ScheduledJob struct {
schedule *Schedule
ticker *Ticker
f func(t time.Time) error
runtimes []*JobRuntime
mu sync.RWMutex
stopCh chan struct{}
// Failures is the number of times the job has failed
Failures atomic.Int64
// ConsecutiveFailures is the number of times the job has failed in a row
ConsecutiveFailures atomic.Int64
// Runs is the number of times the job has run
Runs atomic.Int64
// Running is the number of times the job is currently running
Running atomic.Int64
state atomic.Int64
previouslyStarted atomic.Bool
startMu sync.Mutex
options ScheduledJobOptions
}
func NewScheduledJob(
schedule *Schedule,
opts ScheduledJobOptions,
f func(t time.Time) error,
) *ScheduledJob {
job := &ScheduledJob{
schedule: schedule,
ticker: NewTicker(
context.Background(),
schedule,
opts.TickerReceiveTimeout,
),
f: f,
runtimes: make([]*JobRuntime, 0),
stopCh: make(chan struct{}, 1),
options: opts,
}
return job
}
func (s ScheduledJob) LogValue() slog.Value {
return slog.GroupValue(
slog.String("schedule", s.schedule.String()),
slog.Group(
"options", slog.Int("max_concurrent", s.options.MaxConcurrent),
slog.Int("max_failures", s.options.MaxFailures),
slog.Int(
"max_consecutive_failures",
s.options.MaxConsecutiveFailures,
),
slog.Duration(
"ticker_receive_timeout",
s.options.TickerReceiveTimeout,
),
),
slog.Int64("failures", s.Failures.Load()),
slog.Int64("consecutive_failures", s.ConsecutiveFailures.Load()),
slog.Int64("runs", s.Runs.Load()),
slog.Int64("running", s.Running.Load()),
)
}
// ScheduleFunc creates and starts a new ScheduledJob with the given schedule and options.
// It immediately begins executing the provided function according to the schedule.
//
// The function f will be called with the current time whenever the schedule is triggered.
// If f returns an error, it will be recorded in the job's runtime history.
//
// Parameters:
// - ctx: A context.Context for cancellation and timeout control.
// - schedule: A *Schedule that determines when the job should run.
// - opts: ScheduledJobOptions to configure the job's behavior.
// - f: A function to be executed on each scheduled tick, with the signature func(time.Time) error.
//
// Returns:
// - *ScheduledJob: A pointer to the newly created and started ScheduledJob.
//
// The returned ScheduledJob is already running and does not need to be started manually.
// Use the returned ScheduledJob's methods (e.g., Stop, Suspend, Resume) to control its execution.
//
// Example:
//
// schedule, _ := crong.New("*/5 * * * *", nil)
// job := crong.ScheduleFunc(ctx, schedule, crong.ScheduledJobOptions{
// MaxConcurrent: 1,
// TickerReceiveTimeout: 5 * time.Second,
// }, func(t time.Time) error {
// fmt.Printf("Job ran at %v\n", t)
// return nil
// })
//
// // ... later
// job.Stop(context.Background())
func ScheduleFunc(
ctx context.Context,
schedule *Schedule,
opts ScheduledJobOptions,
f func(t time.Time) error,
) *ScheduledJob {
s := &ScheduledJob{
schedule: schedule,
ticker: NewTicker(ctx, schedule, opts.TickerReceiveTimeout),
f: f,
runtimes: make([]*JobRuntime, 0),
stopCh: make(chan struct{}, 1),
state: atomic.Int64{},
previouslyStarted: atomic.Bool{},
options: opts,
}
s.state.Store(int64(ScheduleStarted))
s.previouslyStarted.Store(true)
go func() {
_ = s.start(ctx)
}()
return s
}
func (s *ScheduledJob) Start(ctx context.Context) error {
if ScheduleState(s.state.Load()) == ScheduleStopped {
return errors.New("cannot start a job that has been stopped")
}
if s.previouslyStarted.Load() {
return errors.New("job has already been started")
}
return s.start(ctx)
}
// Stop stops job execution. After Stop is called, the job cannot be
// restarted.
func (s *ScheduledJob) Stop(ctx context.Context) bool {
select {
case <-ctx.Done():
case s.stopCh <- struct{}{}:
//
}
old := s.state.Swap(int64(ScheduleStopped))
if old == int64(ScheduleStopped) {
return false
}
return true
}
// Suspend pauses job execution until Resume is called
func (s *ScheduledJob) Suspend() bool {
return s.state.CompareAndSwap(
int64(ScheduleStarted),
int64(ScheduleSuspended),
)
}
// Resume resumes job execution after a call to Suspend
func (s *ScheduledJob) Resume() bool {
return s.state.CompareAndSwap(
int64(ScheduleSuspended),
int64(ScheduleStarted),
)
}
// Runtimes returns a slice of the job's runtimes
func (s *ScheduledJob) Runtimes() []*JobRuntime {
s.mu.RLock()
defer s.mu.RUnlock()
return s.runtimes[:]
}
func (s *ScheduledJob) State() ScheduleState {
return ScheduleState(s.state.Load())
}
// Start starts the job. If the job has already been started,
// it returns an error. If the job has been stopped, it returns an error.
func (s *ScheduledJob) start(ctx context.Context) error {
s.mu.Lock()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
s.state.Store(int64(ScheduleStarted))
defer s.ticker.Stop()
s.previouslyStarted.Store(true)
s.mu.Unlock()
wg := sync.WaitGroup{}
// Waits for a stop signal, then cancels the context
wg.Add(1)
go func() {
defer s.state.Store(int64(ScheduleStopped))
defer wg.Done()
select {
case <-ctx.Done():
return
case <-s.stopCh:
cancel()
return
}
}()
var jobCh chan time.Time
if s.options.MaxConcurrent > 0 {
jobCh = make(chan time.Time)
defer close(jobCh)
for i := 0; i < s.options.MaxConcurrent; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case rt := <-jobCh:
s.execute(rt)
}
}
}()
}
}
// Waits for ticks on the Ticker.C channel, then
// executes the job
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case rt := <-s.ticker.C:
switch {
case ScheduleState(s.state.Load()) == ScheduleSuspended:
Logger.Debug(
"execution suspended, skipping tick",
"scheduled_job", s,
"tick", rt,
)
case jobCh == nil:
wg.Add(1)
go func() {
defer wg.Done()
s.execute(rt)
}()
default:
jobCh <- rt
}
}
}
}()
wg.Wait()
return nil
}
func (s *ScheduledJob) execute(rt time.Time) {
s.Runs.Add(1)
s.Running.Add(1)
defer s.Running.Add(-1)
s.mu.Lock()
defer s.mu.Unlock()
runtime := &JobRuntime{Start: rt}
Logger.Info("running scheduled job", "scheduled_job", s)
runtime.Error = s.f(rt)
if runtime.Error == nil {
s.ConsecutiveFailures.Store(0)
} else {
failures := s.Failures.Add(1)
consecutiveFailures := s.ConsecutiveFailures.Add(1)
if s.options.MaxFailures > 0 && failures >= int64(s.options.MaxFailures) {
Logger.Warn(
"max failures reached, stopping job",
"scheduled_job", s,
)
select {
case s.stopCh <- struct{}{}:
default:
}
} else if s.options.MaxConsecutiveFailures > 0 &&
consecutiveFailures >= int64(s.options.MaxConsecutiveFailures) {
Logger.Warn(
"max consecutive failures reached, stopping job",
"scheduled_job", s,
)
select {
case s.stopCh <- struct{}{}:
default:
}
}
}
runtime.End = time.Now()
Logger.Info(
"job finished",
"start", runtime.Start,
"end", runtime.End,
"scheduled_job", s,
)
s.runtimes = append(s.runtimes, runtime)
}
// JobRuntime is a record of a job's runtime and any error
type JobRuntime struct {
// Start is the time the job started
Start time.Time
// End is the time the job ended
End time.Time
// Error is any error that occurred during the job
Error error
}