2011-09-12 09:25:42 +00:00
|
|
|
from kombu.mixins import ConsumerMixin
|
2012-11-02 14:32:45 +00:00
|
|
|
from kombu.log import get_logger
|
2011-09-12 09:47:22 +00:00
|
|
|
from kombu.utils import kwdict, reprcall
|
2011-09-12 09:25:42 +00:00
|
|
|
|
|
|
|
from queues import task_queues
|
|
|
|
|
2012-11-02 14:32:45 +00:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2011-09-12 09:47:22 +00:00
|
|
|
|
2011-09-12 09:25:42 +00:00
|
|
|
class Worker(ConsumerMixin):
|
|
|
|
|
2011-09-12 09:47:22 +00:00
|
|
|
def __init__(self, connection):
|
|
|
|
self.connection = connection
|
|
|
|
|
2011-09-12 09:25:42 +00:00
|
|
|
def get_consumers(self, Consumer, channel):
|
2011-09-12 09:47:22 +00:00
|
|
|
return [Consumer(queues=task_queues,
|
|
|
|
callbacks=[self.process_task])]
|
2011-09-12 09:25:42 +00:00
|
|
|
|
2011-09-12 09:47:22 +00:00
|
|
|
def process_task(self, body, message):
|
2012-06-24 15:32:17 +00:00
|
|
|
fun = body['fun']
|
|
|
|
args = body['args']
|
|
|
|
kwargs = body['kwargs']
|
2012-11-02 14:32:45 +00:00
|
|
|
logger.info('Got task: %s', reprcall(fun.__name__, args, kwargs))
|
2011-09-12 09:47:22 +00:00
|
|
|
try:
|
|
|
|
fun(*args, **kwdict(kwargs))
|
2012-11-13 13:44:41 +00:00
|
|
|
except Exception, exc:
|
2012-11-02 14:32:45 +00:00
|
|
|
logger.error('task raised exception: %r', exc)
|
2011-09-12 09:25:42 +00:00
|
|
|
message.ack()
|
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from kombu import Connection
|
2011-10-18 10:36:14 +00:00
|
|
|
from kombu.utils.debug import setup_logging
|
2012-06-24 15:32:17 +00:00
|
|
|
setup_logging(loglevel='INFO')
|
2011-09-12 09:25:42 +00:00
|
|
|
|
2012-06-24 15:32:17 +00:00
|
|
|
with Connection('amqp://guest:guest@localhost:5672//') as conn:
|
2011-09-12 11:57:12 +00:00
|
|
|
try:
|
|
|
|
Worker(conn).run()
|
|
|
|
except KeyboardInterrupt:
|
2012-06-24 15:32:17 +00:00
|
|
|
print('bye bye')
|