mosquito / aio-pika

AMQP 0.9 client designed for asyncio and humans.
https://aio-pika.readthedocs.org/
Apache License 2.0
1.18k stars 186 forks source link

How to implemented on_reconnect_callback in new version #515

Closed pdechery closed 1 year ago

pdechery commented 1 year ago

Hi, thanks for maintaining this package.

My project used version 6.3 and called the on_reconnect_callback method inside robust_connect

I've migrated to the new version (8.3.0) and that method is gone. I've been searching how to have the same functionality on the new version but have found nothing so far. The documentation stops mentioning on_reconnect_callback from version 7.0 onwards.

Browsing through the code I found a CallbackCollection object that's called inside RobustConnection. It seems to be made with the purpose I'm looking for but documentation (or CHANGELOG) doesn't provide any information about how to use it.

Can anyone give some guidance about that? Thanks a lot.

mosquito commented 1 year ago

Fell free to use connection.reconnect_callbacks in this case. https://github.com/mosquito/aio-pika/blob/master/aio_pika/robust_connection.py here it's calling.

pdechery commented 1 year ago

Hi, thanks for the reply.

So, should I use the add method in CallbackCollection class and pass any callable as argument?

Like so:

def my_callback():
   pass

connect_robust.reconnect_callback.add(my_callback)
fadighattas100 commented 1 year ago

@pdechery i was implementing the reconnect callback, and it seems you need to pass a sender param to the callback function definition to make it work with aio-pika so it will look like

@staticmethod
def my_callback(sender):
   pass

connect_robust.reconnect_callback.add(my_callback)

or if you need self

def my_callback(self, sender):
   self.do_some_work()
   pass

connect_robust.reconnect_callback.add(my_callback)

i think this issue should be closed, @mosquito