Ejemplo n.º 1
0
 def startService(self):
     super(Poller, self).startService()
     
     queue = defer.DeferredQueue()
     
     # Init AMQP connection
     kwargs = {
         'config': self.config,
         'queue': queue,
         'delegate': TwistedDelegate(),
         'vhost': self.config.get('amqp', 'vhost'),
         'spec': get_spec(),
     }
     
     self.publisher_factory = PublisherFactory(**kwargs)
     reactor.connectTCP(
         self.config.get('amqp', 'host'),
         int(self.config.get('amqp', 'port')),
         self.publisher_factory
     )
     
     # Init MySQL connection
     kwargs = {
         'host': self.config.get('database', 'host'),
         'port': int(self.config.get('database', 'port')),
         'user': self.config.get('database', 'user'),
         'passwd': self.config.get('database', 'password'),
         'db': self.config.get('database', 'temp'),
         'use_unicode': True
     }
     
     self.sql_connection = yield threads.deferToThread(db.connect, **kwargs)
     
     def poll(cursor, query, rate):
         cursor.execute(query, (rate,))
         for row in cursor.fetchall():
             row = json.dumps(row)
             threads.blockingCallFromThread(reactor, queue.put, row)
     
     self.polling_call = task.LoopingCall(
         threads.deferToThread,
         poll,
         self.sql_connection.cursor(),
         "SELECT * FROM post_updates ORDER BY timestamp ASC LIMIT 0,%d",
         int(self.config.get('polling', 'rate'))
     )
     self.polling_call.start(int(self.config.get('polling', 'interval')))
Ejemplo n.º 2
0
    def startService(self):
        super(Controller, self).startService()

        # Create AMQP connection
        spec = get_spec()
        delegate = txamqp.client.TwistedDelegate()
        vhost = "/"
        host = "localhost"
        port = 5672
        exchange = "bert"
        routing_key = "test"

        conn = yield ClientCreator(
            reactor, txamqp.protocol.AMQClient, delegate=delegate, vhost=vhost, spec=spec
        ).connectTCP(host, port)

        yield conn.authenticate("guest", "guest")
        channel = yield conn.channel(1)

        yield channel.channel_open()
        yield channel.exchange_declare(exchange=exchange, type="fanout", durable=False, auto_delete=False)
        queue = yield channel.queue_declare(queue="cast", durable=False, exclusive=True, auto_delete=True)
        yield channel.queue_bind(queue=queue.queue, exchange="bert")
        tag = yield channel.basic_consume(consumer_tag="functions", queue=queue.queue, no_ack=True)
        queue = yield conn.queue("functions")

        print "\n" * 5

        while True:
            try:
                msg = yield queue.get()
            except txamqp.queue.Closed:
                break

            # print dir(msg)
            # print msg.fields
            # print msg.method
            print repr(msg.content)
            print msg.content.weight()

            print "\n" * 5