Exemplo n.º 1
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)
Exemplo n.º 2
0
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')
Exemplo n.º 3
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')
Exemplo n.º 4
0
    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)