Exemplo n.º 1
0
        def _run(self):
            try:
                conn = chan = None
                conn = pika.BlockingConnection()
                chan = conn.channel()
                chan.exchange_declare('thb_events', 'fanout')
                queue = chan.queue_declare(
                    exclusive=True,
                    auto_delete=True,
                    arguments={'x-message-ttl': 1000},
                )
                chan.queue_bind(queue.method.queue, 'thb_events')

                for method, header, body in chan.consume(queue.method.queue):
                    chan.basic_ack(method.delivery_tag)
                    body = json.loads(body)
                    node, topic = method.routing_key.split(':')

                    if topic == 'speaker':

                        @gevent.spawn
                        def speaker(body=body, node=node):
                            node = node if node != options.node else ''
                            body.insert(0, node)
                            for u in users.values():
                                # u.write(['speaker_msg', [user.account.username, msg]])
                                # u.write(['speaker_msg', ['%s(%s)' % (body[0], options.node), body[1]]])
                                u.write(['speaker_msg', body])

            finally:
                gevent.sleep(1)
                chan and swallow(chan.close)()
                conn and swallow(conn.close)()
Exemplo n.º 2
0
        def _run(self):
            try:
                conn = chan = None
                conn = pika.BlockingConnection()
                chan = conn.channel()
                chan.exchange_declare('thb_events', 'fanout')
                queue = chan.queue_declare(
                    exclusive=True,
                    auto_delete=True,
                    arguments={'x-message-ttl': 1000},
                )
                chan.queue_bind(queue.method.queue, 'thb_events')

                for method, header, body in chan.consume(queue.method.queue):
                    chan.basic_ack(method.delivery_tag)
                    body = json.loads(body)
                    node, topic = method.routing_key.split(':')

                    if topic == 'speaker':
                        @gevent.spawn
                        @log_failure(log)
                        def speaker(body=body, node=node):
                            node = node if node != options.node else ''
                            body.insert(0, node)
                            for u in users.values():
                                # u.write(['speaker_msg', [user.account.username, msg]])
                                # u.write(['speaker_msg', ['%s(%s)' % (body[0], options.node), body[1]]])
                                u.write(['speaker_msg', body])

            finally:
                gevent.sleep(1)
                chan and swallow(chan.close)()
                conn and swallow(conn.close)()
Exemplo n.º 3
0
    def publish(self, key, body):
        try:
            self.chan.basic_publish("thb_events", "%s:%s" % ("forum", key), Endpoint.encode(body))

        except:
            logging.error("error publishing", exc_info=True)
            swallow(self.shutdown)()
            self.connect()
Exemplo n.º 4
0
 def publish(self, key, body):
     try:
         self.chan.basic_publish(
             'thb_events', '%s:%s' % (options.node, key),
             Endpoint.encode(body),
         )
     except:
         log.error('error publishing', exc_info=True)
         swallow(self.shutdown)()
         self.connect()
Exemplo n.º 5
0
    def publish(self, key, body):
        try:
            self.chan.basic_publish(
                'thb_events', '%s:%s' % ('forum', key),
                Endpoint.encode(body),
            )

        except:
            logging.error('error publishing', exc_info=True)
            swallow(self.shutdown)()
            self.connect()
Exemplo n.º 6
0
    def _run(self):
        try:
            self.conn = None
            conn = pika.BlockingConnection(
                pika.connection.ConnectionParameters(
                    host=options.rabbitmq_host,
                    socket_timeout=6.0,
                    heartbeat_interval=2,
                ),
            )
            chan = conn.channel()
            chan.exchange_declare('thb_events', 'fanout')
            queue = chan.queue_declare(
                exclusive=True,
                auto_delete=True,
                arguments={'x-message-ttl': 1000},
            )
            chan.queue_bind(queue.method.queue, 'thb_events')

            def notify(key, message):
                @gevent.spawn
                def _notify():
                    events_history.rotate()
                    events_history[0] = [[key, message], time.time()]
                    [evt.set() for evt in list(event_waiters)]

            for method, header, body in chan.consume(queue.method.queue):
                chan.basic_ack(method.delivery_tag)
                message = json.loads(body)
                key = method.routing_key
                node, topic = key.split(':')

                if topic == 'current_users':
                    # [[node, username, state], ...]
                    current_users[node] = [
                        (node, i[1], i[2]) for i in message
                    ]
                    rst = []
                    map(rst.__iadd__, current_users.values())
                    notify('current_users', rst)

                elif topic == 'shutdown':
                    # not implemented yet
                    current_users[node] = []
                    rst = []
                    map(rst.__iadd__, current_users.values())
                    notify('current_users', rst)

                if topic == 'speaker':
                    # [node, username, content]
                    message.insert(0, node)
                    notify('speaker', message)

        finally:
            self.conn and swallow(conn.close)()
            gevent.sleep(1)
Exemplo n.º 7
0
    def _run(self):
        try:
            self.conn = None
            conn = pika.BlockingConnection(
                pika.connection.ConnectionParameters(host=options.rabbitmq_host, socket_timeout=6.0),
            )
            chan = conn.channel()
            chan.exchange_declare('thb_events', 'fanout')
            queue = chan.queue_declare(
                exclusive=True,
                auto_delete=True,
                arguments={'x-message-ttl': 1000},
            )
            chan.queue_bind(queue.method.queue, 'thb_events')

            def notify(key, message):
                @gevent.spawn
                def _notify():
                    events_history.rotate()
                    events_history[0] = [[key, message], time.time()]
                    encoded = json.dumps([key, message])
                    [evt.set() for evt in list(event_waiters)]

            for method, header, body in chan.consume(queue.method.queue):
                chan.basic_ack(method.delivery_tag)
                message = json.loads(body)
                key = method.routing_key
                node, topic = key.split(':')

                if topic == 'current_users':
                    # [[node, username, state], ...]
                    current_users[node] = [
                        (node, i[1], i[2]) for i in message
                    ]
                    rst = []
                    map(rst.__iadd__, current_users.values())
                    notify('current_users', rst)

                elif topic == 'shutdown':
                    # not implemented yet
                    current_users[node] = []
                    rst = []
                    map(rst.__iadd__, current_users.values())
                    notify('current_users', rst)

                if topic == 'speaker':
                    # [node, username, content]
                    message.insert(0, node)
                    notify('speaker', message)

        finally:
            self.conn and swallow(conn.close)()
            gevent.sleep(1)
Exemplo n.º 8
0
 def shutdown(self):
     self.conn and swallow(self.conn.close)()
Exemplo n.º 9
0
 def shutdown(self):
     self.conn and swallow(self.conn.close)()