コード例 #1
0
class PgsqlQueueConsumer(AbstractConsumer):
    """Base class for consumers of events sent from PostgreSQL via rabbitmq.

    This class is intended to be used in a completely "stand-alone" manner, such as
    inside a script being run separately as a background job. As such, it also includes
    connecting to rabbitmq, declaring the underlying queue and bindings, and
    (optionally) connecting to the database to be able to fetch and modify data as
    necessary. It relies on the environment variable INI_FILE being set.

    Note that all messages received by these consumers are expected to be in JSON
    format.
    """

    PGSQL_EXCHANGE_NAME = "pgsql_events"

    def __init__(self,
                 queue_name: str,
                 routing_keys: Sequence[str],
                 uses_db: bool = True):
        """Initialize a new queue, bindings, and consumer for it."""
        self.connection = Connection()
        self.channel = self.connection.channel()

        self.channel.queue_declare(queue_name, durable=True, auto_delete=False)

        for routing_key in routing_keys:
            self.channel.queue_bind(queue_name,
                                    exchange=self.PGSQL_EXCHANGE_NAME,
                                    routing_key=routing_key)

        if uses_db:
            self.db_session = get_session_from_config(os.environ["INI_FILE"])
        else:
            self.db_session = None

        super().__init__(self.channel, queue_name)

    def consume_queue(self) -> None:
        """Declare the consumer and consume messages indefinitely."""
        self.declare()
        self.connection.loop()

    @abstractmethod
    def run(self, msg: Message) -> None:
        """Process a delivered message (subclasses must implement)."""
        pass

    def start(self, msg: Message) -> None:
        """Setup/teardown for message-processing (wraps run())."""
        # decode the msg body from JSON
        msg.body = json.loads(msg.body)

        # process the message, will call run()
        super().start(msg)

        # after processing is finished, commit the transaction and ack the msg
        if self.db_session:
            self.db_session.commit()

        msg.ack()
コード例 #2
0
ファイル: mqtt_to_mq.py プロジェクト: imgcre/temple_light
def amqp_reinit():
    global amqp_conn
    global amqp_channel
    dbg('amqp_reinit')
    amqp_conn = Connection(host=MQ_SERVER_IP)  # mqtt broker 1
    amqp_channel = amqp_conn.channel()

    # declare an exchange and queue, and bind the queue to the exchange
    amqp_channel.exchange_declare('mqtt.exchange',
                                  'direct',
                                  durable=True,
                                  auto_delete=False)
    amqp_channel.queue_declare('mqtt.q', durable=True, auto_delete=False)
    amqp_channel.queue_bind('mqtt.q',
                            exchange='mqtt.exchange',
                            routing_key='mqtt.q')

    # device coin check
    amqp_channel.exchange_declare('coincheck.exchange',
                                  'direct',
                                  durable=True,
                                  auto_delete=False)
    amqp_channel.queue_declare('coincheck.q', durable=True, auto_delete=False)
    amqp_channel.queue_bind('coincheck.q',
                            exchange='coincheck.exchange',
                            routing_key='coincheck.q')
コード例 #3
0
    def __init__(
        self,
        queue_name: str,
        routing_keys: Sequence[str],
        uses_db: bool = True,
    ) -> None:
        """Initialize a new queue, bindings, and consumer for it."""
        self.connection = Connection()
        self.channel = self.connection.channel()

        self.channel.queue_declare(queue_name, durable=True, auto_delete=False)

        for routing_key in routing_keys:
            self.channel.queue_bind(
                queue_name,
                exchange=self.PGSQL_EXCHANGE_NAME,
                routing_key=routing_key,
            )

        if uses_db:
            self.db_session = get_session_from_config(os.environ['INI_FILE'])
        else:
            self.db_session = None

        super().__init__(self.channel, queue_name)
コード例 #4
0
ファイル: reviewbot.py プロジェクト: indygreg/reviewbot
    async def get_review_messages(self):
        class Consumer(AbstractConsumer):
            def __init__(self, channel, queue_name, plugin):
                self.plugin = plugin
                super().__init__(channel, queue_name)

            def run(self, message: Message):
                """Dispatch the message to the correct handler."""
                msg = json.loads(message.body)

                if msg['_meta']['routing_key'] == 'mozreview.commits.published':
                    asyncio.ensure_future(self.plugin.handle_review_requested(message), loop=self.plugin.bot.loop)
                elif msg['_meta']['routing_key'] == 'mozreview.review.published':
                    asyncio.ensure_future(self.plugin.handle_reviewed(message), loop=self.plugin.bot.loop)

        conn = Connection(host=self.host, port=self.port, ssl=self.ssl, userid=self.userid, password=self.password,
                virtual_host=self.vhost)
        channel = conn.channel()
        channel.queue_declare(queue=self.queue_name, durable=True, auto_delete=False)
        channel.queue_bind(self.queue_name, exchange=self.exchange_name, routing_key=self.routing_key)
        consumer = Consumer(channel, self.queue_name, self)
        consumer.declare()

        while True:
            if getattr(self.bot, 'protocol', None) and irc_channel in self.bot.channels: break # Check if connected to IRC
            else: await asyncio.sleep(.001, loop=self.bot.loop)

        while True:
            try:
                conn.drain_events(timeout=self.timeout)
            except Timeout:
                await asyncio.sleep(.001, loop=self.bot.loop)
コード例 #5
0
def amqp_reinit():
    global amqp_conn
    global amqp_channel
    amqp_conn = Connection(host = MQ_SERVER_IP) # mqtt broker 1
    amqp_channel = amqp_conn.channel()

    # declare an exchange and queue, and bind the queue to the exchange
    amqp_channel.exchange_declare('mqtt_client_status.e', 'direct', durable = True, auto_delete = False)
    amqp_channel.queue_declare('mqtt_client_status.q', durable = True, auto_delete = False)
    amqp_channel.queue_bind('mqtt_client_status.q', exchange = 'mqtt_client_status.e', routing_key = 'mqtt_client_status.k')
コード例 #6
0
ファイル: reviewbot.py プロジェクト: stephendonner/reviewbot
    async def get_review_messages(self):
        class Consumer(AbstractConsumer):
            def __init__(self, channel, queue_name, plugin):
                self.plugin = plugin
                super().__init__(channel, queue_name)

            def run(self, message: Message):
                """Dispatch the message to the correct handler."""
                msg = json.loads(message.body)

                if msg['_meta'][
                        'routing_key'] == 'mozreview.commits.published':
                    asyncio.ensure_future(
                        self.plugin.handle_review_requested(message),
                        loop=self.plugin.bot.loop)
                elif msg['_meta'][
                        'routing_key'] == 'mozreview.review.published':
                    asyncio.ensure_future(self.plugin.handle_reviewed(message),
                                          loop=self.plugin.bot.loop)

        conn = Connection(host=self.host,
                          port=self.port,
                          ssl=self.ssl,
                          userid=self.userid,
                          password=self.password,
                          virtual_host=self.vhost)
        channel = conn.channel()
        channel.queue_declare(queue=self.queue_name,
                              durable=True,
                              auto_delete=False)
        channel.queue_bind(self.queue_name,
                           exchange=self.exchange_name,
                           routing_key=self.routing_key)
        consumer = Consumer(channel, self.queue_name, self)
        consumer.declare()

        while True:
            if getattr(self.bot, 'protocol',
                       None) and irc_channel in self.bot.channels:
                break  # Check if connected to IRC
            else:
                await asyncio.sleep(.001, loop=self.bot.loop)

        while True:
            try:
                conn.drain_events(timeout=self.timeout)
            except Timeout:
                await asyncio.sleep(.001, loop=self.bot.loop)
コード例 #7
0
class RabbitPipe(GenericPipe):
    def __init__(self, ip_rabbit, port_rabbit, listen_queue_name, callback_func):
        self.listen_queue_name = listen_queue_name
        self.conn = Connection(host=ip_rabbit, port=port_rabbit)
        self.ch = self.conn.channel()
        self.ch.queue_declare(self.listen_queue_name + '.q', durable=True, auto_delete=False)
        self.r_listen = RabbitListener(callback_func, self.ch, self.listen_queue_name + '.q')

    def start(self):
        self.r_listen.declare()
        # wait for events, which will receive delivered messages and call any consumer callbacks
        while True:
            self.conn.drain_events(timeout=None)

    def recover_unacked(self):
        self.ch.basic_recover(requeue=True)

    def purge(self):
        self.ch.queue_purge(self.listen_queue_name)
コード例 #8
0
    def __init__(self, ip_rabbit, port_rabbit, name):
        super().__init__()
        self.name = name
        self.ip = ip_rabbit
        self.port = port_rabbit

        self.conn = Connection(host=ip_rabbit, port=port_rabbit)
        self.ch = self.conn.channel()
        self.ch.exchange_declare(self.name + '.exchange', 'direct', durable=True)
        self.ch.queue_declare(self.name + '.q', durable=True, auto_delete=False)
        self.ch.queue_bind(self.name + '.q', exchange=self.name + '.exchange', routing_key=self.name + '.q')
コード例 #9
0
class RabbitTalker(object):
    def __init__(self, ip_rabbit, port_rabbit, name):
        super().__init__()
        self.name = name
        self.ip = ip_rabbit
        self.port = port_rabbit

        self.conn = Connection(host=ip_rabbit, port=port_rabbit)
        self.ch = self.conn.channel()
        self.ch.exchange_declare(self.name + '.exchange', 'direct', durable=True)
        self.ch.queue_declare(self.name + '.q', durable=True, auto_delete=False)
        self.ch.queue_bind(self.name + '.q', exchange=self.name + '.exchange', routing_key=self.name + '.q')

    def publish_message(self, data):
        self.ch.basic_publish(Message(data), exchange=self.name + '.exchange', routing_key=self.name + '.q')
コード例 #10
0
 def __init__(self, ip_rabbit, port_rabbit, listen_queue_name, callback_func):
     self.listen_queue_name = listen_queue_name
     self.conn = Connection(host=ip_rabbit, port=port_rabbit)
     self.ch = self.conn.channel()
     self.ch.queue_declare(self.listen_queue_name + '.q', durable=True, auto_delete=False)
     self.r_listen = RabbitListener(callback_func, self.ch, self.listen_queue_name + '.q')
コード例 #11
0
import json
from amqpy import Connection, Message, AbstractConsumer, Timeout
conn = Connection()  # connect to guest:guest@localhost:5672 by default
ch = conn.channel()


class Consumer(AbstractConsumer):
    def run(self, msg: Message):
        #print('Received a message: {}'.format(msg.body))
        mesajprimit = json.loads(msg.body)
        print("mesaj primit de la: "+mesajprimit['from'])
        print("mesajul contine:"+mesajprimit['body'])
        msg.ack()

consumer = Consumer(ch, 'test.q')
consumer.declare()

# wait for events, which will receive delivered messages and call any consumer callbacks
while True:
    conn.drain_events(timeout=None)
コード例 #12
0
from amqpy import Connection, Message, AbstractConsumer, Timeout

conn = Connection()  # connect to guest:guest@localhost:5672 by default

ch = conn.channel()

# declare an exchange and queue, and bind the queue to the exchange
ch.exchange_declare('test.exchange', 'direct')
ch.queue_declare('test.q')
ch.queue_bind('test.q', exchange='test.exchange', routing_key='test.q')

# publish a few messages, which will get routed to the queue bound to the routing key "test.q"
ch.basic_publish(Message('hello world 1'), exchange='test.exchange', routing_key='test.q')
ch.basic_publish(Message('hello world 2'), exchange='test.exchange', routing_key='test.q')
ch.basic_publish(Message('hello world 3'), exchange='test.exchange', routing_key='test.q')

# get a message from the queue
msg = ch.basic_get('test.q')

# don't forget to acknowledge it
msg.ack()