-
Notifications
You must be signed in to change notification settings - Fork 916
/
eager.go
201 lines (165 loc) · 5.16 KB
/
eager.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
package backends
import (
"bytes"
"encoding/json"
"fmt"
"github.com/RichardKnop/machinery/v1/tasks"
)
// ErrGroupNotFound ...
type ErrGroupNotFound struct {
groupUUID string
}
// NewErrGroupNotFound returns new instance of ErrGroupNotFound
func NewErrGroupNotFound(groupUUID string) ErrGroupNotFound {
return ErrGroupNotFound{groupUUID: groupUUID}
}
// Error implements error interface
func (e ErrGroupNotFound) Error() string {
return fmt.Sprintf("Group not found: %v", e.groupUUID)
}
// ErrTasknotFound ...
type ErrTasknotFound struct {
taskUUID string
}
// NewErrTasknotFound returns new instance of ErrTasknotFound
func NewErrTasknotFound(taskUUID string) ErrTasknotFound {
return ErrTasknotFound{taskUUID: taskUUID}
}
// Error implements error interface
func (e ErrTasknotFound) Error() string {
return fmt.Sprintf("Task not found: %v", e.taskUUID)
}
// EagerBackend represents an "eager" in-memory result backend
type EagerBackend struct {
groups map[string][]string
tasks map[string][]byte
}
// NewEagerBackend creates EagerBackend instance
func NewEagerBackend() Interface {
return &EagerBackend{
groups: make(map[string][]string),
tasks: make(map[string][]byte),
}
}
// InitGroup creates and saves a group meta data object
func (b *EagerBackend) InitGroup(groupUUID string, taskUUIDs []string) error {
tasks := make([]string, 0, len(taskUUIDs))
// copy every task
for _, v := range taskUUIDs {
tasks = append(tasks, v)
}
b.groups[groupUUID] = tasks
return nil
}
// GroupCompleted returns true if all tasks in a group finished
func (b *EagerBackend) GroupCompleted(groupUUID string, groupTaskCount int) (bool, error) {
tasks, ok := b.groups[groupUUID]
if !ok {
return false, NewErrGroupNotFound(groupUUID)
}
var countSuccessTasks = 0
for _, v := range tasks {
t, err := b.GetState(v)
if err != nil {
return false, err
}
if t.IsCompleted() {
countSuccessTasks++
}
}
return countSuccessTasks == groupTaskCount, nil
}
// GroupTaskStates returns states of all tasks in the group
func (b *EagerBackend) GroupTaskStates(groupUUID string, groupTaskCount int) ([]*tasks.TaskState, error) {
taskUUIDs, ok := b.groups[groupUUID]
if !ok {
return nil, NewErrGroupNotFound(groupUUID)
}
ret := make([]*tasks.TaskState, 0, groupTaskCount)
for _, taskUUID := range taskUUIDs {
t, err := b.GetState(taskUUID)
if err != nil {
return nil, err
}
ret = append(ret, t)
}
return ret, nil
}
// 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 *EagerBackend) TriggerChord(groupUUID string) (bool, error) {
return true, nil
}
// SetStatePending updates task state to PENDING
func (b *EagerBackend) SetStatePending(signature *tasks.Signature) error {
state := tasks.NewPendingTaskState(signature)
return b.updateState(state)
}
// SetStateReceived updates task state to RECEIVED
func (b *EagerBackend) SetStateReceived(signature *tasks.Signature) error {
state := tasks.NewReceivedTaskState(signature)
return b.updateState(state)
}
// SetStateStarted updates task state to STARTED
func (b *EagerBackend) SetStateStarted(signature *tasks.Signature) error {
state := tasks.NewStartedTaskState(signature)
return b.updateState(state)
}
// SetStateRetry updates task state to RETRY
func (b *EagerBackend) SetStateRetry(signature *tasks.Signature) error {
state := tasks.NewRetryTaskState(signature)
return b.updateState(state)
}
// SetStateSuccess updates task state to SUCCESS
func (b *EagerBackend) SetStateSuccess(signature *tasks.Signature, results []*tasks.TaskResult) error {
state := tasks.NewSuccessTaskState(signature, results)
return b.updateState(state)
}
// SetStateFailure updates task state to FAILURE
func (b *EagerBackend) SetStateFailure(signature *tasks.Signature, err string) error {
state := tasks.NewFailureTaskState(signature, err)
return b.updateState(state)
}
// GetState returns the latest task state
func (b *EagerBackend) GetState(taskUUID string) (*tasks.TaskState, error) {
tasktStateBytes, ok := b.tasks[taskUUID]
if !ok {
return nil, NewErrTasknotFound(taskUUID)
}
state := new(tasks.TaskState)
decoder := json.NewDecoder(bytes.NewReader(tasktStateBytes))
decoder.UseNumber()
if err := decoder.Decode(state); err != nil {
return nil, fmt.Errorf("Failed to unmarshal task state %v", b)
}
return state, nil
}
// PurgeState deletes stored task state
func (b *EagerBackend) PurgeState(taskUUID string) error {
_, ok := b.tasks[taskUUID]
if !ok {
return NewErrTasknotFound(taskUUID)
}
delete(b.tasks, taskUUID)
return nil
}
// PurgeGroupMeta deletes stored group meta data
func (b *EagerBackend) PurgeGroupMeta(groupUUID string) error {
_, ok := b.groups[groupUUID]
if !ok {
return NewErrGroupNotFound(groupUUID)
}
delete(b.groups, groupUUID)
return nil
}
func (b *EagerBackend) updateState(s *tasks.TaskState) error {
// simulate the behavior of json marshal/unmarshal
msg, err := json.Marshal(s)
if err != nil {
return fmt.Errorf("Marshal task state error: %v", err)
}
b.tasks[s.TaskUUID] = msg
return nil
}