-
Notifications
You must be signed in to change notification settings - Fork 916
/
memcache.go
288 lines (242 loc) · 7.53 KB
/
memcache.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
package backends
import (
"bytes"
"encoding/json"
"time"
"github.com/RichardKnop/machinery/v1/config"
"github.com/RichardKnop/machinery/v1/log"
"github.com/RichardKnop/machinery/v1/tasks"
"github.com/bradfitz/gomemcache/memcache"
)
// MemcacheBackend represents a Memcache result backend
type MemcacheBackend struct {
Backend
servers []string
client *memcache.Client
}
// NewMemcacheBackend creates MemcacheBackend instance
func NewMemcacheBackend(cnf *config.Config, servers []string) Interface {
return &MemcacheBackend{
Backend: New(cnf),
servers: servers,
}
}
// InitGroup creates and saves a group meta data object
func (b *MemcacheBackend) InitGroup(groupUUID string, taskUUIDs []string) error {
groupMeta := &tasks.GroupMeta{
GroupUUID: groupUUID,
TaskUUIDs: taskUUIDs,
}
encoded, err := json.Marshal(&groupMeta)
if err != nil {
return err
}
return b.getClient().Set(&memcache.Item{
Key: groupUUID,
Value: encoded,
Expiration: b.getExpirationTimestamp(),
})
}
// GroupCompleted returns true if all tasks in a group finished
func (b *MemcacheBackend) GroupCompleted(groupUUID string, groupTaskCount int) (bool, error) {
groupMeta, err := b.getGroupMeta(groupUUID)
if err != nil {
return false, err
}
taskStates, err := b.getStates(groupMeta.TaskUUIDs...)
if err != nil {
return false, err
}
var countSuccessTasks = 0
for _, taskState := range taskStates {
if taskState.IsCompleted() {
countSuccessTasks++
}
}
return countSuccessTasks == groupTaskCount, nil
}
// GroupTaskStates returns states of all tasks in the group
func (b *MemcacheBackend) GroupTaskStates(groupUUID string, groupTaskCount int) ([]*tasks.TaskState, error) {
groupMeta, err := b.getGroupMeta(groupUUID)
if err != nil {
return []*tasks.TaskState{}, err
}
return b.getStates(groupMeta.TaskUUIDs...)
}
// TriggerChord flags chord as triggered in the backend storage to make sure
// chord is never trigerred multiple times. Returns a boolean flag to indicate
// whether the worker should trigger chord (true) or no if it has been triggered
// already (false)
func (b *MemcacheBackend) TriggerChord(groupUUID string) (bool, error) {
groupMeta, err := b.getGroupMeta(groupUUID)
if err != nil {
return false, err
}
// Chord has already been triggered, return false (should not trigger again)
if groupMeta.ChordTriggered {
return false, nil
}
// If group meta is locked, wait until it's unlocked
for groupMeta.Lock {
groupMeta, _ = b.getGroupMeta(groupUUID)
log.WARNING.Print("Group meta locked, waiting")
<-time.After(time.Millisecond * 5)
}
// Acquire lock
if err = b.lockGroupMeta(groupMeta); err != nil {
return false, err
}
defer b.unlockGroupMeta(groupMeta)
// Update the group meta data
groupMeta.ChordTriggered = true
encoded, err := json.Marshal(&groupMeta)
if err != nil {
return false, err
}
if err = b.getClient().Replace(&memcache.Item{
Key: groupUUID,
Value: encoded,
Expiration: b.getExpirationTimestamp(),
}); err != nil {
return false, err
}
return true, nil
}
// SetStatePending updates task state to PENDING
func (b *MemcacheBackend) SetStatePending(signature *tasks.Signature) error {
taskState := tasks.NewPendingTaskState(signature)
return b.updateState(taskState)
}
// SetStateReceived updates task state to RECEIVED
func (b *MemcacheBackend) SetStateReceived(signature *tasks.Signature) error {
taskState := tasks.NewReceivedTaskState(signature)
return b.updateState(taskState)
}
// SetStateStarted updates task state to STARTED
func (b *MemcacheBackend) SetStateStarted(signature *tasks.Signature) error {
taskState := tasks.NewStartedTaskState(signature)
return b.updateState(taskState)
}
// SetStateRetry updates task state to RETRY
func (b *MemcacheBackend) SetStateRetry(signature *tasks.Signature) error {
state := tasks.NewRetryTaskState(signature)
return b.updateState(state)
}
// SetStateSuccess updates task state to SUCCESS
func (b *MemcacheBackend) SetStateSuccess(signature *tasks.Signature, results []*tasks.TaskResult) error {
taskState := tasks.NewSuccessTaskState(signature, results)
return b.updateState(taskState)
}
// SetStateFailure updates task state to FAILURE
func (b *MemcacheBackend) SetStateFailure(signature *tasks.Signature, err string) error {
taskState := tasks.NewFailureTaskState(signature, err)
return b.updateState(taskState)
}
// GetState returns the latest task state
func (b *MemcacheBackend) GetState(taskUUID string) (*tasks.TaskState, error) {
item, err := b.getClient().Get(taskUUID)
if err != nil {
return nil, err
}
state := new(tasks.TaskState)
decoder := json.NewDecoder(bytes.NewReader(item.Value))
decoder.UseNumber()
if err := decoder.Decode(state); err != nil {
return nil, err
}
return state, nil
}
// PurgeState deletes stored task state
func (b *MemcacheBackend) PurgeState(taskUUID string) error {
return b.getClient().Delete(taskUUID)
}
// PurgeGroupMeta deletes stored group meta data
func (b *MemcacheBackend) PurgeGroupMeta(groupUUID string) error {
return b.getClient().Delete(groupUUID)
}
// updateState saves current task state
func (b *MemcacheBackend) updateState(taskState *tasks.TaskState) error {
encoded, err := json.Marshal(taskState)
if err != nil {
return err
}
return b.getClient().Set(&memcache.Item{
Key: taskState.TaskUUID,
Value: encoded,
Expiration: b.getExpirationTimestamp(),
})
}
// lockGroupMeta acquires lock on group meta data
func (b *MemcacheBackend) lockGroupMeta(groupMeta *tasks.GroupMeta) error {
groupMeta.Lock = true
encoded, err := json.Marshal(groupMeta)
if err != nil {
return err
}
return b.getClient().Set(&memcache.Item{
Key: groupMeta.GroupUUID,
Value: encoded,
Expiration: b.getExpirationTimestamp(),
})
}
// unlockGroupMeta releases lock on group meta data
func (b *MemcacheBackend) unlockGroupMeta(groupMeta *tasks.GroupMeta) error {
groupMeta.Lock = false
encoded, err := json.Marshal(groupMeta)
if err != nil {
return err
}
return b.getClient().Set(&memcache.Item{
Key: groupMeta.GroupUUID,
Value: encoded,
Expiration: b.getExpirationTimestamp(),
})
}
// getGroupMeta retrieves group meta data, convenience function to avoid repetition
func (b *MemcacheBackend) getGroupMeta(groupUUID string) (*tasks.GroupMeta, error) {
item, err := b.getClient().Get(groupUUID)
if err != nil {
return nil, err
}
groupMeta := new(tasks.GroupMeta)
decoder := json.NewDecoder(bytes.NewReader(item.Value))
decoder.UseNumber()
if err := decoder.Decode(groupMeta); err != nil {
return nil, err
}
return groupMeta, nil
}
// getStates returns multiple task states
func (b *MemcacheBackend) getStates(taskUUIDs ...string) ([]*tasks.TaskState, error) {
states := make([]*tasks.TaskState, len(taskUUIDs))
for i, taskUUID := range taskUUIDs {
item, err := b.getClient().Get(taskUUID)
if err != nil {
return nil, err
}
state := new(tasks.TaskState)
decoder := json.NewDecoder(bytes.NewReader(item.Value))
decoder.UseNumber()
if err := decoder.Decode(state); err != nil {
return nil, err
}
states[i] = state
}
return states, nil
}
// getExpirationTimestamp returns expiration timestamp
func (b *MemcacheBackend) getExpirationTimestamp() int32 {
expiresIn := b.cnf.ResultsExpireIn
if expiresIn == 0 {
// // expire results after 1 hour by default
expiresIn = 3600
}
return int32(time.Now().Unix() + int64(expiresIn))
}
// getClient returns or creates instance of Memcache client
func (b *MemcacheBackend) getClient() *memcache.Client {
if b.client == nil {
b.client = memcache.New(b.servers...)
}
return b.client
}