|
1 | 1 | import pickle
|
| 2 | +import time |
2 | 3 | import uuid
|
3 | 4 |
|
4 | 5 | try:
|
@@ -61,7 +62,7 @@ def __init__(self, url='amqp://guest:guest@localhost:5672//',
|
61 | 62 | self.exchange_options = exchange_options or {}
|
62 | 63 | self.queue_options = queue_options or {}
|
63 | 64 | self.producer_options = producer_options or {}
|
64 |
| - self.producer = self._producer() |
| 65 | + self.publisher_connection = self._connection() |
65 | 66 |
|
66 | 67 | def initialize(self):
|
67 | 68 | super(KombuManager, self).initialize()
|
@@ -92,31 +93,44 @@ def _queue(self):
|
92 | 93 | options.update(self.queue_options)
|
93 | 94 | return kombu.Queue(queue_name, self._exchange(), **options)
|
94 | 95 |
|
95 |
| - def _producer(self): |
96 |
| - return self._connection().Producer(exchange=self._exchange(), |
97 |
| - **self.producer_options) |
98 |
| - |
99 |
| - def __error_callback(self, exception, interval): |
100 |
| - self._get_logger().exception('Sleeping {}s'.format(interval)) |
| 96 | + def _producer_publish(self, connection): |
| 97 | + producer = connection.Producer(exchange=self._exchange(), |
| 98 | + **self.producer_options) |
| 99 | + return connection.ensure(producer, producer.publish) |
101 | 100 |
|
102 | 101 | def _publish(self, data):
|
103 |
| - connection = self._connection() |
104 |
| - publish = connection.ensure(self.producer, self.producer.publish, |
105 |
| - errback=self.__error_callback) |
106 |
| - publish(pickle.dumps(data)) |
| 102 | + retry = True |
| 103 | + while True: |
| 104 | + try: |
| 105 | + producer_publish = self._producer_publish( |
| 106 | + self.publisher_connection) |
| 107 | + producer_publish(pickle.dumps(data)) |
| 108 | + break |
| 109 | + except (OSError, kombu.exceptions.KombuError): |
| 110 | + if retry: |
| 111 | + self._get_logger().error('Cannot publish to rabbitmq... ' |
| 112 | + 'retrying') |
| 113 | + retry = False |
| 114 | + else: |
| 115 | + self._get_logger().error( |
| 116 | + 'Cannot publish to rabbitmq... giving up') |
| 117 | + break |
107 | 118 |
|
108 | 119 | def _listen(self):
|
109 | 120 | reader_queue = self._queue()
|
110 |
| - |
| 121 | + retry_sleep = 1 |
111 | 122 | while True:
|
112 |
| - connection = self._connection().ensure_connection( |
113 |
| - errback=self.__error_callback) |
114 | 123 | try:
|
115 |
| - with connection.SimpleQueue(reader_queue) as queue: |
116 |
| - while True: |
117 |
| - message = queue.get(block=True) |
118 |
| - message.ack() |
119 |
| - yield message.payload |
120 |
| - except connection.connection_errors: |
121 |
| - self._get_logger().exception("Connection error " |
122 |
| - "while reading from queue") |
| 124 | + with self._connection() as connection: |
| 125 | + with connection.SimpleQueue(reader_queue) as queue: |
| 126 | + while True: |
| 127 | + message = queue.get(block=True) |
| 128 | + message.ack() |
| 129 | + yield message.payload |
| 130 | + retry_sleep = 1 |
| 131 | + except (OSError, kombu.exceptions.KombuError): |
| 132 | + self._get_logger().error( |
| 133 | + 'Cannot receive from rabbitmq... ' |
| 134 | + 'retrying in {} secs'.format(retry_sleep)) |
| 135 | + time.sleep(retry_sleep) |
| 136 | + retry_sleep = min(retry_sleep * 2, 60) |
0 commit comments