-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathtask_queue_mgr.go
More file actions
232 lines (191 loc) · 6.04 KB
/
task_queue_mgr.go
File metadata and controls
232 lines (191 loc) · 6.04 KB
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
package celeriac
import (
"encoding/json"
"time"
// Package dependencies
"crypto/tls"
amqp "github.com/rabbitmq/amqp091-go"
log "github.com/sirupsen/logrus"
"strings"
)
/*
TaskQueueMgr defines a manager for interacting with a Celery task queue
*/
type TaskQueueMgr struct {
brokerURI string
connection *amqp.Connection
channel *amqp.Channel
Monitor *TaskMonitor
errorChannel chan *amqp.Error
closed bool
}
/*
NewTaskQueueMgr is a factory function that creates a new instance of the TaskQueueMgr
*/
func NewTaskQueueMgr(brokerURI string) (*TaskQueueMgr, error) {
self := &TaskQueueMgr{
brokerURI: brokerURI,
errorChannel: make(chan *amqp.Error),
}
err := self.connect()
if err != nil {
return nil, err
}
// Setup broker reconnection monitor
go self.brokerReconnector()
return self, nil
}
func (taskQueueMgr *TaskQueueMgr) connect() error {
for {
var err error
// Connect to the task queue
if strings.HasPrefix(taskQueueMgr.brokerURI, "amqps") {
tlsConfig := &tls.Config{}
//tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12}
tlsConfig.InsecureSkipVerify = true
taskQueueMgr.connection, err = amqp.DialTLS(taskQueueMgr.brokerURI, tlsConfig)
} else {
taskQueueMgr.connection, err = amqp.Dial(taskQueueMgr.brokerURI)
}
if err != nil {
log.Errorf("Failed to connect to AMQP queue: %v. Retrying...", err)
time.Sleep(1000 * time.Millisecond)
} else {
taskQueueMgr.errorChannel = make(chan *amqp.Error)
// Be informed when the connection is closed so we can reconnect automatically
taskQueueMgr.connection.NotifyClose(taskQueueMgr.errorChannel)
log.Printf("Established AMQP connection, getting Channel")
taskQueueMgr.channel, err = taskQueueMgr.connection.Channel()
if err != nil {
log.Errorf("Failed to open AMQP channel: %v", err)
return err
}
// Create the task monitor
// Currently the monitor has one queue for all events
taskQueueMgr.Monitor, err = NewTaskMonitor(taskQueueMgr.connection,
taskQueueMgr.channel,
ConstEventsMonitorExchangeName,
ConstEventsMonitorExchangeType,
ConstEventsMonitorQueueName,
ConstEventsMonitorBindingKey,
ConstEventsMonitorConsumerTag)
if err != nil {
log.Errorf("%s", err)
return err
}
return nil
}
}
}
func (taskQueueMgr *TaskQueueMgr) brokerReconnector() {
for {
err := <-taskQueueMgr.errorChannel
if !taskQueueMgr.closed {
log.Errorf("Connection closed. Reconnecting... (%v)", err)
taskQueueMgr.connect()
}
}
}
/*
Close performs appropriate cleanup of any open task queue connections
*/
func (taskQueueMgr *TaskQueueMgr) Close() {
taskQueueMgr.closed = true
// Stop monitoring
taskQueueMgr.Monitor.Shutdown()
// Close connections
if taskQueueMgr.connection != nil {
taskQueueMgr.connection.Close()
}
}
/*
publish publishes data onto an AMQP channel via the specified exchange name and routing key
*/
func (taskQueueMgr *TaskQueueMgr) publish(data interface{}, exchangeName string, routingKey string) error {
// Non-blocking channel where if there is no error its simply ignored
select {
case err := <-taskQueueMgr.errorChannel:
if err != nil {
taskQueueMgr.connect()
}
default:
}
bodyData, err := json.Marshal(data)
if err != nil {
return err
}
msg := amqp.Publishing{
DeliveryMode: amqp.Persistent,
Timestamp: time.Now(),
ContentType: ConstPublishTaskContentType,
ContentEncoding: ConstPublishTaskContentEncoding,
Body: bodyData,
}
return taskQueueMgr.channel.Publish(exchangeName, routingKey, false, false, msg)
}
/*
DispatchTask places a new task on the Celery task queue
Creates a new Task based on the supplied task name and data
*/
func (taskQueueMgr *TaskQueueMgr) DispatchTask(taskName string, taskData map[string]interface{}, routingKey string) (*Task, error) {
var err error
task, err := taskQueueMgr.DispatchTaskWithID("", taskName, taskData, routingKey)
return task, err
}
/*
DispatchTaskWithID places a new task with the specified ID on the Celery task queue
Creates a new Task based on the supplied task name and data
*/
func (taskQueueMgr *TaskQueueMgr) DispatchTaskWithID(taskID string, taskName string, taskData map[string]interface{}, routingKey string) (*Task, error) {
var err error
task, err := NewTaskWithID(taskID, taskName, nil, taskData)
if err != nil {
log.Fatalf("Failed to create task: %v", err)
panic(err)
}
if len(routingKey) == 0 || routingKey == "" {
routingKey = ConstTaskDefaultRoutingKey
}
err = taskQueueMgr.publish(task, ConstTaskDefaultExchangeName, routingKey)
log.Infof("Dispatched task [NAME]: %s, [ID]: %s to task queue with [ROUTING KEY]: %s", taskName, task.ID, routingKey)
return task, err
}
/*
RevokeTask attempts to notify Celery workers that the specified task needs revoking
*/
func (taskQueueMgr *TaskQueueMgr) RevokeTask(taskID string) error {
if taskID == "" || len(taskID) == 0 {
return ErrInvalidTaskID
}
log.Infof("Revoking task [ID]: %s", taskID)
rt := NewRevokeTaskCmd(taskID, true)
return taskQueueMgr.publish(rt, ConstTaskControlExchangeName, ConstTaskDefaultRoutingKey)
}
/*
Ping attempts to ping Celery workers
*/
func (taskQueueMgr *TaskQueueMgr) Ping() error {
log.Infof("Sending ping to workers")
rt := NewPingCmd()
return taskQueueMgr.publish(rt, ConstTaskControlExchangeName, ConstTaskDefaultRoutingKey)
}
/*
RateLimitTask attempts to set rate limit tasks by type
*/
func (taskQueueMgr *TaskQueueMgr) RateLimitTask(taskName string, rateLimit string) error {
if taskName == "" || len(taskName) == 0 {
return ErrInvalidTaskName
}
rt := NewRateLimitTaskCmd(taskName, rateLimit)
return taskQueueMgr.publish(rt, ConstTaskControlExchangeName, ConstTaskDefaultRoutingKey)
}
/*
TimeLimitTask attempts to set time limits for task by type
*/
func (taskQueueMgr *TaskQueueMgr) TimeLimitTask(taskName string, hardLimit string, softLimit string) error {
if taskName == "" || len(taskName) == 0 {
return ErrInvalidTaskName
}
rt := NewTimeLimitTaskCmd(taskName, hardLimit, softLimit)
return taskQueueMgr.publish(rt, ConstTaskControlExchangeName, ConstTaskDefaultRoutingKey)
}