-
Notifications
You must be signed in to change notification settings - Fork 0
/
amqp.py
140 lines (109 loc) · 3.37 KB
/
amqp.py
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
import json
import socket
import time
import pika
import config
import log
def connect():
credentials = pika.PlainCredentials(config.ramq_user, config.ramq_passwd)
params = pika.ConnectionParameters(host=config.ramq_host,
credentials=credentials, heartbeat_interval=0)
connection = pika.BlockingConnection(params)
args = {'x-queue-mode': 'lazy',
'x-max-priority': 2,
}
chan = connection.channel()
chan.queue_declare(queue='master', durable=True, arguments=args)
chan.queue_declare(queue='worker', durable=True, arguments=args)
chan.queue_declare(queue='watcher', durable=True, arguments=args)
return chan
class DirectRT:
def __init__(self, adapter):
self.result = None
self.adapter = adapter
def direct_reply_to(self, body):
self.adapter.channel.basic_consume(self.callback,
queue='amq.rabbitmq.reply-to', no_ack=True)
self.adapter.pub(body, reply=True)
self.adapter.channel.start_consuming()
return self.result
def callback(self, ch, method_frame, properties, body):
if body is not None:
self.result = json.loads(body.decode('utf-8'))
ch.stop_consuming()
def pub(self, body, props):
self.adapter.channel.basic_publish('', body=body, routing_key=props.reply_to)
class Adapter:
def __init__(self, queue):
self.queue = queue
self.__reconnect()
def __reconnect(self):
self.channel = connect()
def __get(self):
method, props, body = self.channel.basic_get(self.queue)
if body is not None:
body = json.loads(body.decode('utf-8'))
return method, props, body
def __pub(self, body, props):
try:
self.channel.basic_publish('', self.queue, body, props)
except pika.exceptions.ConnectionClosed:
self.__reconnect()
self.channel.basic_publish('', self.queue, body, props)
def __consume_forever(self, callback):
self.channel.basic_qos(prefetch_count=1)
self.channel.basic_consume(callback, queue=self.queue)
self.channel.start_consuming()
def get(self):
try:
return self.__get()
except pika.exceptions.ConnectionClosed:
self.__reconnect()
return self.__get()
def pub(self, body, failed=False, reply=False, priority=False):
if failed is True:
body['status'] = 'FAILED'
body = json.dumps(body)
if reply is True:
props = pika.BasicProperties(reply_to='amq.rabbitmq.reply-to')
else:
props = pika.BasicProperties(delivery_mode=2)
if priority is True:
props.priority = 2
self.__pub(body, props)
def ack(self, tag):
try:
self.channel.basic_ack(delivery_tag=tag)
except pika.exceptions.ConnectionClosed:
self.__reconnect()
self.channel.basic_ack(delivery_tag=tag)
def consume_forever(self, callback):
while True:
try:
self.__reconnect()
self.__consume_forever(callback)
except pika.exceptions.ConnectionClosed as e:
log.log('consume_forever: %s' % (e,))
time.sleep(1)
def notify(self, _id, status):
body = {'id': _id,
'status': status,
'hostname': socket.gethostname(),
}
self.pub(body)
def direct_reply_to(self, body):
return DirectRT(self).direct_reply_to(body)
def pub_reply(self, body, props):
body = json.dumps(body)
return DirectRT(self).pub(body, props)
def init():
global master, worker, watcher
master = Adapter('master')
worker = Adapter('worker')
watcher = Adapter('watcher')
def ack(function):
def wrapper(*args, **kw):
queue, tag = function(*args, **kw)
if queue is not None and tag is not None:
queue.ack(tag)
return wrapper