Closed MaxOvcharov closed 6 years ago
Now I have another question. If I have cluster of two nodes and after that one node is crushed, how can I to reconnect to another node in my current script?
@MaxOvcharov I think you should use some load-balancer like haproxy for this case.
Sorry for the long wait.
To complement what @singulared said, this feature is beyond the scope of both aioamqp and rabbit itself. There are a number of ways of doing this: you could implement a round robin yourself in your consumer program, you could use various DNS tricks, you could use a TCP load balancer, and possibly another dozen solutions and variants.
Cheers
Hi, I have the exact same problem, can you guide me to an example of any of those solutions? The program hangs at the connection close and the exceptions in my own code is not catching it and the logs show that it just hangs inside aioamqp:
2024-02-20 12:27:39,835 ~~INFO ~~__main__ ~~Awaiting RPC requests ~~[processor.py:376]
2024-02-20 12:27:50,768 ~~WARNING ~~aioamqp.protocol ~~Connection lost exc=ConnectionResetError(104, 'Connection reset by peer') ~~[protocol.py:115]
2024-02-20 12:27:50,769 ~~INFO ~~aioamqp.protocol ~~Close connection ~~[protocol.py:314]
The only solution is to kill my component so it starts again.
My code:
while True: #reconnection loop
try:
transport, protocol = await aioamqp.connect(host=o.hostname, port=o.port,
login=o.username, password=o.password,
virtualhost=o.path[1:])
channel = await protocol.channel()
await channel.queue_declare(queue_name=queue_name)
#bind to shared queue
await channel.queue_bind(queue_name=queue_name, exchange_name=exchange, routing_key=queue_name)
#bind to unique queue
await channel.queue_bind(queue_name=queue_name, exchange_name=exchange, routing_key=service_id)
await channel.basic_consume(on_request, queue_name=queue_name)
log.info('Awaiting RPC requests')
#wait till on_close event
await on_close.wait()
log.info('close amqp connection')
await protocol.close()
transport.close()
except (OSError, aioamqp.exceptions.AmqpClosedConnection) as e: #AMQP server not running or starting up
log.error('AMQP connection error: %s', e)
if not max_attempts:
raise ProcessorException('AMPQ error: max attempt reached: %s', e)
log.info('retry in %s seconds', backoff)
await asyncio.sleep(backoff)
max_attempts-=1
backoff*=2
except ConnectionResetError as e:
log.error('Connection reset by broker: %s', e)
log.info('Retrying indefinitely until the broker is up...')
await asyncio.sleep(retry_interval) # Adjust the retry interval as needed
except Exception as e:
log.error('Unexpected exception: %s', e)
import traceback
traceback.print_exc()
else:
log.info("inside break")
break
log.info("after while")
I run my consumer:
How can I restart my current worker, after RabbitMQ server start again. In this version of my consumer code I have to restart it after every server restarts.