def consume(self) -> None: host, port = self.consumers[0].actor.service.address.split(':') while True: if self.stop: break try: queue = RedisSMQ(host=host, port=port, qname=self.consumers[0].topic) if any(consumer.enable_topic_creation for consumer in self.consumers): try: queue.createQueue(delay=0).vt(INFINITE_QUEUE_VISIBILITY).execute() except QueueAlreadyExists: pass try: msg = queue.receiveMessage().exceptions(False).execute() if msg: self.consume_message( key=None, value=msg['message'], headers={} ) except AttributeError: pass queue.quit() except RedisConnectionError: logging.warning('Couldn\'t establish a connection to Redis instance at %s:%s', host, port) time.sleep(1)
def _create_topic(address: str, topic: str, ssl: bool = False): host, port = address.split(':') queue = RedisSMQ(host=host, port=port, qname=topic) try: queue.createQueue(delay=0).vt(INFINITE_QUEUE_VISIBILITY).execute() except QueueAlreadyExists: pass logging.info('Queue %s created', topic) queue.quit()
def _produce(self, key: str, value: str, headers: dict, payload: AsyncProducerPayload) -> None: host, port = self.actor.service.address.split(':') try: queue = RedisSMQ(host=host, port=port, qname=self.topic) if payload.enable_topic_creation: try: queue.createQueue(delay=0).vt(INFINITE_QUEUE_VISIBILITY).execute() except QueueAlreadyExists: pass try: queue.sendMessage(delay=0).message(value).execute() except AttributeError: pass queue.quit() except RedisConnectionError: logging.warning('Couldn\'t establish a connection to Redis instance at %s:%s', host, port) raise
class RedisUtils(): def __init__(self, host): self.conn = RedisSMQ(host=host, qname="accounts") def redis_enqueue(self, msg): self.conn.sendMessage(delay=0).message(msg).execute() def redis_dequeue(self): return self.conn.popMessage().execute()['message'] def redis_queue_length(self): return self.conn.getQueueAttributes().execute()['msgs'] def redis_quit(self): return self.conn.quit() def redis_clear_queue(self): self.conn.deleteQueue().exceptions(False).execute() self.conn.createQueue(delay=0).vt(20).execute()
from rsmq import RedisSMQ # Create controller. # In this case we are specifying the host and default queue name queue = RedisSMQ(host="127.0.0.1", qname="my-queue") # Delete Queue if it already exists, ignoring exceptions queue.deleteQueue().exceptions(False).execute() # Create Queue with default visibility timeout of 20 and delay of 0 # demonstrating here both ways of setting parameters queue.createQueue(delay=0).vt(20).execute() i = 0 try: while True: i += 1 # Send a message with a 2 second delay message_id = queue.sendMessage().message(f"Hello World {i}").execute() pprint({'queue_status': queue.getQueueAttributes().execute()}) time.sleep(0.1) except KeyboardInterrupt: print("ending the work") # No action queue.quit()