mirror of
https://github.com/ansible/awx.git
synced 2026-05-19 23:07:42 -02:30
POC postgres broker
This commit is contained in:
committed by
Ryan Petrello
parent
355fb125cb
commit
558e92806b
@@ -1,10 +1,14 @@
|
||||
import inspect
|
||||
import logging
|
||||
import sys
|
||||
import json
|
||||
from uuid import uuid4
|
||||
import psycopg2
|
||||
|
||||
from django.conf import settings
|
||||
from kombu import Exchange, Producer, Connection, Queue, Consumer
|
||||
from kombu import Exchange, Producer
|
||||
from django.db import connection
|
||||
from pgpubsub import PubSub
|
||||
|
||||
|
||||
logger = logging.getLogger('awx.main.dispatch')
|
||||
@@ -85,26 +89,15 @@ class task:
|
||||
if callable(queue):
|
||||
queue = queue()
|
||||
if not settings.IS_TESTING(sys.argv):
|
||||
with Connection(settings.BROKER_URL, transport_options=settings.BROKER_TRANSPORT_OPTIONS) as conn:
|
||||
exchange = Exchange(queue, type=exchange_type or 'direct')
|
||||
|
||||
# HACK: With Redis as the broker declaring an exchange isn't enough to create the queue
|
||||
# Creating a Consumer _will_ create a queue so that publish will succeed. Note that we
|
||||
# don't call consume() on the consumer so we don't actually eat any messages
|
||||
Consumer(conn, queues=[Queue(queue, exchange, routing_key=queue)], accept=['json'])
|
||||
producer = Producer(conn)
|
||||
logger.debug('publish {}({}, queue={})'.format(
|
||||
cls.name,
|
||||
task_id,
|
||||
queue
|
||||
))
|
||||
producer.publish(obj,
|
||||
serializer='json',
|
||||
compression='bzip2',
|
||||
exchange=exchange,
|
||||
declare=[exchange],
|
||||
delivery_mode="persistent",
|
||||
routing_key=queue)
|
||||
conf = settings.DATABASES['default']
|
||||
conn = psycopg2.connect(dbname=conf['NAME'],
|
||||
host=conf['HOST'],
|
||||
user=conf['USER'],
|
||||
password=conf['PASSWORD'])
|
||||
conn.set_session(autocommit=True)
|
||||
logger.warn(f"Send message to queue {queue}")
|
||||
pubsub = PubSub(conn)
|
||||
pubsub.notify(queue, json.dumps(obj))
|
||||
return (obj, queue)
|
||||
|
||||
# If the object we're wrapping *is* a class (e.g., RunJob), return
|
||||
|
||||
Reference in New Issue
Block a user