-
Notifications
You must be signed in to change notification settings - Fork 0
/
master_server.py
39 lines (29 loc) · 1.21 KB
/
master_server.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
from time import sleep
import pika
from log import tolog
from config import *
class RabbitConsumer(object):
def __init__(self, queue):
self.queue = queue
self.channel = self.connect_host(rmp_host)
def connect_host(self, host):
while True:
try:
self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=host))
channel = self.connection.channel()
channel.queue_declare(exclusive=True)
channel.exchange_declare(exchange='direct', exchange_type='direct')
channel.queue_bind(exchange='direct', queue=self.queue, routing_key='csv')
break
except pika.exceptions.ChannelClosed:
print('Queue {} not exists. Waiting...'.format(self.queue))
tolog('Queue {} not exists. Waiting...'.format(self.queue), 'warn')
self.close_connection()
sleep(10) # Sleep and wait for the client
return channel
def get_channel(self):
return self.channel
def remove_queue(self, queue_name):
self.channel.queue_delete(queue=queue_name)
def close_connection(self):
self.connection.close()