-
Notifications
You must be signed in to change notification settings - Fork 21
/
jobmanager.go
196 lines (179 loc) · 5.31 KB
/
jobmanager.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
package jobmanager
import (
"context"
"errors"
"strconv"
"time"
"github.com/cenkalti/dalga/v4/internal/log"
"github.com/cenkalti/dalga/v4/internal/scheduler"
"github.com/cenkalti/dalga/v4/internal/table"
"github.com/senseyeio/duration"
)
const (
orderInsert = "insert-order"
orderNextSched = "next-sched"
)
type JobManager struct {
table *table.Table
scheduler *scheduler.Scheduler
}
type ScheduleOptions struct {
OneOff bool
Immediate bool
FirstRun time.Time
Location *time.Location
Interval duration.Duration
}
var ErrInvalidArgs = errors.New("invalid arguments")
func New(t *table.Table, s *scheduler.Scheduler) *JobManager {
return &JobManager{
table: t,
scheduler: s,
}
}
func (m *JobManager) List(ctx context.Context, path, sortBy string, reverse bool, limit int64) (jobs []table.Job, cursor string, err error) {
var orderByColumn string
switch sortBy {
case orderInsert:
orderByColumn = "id"
case orderNextSched:
orderByColumn = "next_sched, id"
default:
return nil, "", errors.New("invalid sort-by param")
}
jobsWithID, err := m.table.List(ctx, path, orderByColumn, reverse, "", "", limit)
if err != nil {
return nil, "", err
}
jobs = make([]table.Job, 0, len(jobsWithID))
for _, j := range jobsWithID {
jobs = append(jobs, j.Job)
}
if len(jobsWithID) > 0 {
lastItem := jobsWithID[len(jobsWithID)-1]
cursor = generateCursor(lastItem, path, sortBy, reverse, limit)
}
return
}
func (m *JobManager) ListContinue(ctx context.Context, cursor string) (jobs []table.Job, nextCursor string, err error) {
var c Cursor
err = c.Decode(cursor)
if err != nil {
return nil, "", errors.New("invalid cursor")
}
var orderByColumn, greaterThan, lessThan string
switch c.SortBy {
case orderInsert:
orderByColumn = "id"
if c.Reverse {
lessThan = c.LastValue
} else {
greaterThan = c.LastValue
}
case orderNextSched:
orderByColumn = "next_sched, id"
if c.Reverse {
lessThan = "'" + c.LastValue + "'"
} else {
greaterThan = "'" + c.LastValue + "'"
}
default:
return nil, "", errors.New("invalid sort-by param")
}
jobsWithID, err := m.table.List(ctx, c.Path, orderByColumn, c.Reverse, greaterThan, lessThan, c.Limit)
if err != nil {
return nil, "", err
}
jobs = make([]table.Job, 0, len(jobsWithID))
for _, j := range jobsWithID {
jobs = append(jobs, j.Job)
}
if len(jobsWithID) > 0 {
lastItem := jobsWithID[len(jobsWithID)-1]
nextCursor = generateCursor(lastItem, c.Path, c.SortBy, c.Reverse, c.Limit)
}
return
}
func generateCursor(job table.JobWithID, path, sortBy string, reverse bool, limit int64) string {
c := Cursor{
Path: path,
SortBy: sortBy,
Reverse: reverse,
Limit: limit,
}
switch sortBy {
case orderInsert:
c.LastValue = strconv.FormatInt(job.ID, 10)
case orderNextSched:
c.LastValue = job.NextSched.Format(time.RFC3339)
}
return c.Encode()
}
func (m *JobManager) Get(ctx context.Context, path, body string) (*table.Job, error) {
return m.table.Get(ctx, path, body)
}
// Schedule inserts a new job to the table or replaces existing one.
// Returns the created or replaced job.
func (m *JobManager) Schedule(ctx context.Context, path, body string, opt ScheduleOptions) (*table.Job, error) {
key := table.Key{
Path: path,
Body: body,
}
var interval duration.Duration
var delay duration.Duration
var nextRun time.Time
switch {
case opt.OneOff && opt.Immediate: // one-off and immediate
// both first-run and interval params must be zero
if !opt.FirstRun.IsZero() || !opt.Interval.IsZero() {
return nil, ErrInvalidArgs
}
case opt.OneOff && !opt.Immediate: // one-off but later
// only one of from first-run and interval params must be set
if (!opt.FirstRun.IsZero() && !opt.Interval.IsZero()) || (opt.FirstRun.IsZero() && opt.Interval.IsZero()) {
return nil, ErrInvalidArgs
}
if !opt.Interval.IsZero() {
delay = opt.Interval
} else {
nextRun = opt.FirstRun
}
case !opt.OneOff && opt.Immediate: // periodic and immediate
if opt.Interval.IsZero() || !opt.FirstRun.IsZero() {
return nil, ErrInvalidArgs
}
interval = opt.Interval
default: // periodic
if opt.Interval.IsZero() {
return nil, ErrInvalidArgs
}
interval = opt.Interval
if !opt.FirstRun.IsZero() {
nextRun = opt.FirstRun
} else {
delay = opt.Interval
}
}
log.Debugln("job is scheduled:", key.Path, key.Body)
return m.table.AddJob(ctx, key, interval, delay, opt.Location, nextRun)
}
// Disable prevents a job from from running until re-enabled.
// Disabling an I/P job does not cancel its current run.
func (m *JobManager) Disable(ctx context.Context, path, body string) (*table.Job, error) {
log.Debugln("job is disabled:", path, body)
return m.table.DisableJob(ctx, table.Key{Path: path, Body: body})
}
// Enable reschedules a disabled job so that it will run again.
func (m *JobManager) Enable(ctx context.Context, path, body string) (*table.Job, error) {
log.Debugln("job is enabled:", path, body)
return m.table.EnableJob(ctx, table.Key{Path: path, Body: body})
}
// Cancel deletes the job with path and body.
func (m *JobManager) Cancel(ctx context.Context, path, body string) error {
log.Debugln("job is cancelled:", path, body)
return m.table.DeleteJob(ctx, table.Key{Path: path, Body: body})
}
// Running returns the number of running jobs currently.
func (m *JobManager) Running() int {
return m.scheduler.Running()
}