/
magi.go
257 lines (234 loc) · 5.55 KB
/
magi.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
package magi
import (
"errors"
"fmt"
"time"
"github.com/evanhuang8/magi/cluster"
"github.com/evanhuang8/magi/job"
"github.com/evanhuang8/magi/lock"
)
// MagiAPIVersion is the current API version
var MagiAPIVersion = "0.1"
// BlockingTimeout is the timeout used for blocking operations
var BlockingTimeout = "5s"
// Magi represents the top level queue application
type Magi struct {
APIVersion string
dqCluster *cluster.DisqueCluster
rCluster *cluster.RedisCluster
processors map[string]*Processor
isProcessing bool
processControl chan string
}
var (
// MagiProcessCommandStop is the command for stopping the processor
MagiProcessCommandStop = "STOP"
)
// Producer creates a Magi instance that acts as a producer
func Producer(config *cluster.DisqueClusterConfig) (*Magi, error) {
dqCluster, err := cluster.NewDisqueCluster(config)
if err != nil {
return nil, err
}
producer := &Magi{
APIVersion: MagiAPIVersion,
dqCluster: dqCluster,
isProcessing: false,
}
return producer, nil
}
// Consumer creates a Magi instance that acts as a consumer
func Consumer(dqConfig *cluster.DisqueClusterConfig, rConfig *cluster.RedisClusterConfig) (*Magi, error) {
dqCluster, err := cluster.NewDisqueCluster(dqConfig)
if err != nil {
return nil, err
}
rCluster := cluster.NewRedisCluster(rConfig)
consumer := &Magi{
APIVersion: MagiAPIVersion,
dqCluster: dqCluster,
rCluster: rCluster,
isProcessing: false,
processors: make(map[string]*Processor),
processControl: make(chan string, 1),
}
return consumer, nil
}
// Close terminates all connections from the Magi instance
func (m *Magi) Close() error {
if m.dqCluster != nil {
err := m.dqCluster.Close()
if err != nil {
return err
}
}
if m.rCluster != nil {
err := m.rCluster.Close()
if err != nil {
return err
}
}
if m.isProcessing {
m.processControl <- MagiProcessCommandStop
}
return nil
}
/**
* Producer methods
*/
// AddJob adds a job to the queue
func (m *Magi) AddJob(queueName string, body string, ETA time.Time, config *cluster.DisqueOpConfig) (*job.Job, error) {
_job, err := job.Add(m.dqCluster, queueName, body, ETA, config)
return _job, err
}
// GetJob tries to get the details about a job
func (m *Magi) GetJob(id string) (*job.Job, error) {
details, err := m.dqCluster.Get(id)
if err != nil {
if err.Error() == "no data available" {
return nil, nil
}
return nil, err
}
_job, err := job.FromDetails(details)
return _job, err
}
// DeleteJob removes the job from the disque cluster
func (m *Magi) DeleteJob(id string) (bool, error) {
err := m.dqCluster.Ack(id)
if err != nil {
return false, err
}
return true, nil
}
/**
* Consumer methods
*/
// Processor is an interface that all job processor should implement
type Processor interface {
Process(*job.Job) (interface{}, error)
ShouldAutoRenew(*job.Job) bool
}
// Register adds a processor for a queue
func (m *Magi) Register(queueName string, processor Processor) {
m.processors[queueName] = &processor
}
// Process starts the job processing procedure
func (m *Magi) Process(queueName string) {
m.isProcessing = true
for {
select {
case command := <-m.processControl:
if command == MagiProcessCommandStop {
return
}
default:
m.dqCluster.Chain()
job, err := m.dqCluster.Fetch(queueName, nil)
if err != nil {
if err.Error() != "no data available" {
fmt.Println("Error:", err)
}
} else {
m.process(queueName, job.ID)
}
m.dqCluster.Unchain()
}
}
}
// IsProcessing returns whether it is currently processing jobs
func (m *Magi) IsProcessing() bool {
return m.isProcessing
}
// ErrDisqueJobWaitFailed is the error for failing to wait on a long processing job
var ErrDisqueJobWaitFailed = errors.New("Disque Error: fail to wait on a job!")
func (m *Magi) process(queueName string, id string) {
var _lock *lock.Lock
// Catch panics
defer func() {
if err := recover(); err != nil {
err, ok := err.(error)
if ok && err.Error() == lock.ErrLockLost.Error() {
// Lock is lost, release remaining lock segments
_lock.Release()
} else {
panic(err)
}
}
}()
// Check if the processor is available
processor, exists := m.processors[queueName]
if !exists {
return
}
// Get job details
_job, err := m.GetJob(id)
if err != nil {
return
}
// Acquire lock
_lock = lock.CreateLock(m.rCluster, id)
result, err := _lock.Get((*processor).ShouldAutoRenew(_job))
// If lock cannot be acquired, return and do not acknowledge
if err != nil {
return
}
if !result {
return
}
// Start the auto wait extension for the job in queue
control := make(chan bool, 1)
_job.IsProcessing = true
go m.autoWait(_job, &control)
// Process the job
(*processor).Process(_job)
// Stop the auto wait extension
_job.IsProcessing = false
control <- true
// Ack the job
err = m.dqCluster.Ack(id)
if err != nil {
return
}
if !result {
return
}
// Release the lock
result, err = _lock.Release()
if err != nil {
return
}
if !result {
return
}
return
}
func (m *Magi) autoWait(job *job.Job, control *chan bool) {
start := time.Now()
for {
select {
case command := <-*control:
if command {
return
}
default:
if !job.IsProcessing {
return
}
// Check if a wait command is needed
elapse := float64(time.Now().Sub(start))
threshold := float64(job.Raw.Retry) * 0.5
if elapse >= threshold {
// Issue wait
err := m.dqCluster.Wait(job.ID)
if err != nil {
fmt.Println(err)
panic(ErrDisqueJobWaitFailed)
}
// Reset ticker
start = time.Now()
}
time.Sleep(time.Millisecond)
}
}
}