From 86d1a8e43c29e26ededc36d1402485c156ce9674 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sat, 27 Jan 2018 17:07:07 +0100 Subject: [PATCH] share rabbitmq conn --- app/core/processor.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/core/processor.py b/app/core/processor.py index 7c92dd4..dfa8c15 100644 --- a/app/core/processor.py +++ b/app/core/processor.py @@ -395,13 +395,12 @@ def rss(token, onstart=False): rss.write_xml(open(config.rss['file'], 'w'), encoding='utf-8') -def get_rmq_channel(): +def get_rabbitmq_connection(): credentials = pika.PlainCredentials( config.rabbitmq['username'], config.rabbitmq['password']) connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.rabbitmq['host'], port=config.rabbitmq[ 'port'], credentials=credentials, virtual_host=config.rabbitmq['vhost'])) - channel = connection.channel() - return channel + return connection def mail(to_email, subject, message): @@ -411,20 +410,23 @@ def mail(to_email, subject, message): 'subject': subject, 'content': message } - channel = get_rmq_channel() + connection = get_rabbitmq_connection() + channel = connection.channel() channel.basic_publish(exchange=config.rabbitmq['exchange'], routing_key='mail.command.send', body=json.dumps(body, indent=False, sort_keys=False)) + connection.close() logger.debug('Email for %s posted' % to_email) - #logger.warn('Cannot post email for %s' % to_email) def send_delete_command(content): - channel = get_rmq_channel() + connection = get_rabbitmq_connection() + channel = connection.channel() channel.basic_publish(exchange=config.rabbitmq['exchange'], routing_key='mail.command.delete', body=json.dumps(content, indent=False, sort_keys=False)) + connection.close() logger.debug('Email accepted. Delete request sent for %s' % content)