def _threadsafe(self, callback, *args, **kwargs): self._event_set() while True: try: self._sync_lock.acquire() if callback == "basic_publish" and self._blocked: raise MessageBusBlocked('Connection blocked by broker') return getattr(self._ch, callback)(*args, **kwargs) except (SystemExit, KeyboardInterrupt): self.close() raise except pika.exceptions.ConnectionClosedByBroker as err: log.warning(err) self._connect() continue except pika.exceptions.AMQPChannelError as err: log.warning(err) self._connect() continue except pika.exceptions.AMQPConnectionError as err: log.warning(err) self._connect() continue except Exception as err: raise MessageBusError(err) from None finally: self._sync_lock.release()
def send(self, func, msg): if self._rmq is not None: msg = {'type': func, 'msg': msg} self._rmq.distribute(self._queue, **msg) else: raise MessageBusError('MBClient connection closed' + '/ use enter context')
def receiver(self, queue, callback, acks=True, prefetch=1): def callback_wrapper(ch, method, properties, body): message = js.loads(body) try: if callback(self.connection, ch, method, properties, message): if acks: ch.basic_ack(delivery_tag=method.delivery_tag) else: if acks: ch.basic_reject(delivery_tag=method.delivery_tag, requeue=False) except Exception as e: if acks: ch.basic_reject(delivery_tag=method.delivery_tag, requeue=True) log.critical( '%s\n%s\n%s' % (str(e), str(message), str(traceback.format_exc(), ))) try: channel = self.channel() channel.queue_declare(queue=queue, durable=True) channel.basic_qos(prefetch_count=prefetch) channel.basic_consume(queue, callback_wrapper) try: channel.start_consuming() except KeyboardInterrupt: channel.stop_consuming() except Exception as e: raise MessageBusError(e)
def start(self): self._running = True while self.running: try: self._ioloop_lock.acquire() self._sync_lock.acquire() with self._conn._acquire_event_dispatch() as dis_acq: # Check if we can actually process pending events self._conn._flush_output( lambda: bool(self._events), lambda: bool(self.shutdown), lambda: bool(not self.running), lambda: bool( dis_acq and (self._conn._channels_pending_dispatch or self._conn._ready_events))) self._event_clear() if self._conn._ready_events: self._conn._dispatch_connection_events() if self._conn._channels_pending_dispatch: self._conn._dispatch_channel_events() except (SystemExit, KeyboardInterrupt): self.close() raise except pika.exceptions.ConnectionClosedByBroker as err: log.warning(err) self._connect() continue except pika.exceptions.AMQPChannelError as err: log.warning(err) self._connect() continue except pika.exceptions.AMQPConnectionError as err: log.warning(err) self._connect() continue except Exception as err: raise MessageBusError(err) from None finally: try: self._ioloop_lock.release() except RuntimeError: pass try: self._sync_lock.release() except RuntimeError: pass
def distribute(self, queue, **kwargs): retry = 5 for i in range(retry): try: message = js.dumps(kwargs) channel = self.channel() channel.queue_declare(queue=queue, durable=True) channel.basic_publish( exchange='', routing_key=queue, body=message, properties=pika.BasicProperties( delivery_mode=2, # msg persistent content_type='application/json', content_encoding='utf-8')) return True except pika.exceptions.ChannelClosed as e: if i == retry - 1: raise MessageBusError(e) self.connection = pika.BlockingConnection(self._params) except pika.exceptions.ConnectionClosed as e: if i == retry - 1: raise MessageBusError(e) self.connection = pika.BlockingConnection(self._params)
def _threadsafe(self, callback, *args, **kwargs): self._event_set() try: self._sync_lock.acquire() if callback == "basic_publish" and self._blocked: raise MessageBusBlocked('Connection blocked by broker') return getattr(self._ch, callback)(*args, **kwargs) except (SystemExit, KeyboardInterrupt): self.close() raise except Exception as err: raise MessageBusError(err) from None finally: self._sync_lock.release()
def __init__(self, host='127.0.0.1', port=5672, virtualhost='/', username=None, password=None): params = [host, port, virtualhost] if username: params.append(pika.PlainCredentials(username, password)) self._params = pika.ConnectionParameters(*params) try: self.connection = pika.BlockingConnection(self._params) except Exception as e: raise MessageBusError(e) self._channels = {}
def _connect(self): if self._connect_lock.acquire(False): if self.shutdown: raise MessageBusError('Connection closed by client') # If not locked, meaning nobody other calls to connect() try: # Shuffle the connections. shuffle(self._conn_params) self.__conn = pika.BlockingConnection(self._conn_params) self.__ch = self._conn.channel() self.__conn.add_on_connection_blocked_callback( self._blocked_cb) self.__conn.add_on_connection_unblocked_callback( self._unblocked_cb) if self._ch_callback: self._ch_callback(self) finally: self._connect_lock.release() else: # If other call to connect()... lock and wait... self._connect_lock.acquire() self._connect_lock.release()
def _ch(self): if self.shutdown: raise MessageBusError('Connection closed by client') return self.__ch