Esempio n. 1
0
    def test_controller_fetches_queue_on_init(self):
        ctrl = Controller()
        self.assertEqual(ctrl.get_queues(), {})

        self._create_queue('test-events')

        ctrl = Controller()
        self.assertEqual(ctrl._queue_cache.keys(), ['test-events'])
Esempio n. 2
0
 def test_get_queue(self):
     ctrl = Controller()
     queue = ctrl.get_queue('events', prefix='test', visibility_timeout=20)
     self.assertEqual(queue.name, 'test-events')
     self.assertEqual(queue.get_timeout(), 20)
     self.assertEqual(queue.message_class, EventMessage)
     self.assertEqual(
         ctrl._queue_cache,
         {'test-events': queue}
     )
Esempio n. 3
0
class Worker(object):

    # Time the consumer is able to sleep if there are no messages.
    # Depends on the real-time factor but keep this value high if possible
    # to reduce load and cost.
    idle_time_seconds = 5

    def __init__(self):
        self.ctrl = Controller()

        # We assume that we have less than 1000 queues here.
        # TODO: ``group`` might make more sense, does it?
        self.pool = Pool(1000)

        self.consumers = {}

    def consume(self, queue_name, prefix=None):
        while True:
            messages = self.ctrl.get_messages(queue_name, prefix, limit=1)
            if messages:
                message = messages[0]

                # Process the message by all consumers. On success
                # acknowledge the message. In case of an error
                # the message is again available on the queue after
                # a short period of time.
                for _, consumer in self.consumers[queue_name]:
                    consumer(self.ctrl, message)
                self.ctrl.ack_message(message, queue_name, prefix)
            else:
                gevent.sleep(self.idle_time_seconds)

    def add_consumer(self, queue_name, consumer, prefix=None):
        # Register a queue first
        self.ctrl.get_queue(queue_name, prefix)
        self.consumers.setdefault(queue_name, []).append((prefix, consumer))

    def run(self):
        self.running = True

        try:
            for queue_name in self.consumers:
                for prefix, consumer in self.consumers[queue_name]:
                    self.pool.spawn(self.consume, queue_name, prefix)

            while self.running:
                # allow fast context switching
                gevent.sleep(self.idle_time_seconds)
        finally:
            self.pool.kill()

    def stop(self):
        self.running = False
        gevent.sleep(self.idle_time_seconds)
        self.pool.kill()
Esempio n. 4
0
 def test_get_and_send_messages(self):
     ctrl = Controller()
     ctrl.send_message('events', 'topic', 'subject')
     self.assertEqual(
         ctrl.get_messages('events')[0].get_body(),
         {
             'topic': 'topic',
             'subject': 'subject',
             'options': {}
         }
     )
Esempio n. 5
0
    def __init__(self):
        self.ctrl = Controller()

        # We assume that we have less than 1000 queues here.
        # TODO: ``group`` might make more sense, does it?
        self.pool = Pool(1000)

        self.consumers = {}
Esempio n. 6
0
    def test_ack_message(self):
        ctrl = Controller()
        ctrl.send_message('events', 'topic', 'subject')

        queue = ctrl.get_queue('events')
        self.assertEqual(queue.count(), 1)

        message = ctrl.get_messages('events')[0]
        ctrl.ack_message(message, 'events')

        self.assertEqual(queue.count(), 0)
Esempio n. 7
0
 def test_get_queues(self):
     ctrl = Controller()
     self.assertEqual(ctrl.get_queues(), {})
     self._create_queue('test')
     self.assertEqual(ctrl.get_queues().keys(), ['test'])