forked from RichardKnop/machinery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
184 lines (154 loc) · 4.71 KB
/
result.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
package backends
import (
"errors"
"reflect"
"time"
"github.com/RichardKnop/machinery/v1/signatures"
"github.com/RichardKnop/machinery/v1/utils"
)
// AsyncResult represents a task result
type AsyncResult struct {
Signature *signatures.TaskSignature
taskState *TaskState
backend Backend
}
// ChordAsyncResult represents a result of a chord
type ChordAsyncResult struct {
groupAsyncResults []*AsyncResult
chordAsyncResult *AsyncResult
backend Backend
}
// ChainAsyncResult represents a result of a chain of tasks
type ChainAsyncResult struct {
asyncResults []*AsyncResult
backend Backend
}
// NewAsyncResult creates AsyncResult instance
func NewAsyncResult(signature *signatures.TaskSignature, backend Backend) *AsyncResult {
return &AsyncResult{
Signature: signature,
taskState: &TaskState{},
backend: backend,
}
}
// NewChordAsyncResult creates ChordAsyncResult instance
func NewChordAsyncResult(groupTasks []*signatures.TaskSignature, chordCallback *signatures.TaskSignature, backend Backend) *ChordAsyncResult {
asyncResults := make([]*AsyncResult, len(groupTasks))
for i, task := range groupTasks {
asyncResults[i] = NewAsyncResult(task, backend)
}
return &ChordAsyncResult{
groupAsyncResults: asyncResults,
chordAsyncResult: NewAsyncResult(chordCallback, backend),
backend: backend,
}
}
// NewChainAsyncResult creates ChainAsyncResult instance
func NewChainAsyncResult(tasks []*signatures.TaskSignature, backend Backend) *ChainAsyncResult {
asyncResults := make([]*AsyncResult, len(tasks))
for i, task := range tasks {
asyncResults[i] = NewAsyncResult(task, backend)
}
return &ChainAsyncResult{
asyncResults: asyncResults,
backend: backend,
}
}
// Get returns task result (synchronous blocking call)
func (asyncResult *AsyncResult) Get() (reflect.Value, error) {
if asyncResult.backend == nil {
return reflect.Value{}, errors.New("Result backend not configured")
}
for {
asyncResult.GetState()
// Purge state if we are using AMQP backend
_, isAMQPBackend := asyncResult.backend.(*AMQPBackend)
if isAMQPBackend && asyncResult.taskState.IsCompleted() {
asyncResult.backend.PurgeState(asyncResult.taskState.TaskUUID)
}
if asyncResult.taskState.IsSuccess() {
return utils.ReflectValue(
asyncResult.taskState.Result.Type,
asyncResult.taskState.Result.Value,
)
}
if asyncResult.taskState.IsFailure() {
return reflect.Value{}, errors.New(asyncResult.taskState.Error)
}
}
}
// GetWithTimeout returns task result limited in time(synchronous blocking call)
func (asyncResult *AsyncResult) GetWithTimeout(timeoutD, sleepD time.Duration) (reflect.Value, error) {
if asyncResult.backend == nil {
return reflect.Value{}, errors.New("Result backend not configured")
}
timeout := time.NewTimer(timeoutD)
for {
select {
case <-timeout.C:
return reflect.Value{}, errors.New("Timeout reached")
default:
asyncResult.GetState()
// Purge state if we are using AMQP backend
_, isAMQPBackend := asyncResult.backend.(*AMQPBackend)
if isAMQPBackend && asyncResult.taskState.IsCompleted() {
asyncResult.backend.PurgeState(asyncResult.taskState.TaskUUID)
}
if asyncResult.taskState.IsSuccess() {
return utils.ReflectValue(
asyncResult.taskState.Result.Type,
asyncResult.taskState.Result.Value,
)
}
if asyncResult.taskState.IsFailure() {
return reflect.Value{}, errors.New(asyncResult.taskState.Error)
}
time.Sleep(sleepD)
}
}
}
// GetState returns latest task state
func (asyncResult *AsyncResult) GetState() *TaskState {
if asyncResult.taskState.IsCompleted() {
return asyncResult.taskState
}
taskState, err := asyncResult.backend.GetState(asyncResult.Signature.UUID)
if err == nil {
asyncResult.taskState = taskState
}
return asyncResult.taskState
}
// Get returns result of a chain of tasks (synchronous blocking call)
func (chainAsyncResult *ChainAsyncResult) Get() (reflect.Value, error) {
if chainAsyncResult.backend == nil {
return reflect.Value{}, errors.New("Result backend not configured")
}
var (
result reflect.Value
err error
)
for _, asyncResult := range chainAsyncResult.asyncResults {
result, err = asyncResult.Get()
if err != nil {
return result, err
}
}
return result, err
}
// Get returns result of a chord (synchronous blocking call)
func (chordAsyncResult *ChordAsyncResult) Get() (reflect.Value, error) {
if chordAsyncResult.backend == nil {
return reflect.Value{}, errors.New("Result backend not configured")
}
var (
result reflect.Value
err error
)
for _, asyncResult := range chordAsyncResult.groupAsyncResults {
result, err = asyncResult.Get()
if err != nil {
return result, err
}
}
return chordAsyncResult.chordAsyncResult.Get()
}