share rabbitmq conn
This commit is contained in:
parent
9d096e86cb
commit
86d1a8e43c
1 changed files with 8 additions and 6 deletions
|
@ -395,13 +395,12 @@ def rss(token, onstart=False):
|
||||||
rss.write_xml(open(config.rss['file'], 'w'), encoding='utf-8')
|
rss.write_xml(open(config.rss['file'], 'w'), encoding='utf-8')
|
||||||
|
|
||||||
|
|
||||||
def get_rmq_channel():
|
def get_rabbitmq_connection():
|
||||||
credentials = pika.PlainCredentials(
|
credentials = pika.PlainCredentials(
|
||||||
config.rabbitmq['username'], config.rabbitmq['password'])
|
config.rabbitmq['username'], config.rabbitmq['password'])
|
||||||
connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.rabbitmq['host'], port=config.rabbitmq[
|
connection = pika.BlockingConnection(pika.ConnectionParameters(host=config.rabbitmq['host'], port=config.rabbitmq[
|
||||||
'port'], credentials=credentials, virtual_host=config.rabbitmq['vhost']))
|
'port'], credentials=credentials, virtual_host=config.rabbitmq['vhost']))
|
||||||
channel = connection.channel()
|
return connection
|
||||||
return channel
|
|
||||||
|
|
||||||
|
|
||||||
def mail(to_email, subject, message):
|
def mail(to_email, subject, message):
|
||||||
|
@ -411,20 +410,23 @@ def mail(to_email, subject, message):
|
||||||
'subject': subject,
|
'subject': subject,
|
||||||
'content': message
|
'content': message
|
||||||
}
|
}
|
||||||
channel = get_rmq_channel()
|
connection = get_rabbitmq_connection()
|
||||||
|
channel = connection.channel()
|
||||||
channel.basic_publish(exchange=config.rabbitmq['exchange'],
|
channel.basic_publish(exchange=config.rabbitmq['exchange'],
|
||||||
routing_key='mail.command.send',
|
routing_key='mail.command.send',
|
||||||
body=json.dumps(body, indent=False, sort_keys=False))
|
body=json.dumps(body, indent=False, sort_keys=False))
|
||||||
|
connection.close()
|
||||||
logger.debug('Email for %s posted' % to_email)
|
logger.debug('Email for %s posted' % to_email)
|
||||||
#logger.warn('Cannot post email for %s' % to_email)
|
|
||||||
|
|
||||||
|
|
||||||
def send_delete_command(content):
|
def send_delete_command(content):
|
||||||
|
|
||||||
channel = get_rmq_channel()
|
connection = get_rabbitmq_connection()
|
||||||
|
channel = connection.channel()
|
||||||
channel.basic_publish(exchange=config.rabbitmq['exchange'],
|
channel.basic_publish(exchange=config.rabbitmq['exchange'],
|
||||||
routing_key='mail.command.delete',
|
routing_key='mail.command.delete',
|
||||||
body=json.dumps(content, indent=False, sort_keys=False))
|
body=json.dumps(content, indent=False, sort_keys=False))
|
||||||
|
connection.close()
|
||||||
logger.debug('Email accepted. Delete request sent for %s' % content)
|
logger.debug('Email accepted. Delete request sent for %s' % content)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue