forked from gocelery/gocelery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgocelery.go
154 lines (136 loc) · 3.79 KB
/
gocelery.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
package gocelery
import (
"fmt"
"time"
)
// CeleryClient provides API for sending celery tasks
type CeleryClient struct {
broker CeleryBroker
backend CeleryBackend
worker *CeleryWorker
}
// CeleryBroker is interface for celery broker database
type CeleryBroker interface {
SendCeleryMessage(*CeleryMessage) error
GetTaskMessage() (*TaskMessage, error) // must be non-blocking
}
// CeleryBackend is interface for celery backend database
type CeleryBackend interface {
GetResult(string) (*ResultMessage, error) // must be non-blocking
SetResult(taskID string, result *ResultMessage) error
}
// NewCeleryClient creates new celery client
func NewCeleryClient(broker CeleryBroker, backend CeleryBackend, numWorkers int) (*CeleryClient, error) {
return &CeleryClient{
broker,
backend,
NewCeleryWorker(broker, backend, numWorkers),
}, nil
}
// Register task
func (cc *CeleryClient) Register(name string, task interface{}) {
cc.worker.Register(name, task)
}
// StartWorker starts celery workers
func (cc *CeleryClient) StartWorker() {
cc.worker.StartWorker()
}
// StopWorker stops celery workers
func (cc *CeleryClient) StopWorker() {
cc.worker.StopWorker()
}
// Delay gets asynchronous result
func (cc *CeleryClient) Delay(task string, args ...interface{}) (*AsyncResult, error) {
celeryTask := getTaskMessage(task)
celeryTask.Args = args
return cc.delay(celeryTask)
}
// DelayKwargs gets asynchronous results with argument map
func (cc *CeleryClient) DelayKwargs(task string, args map[string]interface{}) (*AsyncResult, error) {
celeryTask := getTaskMessage(task)
celeryTask.Kwargs = args
return cc.delay(celeryTask)
}
func (cc *CeleryClient) delay(task *TaskMessage) (*AsyncResult, error) {
defer releaseTaskMessage(task)
encodedMessage, err := task.Encode()
if err != nil {
return nil, err
}
celeryMessage := getCeleryMessage(encodedMessage)
defer releaseCeleryMessage(celeryMessage)
err = cc.broker.SendCeleryMessage(celeryMessage)
if err != nil {
return nil, err
}
return &AsyncResult{
taskID: task.ID,
backend: cc.backend,
}, nil
}
// CeleryTask is an interface that represents actual task
// Passing CeleryTask interface instead of function pointer
// avoids reflection and may have performance gain.
// ResultMessage must be obtained using GetResultMessage()
type CeleryTask interface {
// ParseKwargs - define a method to parse kwargs
ParseKwargs(map[string]interface{}) error
// RunTask - define a method to run
RunTask() (interface{}, error)
}
// AsyncResult is pending result
type AsyncResult struct {
taskID string
backend CeleryBackend
result *ResultMessage
}
// Get gets actual result from redis
// It blocks for period of time set by timeout and return error if unavailable
func (ar *AsyncResult) Get(timeout time.Duration) (interface{}, error) {
ticker := time.NewTicker(50 * time.Millisecond)
timeoutChan := time.After(timeout)
for {
select {
case <-timeoutChan:
err := fmt.Errorf("%v timeout getting result for %s", timeout, ar.taskID)
return nil, err
case <-ticker.C:
val, err := ar.AsyncGet()
if err != nil {
continue
}
return val, nil
}
}
}
// AsyncGet gets actual result from redis and returns nil if not available
func (ar *AsyncResult) AsyncGet() (interface{}, error) {
if ar.result != nil {
return ar.result.Result, nil
}
// process
val, err := ar.backend.GetResult(ar.taskID)
if err != nil {
return nil, err
}
if val == nil {
return nil, err
}
if val.Status != "SUCCESS" {
return nil, fmt.Errorf("error response status %v", val)
}
ar.result = val
return val.Result, nil
}
// Ready checks if actual result is ready
func (ar *AsyncResult) Ready() (bool, error) {
if ar.result != nil {
return true, nil
}
val, err := ar.backend.GetResult(ar.taskID)
if err != nil {
return false, err
}
ar.result = val
return (val != nil), nil
}