forked from svcavallar/celeriac.v1
/
task_queue_mgr.go
199 lines (163 loc) · 5.22 KB
/
task_queue_mgr.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
package celeriac
import (
"encoding/json"
"fmt"
"time"
// Package dependencies
log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
"strings"
"crypto/tls"
)
/*
TaskQueueMgr defines a manager for interacting with a Celery task queue
*/
type TaskQueueMgr struct {
connection *amqp.Connection
channel *amqp.Channel
Monitor *TaskMonitor
}
/*
NewTaskQueueMgr is a factory function that creates a new instance of the TaskQueueMgr
*/
func NewTaskQueueMgr(brokerURI string) (*TaskQueueMgr, error) {
self := &TaskQueueMgr{}
err := self.init(brokerURI)
if err != nil {
return nil, err
}
return self, nil
}
func (taskQueueMgr *TaskQueueMgr) init(brokerURI string) error {
var err error
// Connect to the task queue
if strings.HasPrefix(brokerURI, "amqps") {
tlsConfig := &tls.Config{}
//tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12}
tlsConfig.InsecureSkipVerify = true
taskQueueMgr.connection, err = amqp.DialTLS(brokerURI, tlsConfig)
} else {
taskQueueMgr.connection, err = amqp.Dial(brokerURI)
}
if err != nil {
log.Errorf("Failed to connect to AMQP queue: %v", err)
return err
}
go func() {
fmt.Printf("Closing: %s", <-taskQueueMgr.connection.NotifyClose(make(chan *amqp.Error)))
}()
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
}
/*
Close performs appropriate cleanup of any open task queue connections
*/
func (taskQueueMgr *TaskQueueMgr) Close() {
// 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 {
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)
}