class WorkerConfirmMessage(Worker): def __init__(self, *args, **kwargs): super(WorkerConfirmMessage, self).__init__(*args, **kwargs) self.receive_event = AsyncEvent(io_loop=self.event_io_loop) def handle_request(self, channel, method, properties, body): channel.basic_ack(method.delivery_tag) self.receive_event.set()
class Worker(Thread): rabbit_host = 'localhost' def __init__(self, exchange, bindings=[], event_io_loop=None, *args, **kwargs): super(Worker, self).__init__(*args, **kwargs) self.daemon = True self.exchange = exchange self.bindings = bindings self.event_io_loop = event_io_loop self.listening = AsyncEvent(io_loop=self.event_io_loop) def run(self, *args, **kwargs): self.io_loop = IOLoop() self.client = AsyncRabbitConnectionBase(host=self.rabbit_host, io_loop=self.io_loop) self.io_loop.add_callback(stack_context.wrap(self._start_client)) self.io_loop.start() @engine def _start_client(self): logger.info('Declaring worker exchange') yield Task(self.client.exchange_declare, exchange=self.exchange, exchange_type='topic', auto_delete=True) logger.info('Declaring worker queue') self.queue = yield Task(self.client.queue_declare, exclusive=True, auto_delete=True) logger.info('Binding worker keys') for routing_key in self.bindings: yield Task(self.client.queue_bind, queue=self.queue, exchange=self.exchange, routing_key=routing_key) logger.info('Starting worker message consumption') yield Task(self.client.basic_consume, consumer_callback=self._handle_message, queue=self.queue) self.listening.set() def _handle_message(self, channel, method, properties, body): self.handle_request(channel, method, properties, json.loads(body)) def handle_request(self, channel, method, properties, body): # This is an example implementation. Subclasses should override # handle_request # print "got request with cid: %s" % properties.correlation_id props = BasicProperties(correlation_id=properties.correlation_id) reply_body = json.dumps({'my': 'BODY'}) # Note that we don't provide an exchange here because the routing key # is setup as a "direct" key for RPC. channel.basic_publish(exchange='', routing_key=properties.reply_to, properties=props, body=reply_body) # print "sent reply to: %s" % properties.reply_to # Acknowledge the message was received and processed channel.basic_ack(method.delivery_tag)