-
Notifications
You must be signed in to change notification settings - Fork 104
/
jobqueue.go
172 lines (142 loc) · 3.57 KB
/
jobqueue.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
package jobqueue
import (
"github.com/fireworq/fireworq/jobqueue/logger"
"github.com/fireworq/fireworq/model"
"github.com/rs/zerolog/log"
)
// Impl is an interface of a job queue implementation.
type Impl interface {
Start()
Stop() <-chan struct{}
Push(job IncomingJob) (Job, error)
Pop(limit uint) ([]Job, error)
Delete(job Job)
Update(job Job, next NextInfo)
IsActive() bool
}
// JobQueue is an interface of a job queue.
type JobQueue interface {
Stop() <-chan struct{}
Push(job IncomingJob) (uint64, error)
Pop(limit uint) ([]Job, error)
Complete(job Job, res *Result)
Name() string
IsActive() bool
Node() (*Node, error)
Stats() *Stats
Inspector() (Inspector, bool)
FailureLog() (FailureLog, bool)
}
// Start returns a job queue.
func Start(definition *model.Queue, q Impl) JobQueue {
jq := &jobQueue{
name: definition.Name,
maxWorkers: definition.MaxWorkers,
impl: q,
stats: newStats(),
}
q.Start()
return jq
}
type jobQueue struct {
name string
maxWorkers uint
impl Impl
stats *stats
}
func (q *jobQueue) Name() string {
return q.name
}
func (q *jobQueue) Stop() <-chan struct{} {
return q.impl.Stop()
}
func (q *jobQueue) Push(j IncomingJob) (uint64, error) {
job, err := q.impl.Push(j)
if err != nil {
return 0, err
}
q.stats.push(1)
loggableJob := job.ToLoggable()
logger.Info(q.name, "push", loggableJob, "New job accepted")
return loggableJob.ID(), nil
}
func (q *jobQueue) Pop(limit uint) ([]Job, error) {
results, err := q.impl.Pop(limit)
if err != nil {
return nil, err
}
q.stats.pop(int64(len(results)))
for _, j := range results {
logger.Debug(q.name, "pop", j.ToLoggable(), "A job grabbed")
}
return results, nil
}
func (q *jobQueue) Complete(job Job, res *Result) {
var j *completedJob
if res.IsSuccess() {
j = &completedJob{job, 0}
} else {
j = &completedJob{job, 1}
}
loggable := j.ToLoggable()
if res.IsSuccess() {
logger.Info(q.name, "complete", loggable, res.Message)
q.stats.succeed(1)
q.stats.complete(1)
q.stats.elapsed(logger.Elapsed(loggable))
q.impl.Delete(job)
} else if res.IsPermanentFailure() || !j.canRetry() {
logger.Info(q.name, "complete", loggable, res.Message)
q.stats.fail(1)
q.stats.permanentlyFail(1)
q.stats.complete(1)
q.stats.elapsed(logger.Elapsed(loggable))
if failureLog, ok := q.FailureLog(); ok {
err := failureLog.Add(job, res)
if err != nil {
log.Warn().Msg(err.Error())
}
}
q.impl.Delete(job)
} else {
logger.Info(q.name, "retry", loggable, res.Message)
q.stats.fail(1)
q.impl.Update(job, &nextJob{j})
}
}
func (q *jobQueue) IsActive() bool {
return q.impl.IsActive()
}
func (q *jobQueue) Node() (*Node, error) {
if info, ok := q.impl.(HasNodeInfo); ok {
return info.Node()
}
return nil, nil
}
func (q *jobQueue) Stats() *Stats {
return q.stats.export()
}
func (q *jobQueue) Inspector() (Inspector, bool) {
if hasInspector, ok := q.impl.(HasInspector); ok {
return hasInspector.Inspector(), ok
}
return nil, false
}
func (q *jobQueue) FailureLog() (FailureLog, bool) {
if hasFailureLog, ok := q.impl.(HasFailureLog); ok {
return hasFailureLog.FailureLog(), ok
}
return nil, false
}
// InactiveError is an error returned when Pop() is called on an
// inactive queue.
type InactiveError struct{}
func (e *InactiveError) Error() string {
return "queue is not active"
}
// ConnectionClosedError is an error returned when Pop() is called but
// connection to a remote store has been lost.
type ConnectionClosedError struct{}
func (e *ConnectionClosedError) Error() string {
return "connection has been closed"
}