-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob.go
213 lines (182 loc) · 4.7 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
package jobq
import (
"context"
"errors"
"sync"
"time"
"github.com/google/uuid"
)
// Task is a common interface for tasks that can be executed by a Job.
type Task interface {
Execute(context.Context) (interface{}, error)
}
// JobStatus is the status of a Job.
type JobStatus string
const (
// StatusPending is the status of a Job that has not yet been enqueued.
StatusPending JobStatus = "pending"
// StatusRunning is the status of a Job that is currently running.
StatusRunning JobStatus = "running"
// StatusCompleted is the status of a Job that has completed successfully.
StatusCompleted JobStatus = "completed"
// StatusFailed is the status of a Job that has failed.
StatusFailed JobStatus = "failed"
)
var (
// jobMaxTimeout is the maximum amount of time a Job can run before it is cancelled.
jobMaxTimeout = 10 * time.Minute
// jobStatusMap is a map of Job IDs to JobStatuses.
jobStatusMap = make(map[uuid.UUID]JobStatus)
mapMutex sync.Mutex
)
// Job is a unit of work that can be executed by a Worker.
type Job struct {
ID uuid.UUID
Task Task
Ctx context.Context
Future *Future
QueueOptions interface{}
maxRetries int
retries int
}
// JobOptions are options for creating a new Job.
type JobOptions struct {
ID uuid.UUID
Task Task
Ctx context.Context
MaxRetries int
QueueOptions interface{}
}
// NewJob creates a new Job with the given Task.
func NewJob(opts *JobOptions) (*Job, error) {
// We set maxRetries to 1 if it is less than or equal to 0
// to ensure the task runs at least once.
if opts.MaxRetries <= 0 {
opts.MaxRetries = 1
}
if opts.ID == uuid.Nil {
opts.ID = uuid.New()
}
if opts.Ctx == nil {
opts.Ctx = context.Background()
}
if opts.Task == nil {
return nil, errors.New("Task is required")
}
job := &Job{
Ctx: opts.Ctx,
Task: opts.Task,
Future: NewFuture(),
ID: opts.ID,
QueueOptions: opts.QueueOptions,
maxRetries: opts.MaxRetries,
retries: 0,
}
job.SetStatus(StatusPending)
return job, nil
}
// executeJob executes the Job's Task, returning the result and error.
// If the Job's context is cancelled, the context error is returned.
func (j *Job) executeJob() (interface{}, error) {
done := make(chan bool, 1)
var result interface{}
var err error
go func() {
defer close(done)
result, err = j.Task.Execute(j.Ctx)
}()
select {
case <-j.Ctx.Done():
return nil, j.Ctx.Err()
case <-done:
return result, err
case <-time.After(jobMaxTimeout):
return nil, ErrJobTimeout
}
}
// executeWithRetries executes the Job's Task with retries.
func (j *Job) executeWithRetries() bool {
var err error
for j.retries < j.maxRetries {
result, execErr := j.executeJob()
if execErr != nil {
err = execErr
j.retries++
// TODO: exponential backoff?
} else {
j.Future.SetResult(result, nil)
return true
}
}
j.Future.SetResult(nil, err)
return false
}
// Run executes the Job's Task.
func (j *Job) Run() {
j.SetStatus(StatusRunning)
if j.executeWithRetries() {
j.SetStatus(StatusCompleted)
} else {
j.SetStatus(StatusFailed)
}
}
// Result returns the result of the Job's Task.
func (j *Job) Result() (interface{}, error) {
return j.Future.Result()
}
// Empty returns true if the Job is empty.
func (j *Job) Empty() bool {
return j.Task == nil
}
// SetStatus sets the status of the Job.
func (j *Job) SetStatus(status JobStatus) {
mapMutex.Lock()
jobStatusMap[j.ID] = status
if status == StatusCompleted || status == StatusFailed {
delete(jobStatusMap, j.ID)
}
mapMutex.Unlock()
}
// GetStatus returns the status of the Job.
func (j *Job) GetStatus() JobStatus {
mapMutex.Lock()
status := jobStatusMap[j.ID]
mapMutex.Unlock()
return status
}
// JobQueue is a FIFO queue for Jobs.
type JobQueue struct {
queue Queue
}
// JobQueueOptions are options for creating a new JobQueue.
type JobQueueOptions struct {
Queue Queue
}
// NewJobQueue creates a new JobQueue with the given options.
func NewJobQueue(opts *JobQueueOptions) (*JobQueue, error) {
if opts.Queue == nil {
return nil, errors.New("Queue must be provided")
}
return &JobQueue{queue: opts.Queue}, nil
}
// EnqueueJob creates a new Job from the given Task and adds it to the queue.
func (q *JobQueue) EnqueueJob(opts *JobOptions) (*Future, error) {
job, err := NewJob(opts)
if err != nil {
return nil, err
}
q.queue.Enqueue(job, opts.QueueOptions)
return job.Future, nil
}
// DequeueJob removes a Job from the queue.
func (q *JobQueue) DequeueJob() (*Job, error) {
return q.queue.Dequeue()
}
// Close closes the queue.
func (q *JobQueue) Close() {
q.queue.Close()
}
// Len returns the number of Jobs in the queue.
func (q *JobQueue) Len() int {
return q.queue.Len()
}