/
QueueWork.py
85 lines (78 loc) · 2.86 KB
/
QueueWork.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
#coding:utf-8
from tools.json_tools import DefJsonEncoder
__author__ = 'amen'
import traceback
import codecs
from kombu import Connection, Exchange
from kombu.messaging import Consumer,Producer
from kombu import Queue
import kombu.serialization
import json
def word_decode(t, coding):
return codecs.decode(t,coding,'ignore')
kombu.serialization._decode=word_decode
connection=None
channel=None
producer=None
task_queue=None
consumer=None
WorkFunction=None
back_exchange=None
def init(host,port,virtual_host,usr,psw,queue_name,exchange_name=None,routing_key=None):
global connection,channel,producer,task_queue,consumer
connection = Connection(hostname=host,port=port,userid=usr,password=psw,virtual_host=virtual_host)
channel = connection.channel()
channel.auto_decode=False
producer=Producer(channel)
if exchange_name:
exchange=Exchange(exchange_name,'topic',channel,durable=True,delivery_mode=2)
task_queue = Queue(queue_name,routing_key=routing_key,durable=True,exchange=exchange)
else:
task_queue = Queue(queue_name,durable=True)
consumer = Consumer(channel,task_queue,no_ack=False)
consumer.qos(prefetch_count=1)
consumer.register_callback(RequestCallBack)
def run():
global connection,channel,producer,task_queue,consumer
try:
consumer.consume()
try:
while True:
connection.drain_events()
except Exception,e:
print e
connection.close()
except BaseException,e:
print traceback.format_exc()
print e
def RequestCallBack(body, message):
properties=message.properties
headers=message.headers
replyheader=None
replybody=None
try:
res=WorkFunction(headers,body,properties.get('reply_to'))
if res is None:
message.ack()
return
replyheader,replybody=res
except Exception,e:
replybody = traceback.format_exc()
replyheader={'error':str(e)}
if 'reply_to' in properties:
if replyheader.get('zip'):
producer.publish(body=replybody,delivery_mode=2,headers=replyheader,
routing_key=properties['reply_to'],
correlation_id=properties.get('correlation_id'),
content_type='application/data',
content_encoding='binary',
exchange=back_exchange)
else:
producer.publish(body=replybody,delivery_mode=2,headers=replyheader,
routing_key=properties['reply_to'],
correlation_id=properties.get('correlation_id'),
compression='gzip',
exchange=back_exchange)
else:
print DefJsonEncoder.encode(replyheader),replybody
message.ack()