@@ -276,7 +276,18 @@ async def setup_async_connection(self):
276
276
except Exception as e :
277
277
self .log .error (f'Oh my lordy lord! I caugth an unexpected exception while trying to connect: { e } ' , exc_info = True )
278
278
279
- async def async_start_consumer (
279
+ @retry (
280
+ retry = retry_if_exception_type ((
281
+ AMQPConnectionError ,
282
+ ChannelClosedByBroker ,
283
+ ConnectionClosedByBroker ,
284
+ StreamLostError ,
285
+ )),
286
+ stop = stop_after_attempt (3 ),
287
+ wait = wait_fixed (2 ),
288
+ before_sleep = before_sleep_log (log , WARNING )
289
+ )
290
+ async def start_consumer (
280
291
self ,
281
292
queue_name : str ,
282
293
callback : Callable | None = None ,
@@ -291,6 +302,10 @@ async def async_start_consumer(
291
302
):
292
303
"""Start the async consumer with the provided setup."""
293
304
# Check if there's a connection; if not, create one
305
+ try :
306
+ asyncio .get_running_loop ()
307
+ except RuntimeError :
308
+ raise MrsalNoAsyncioLoopFound (f'Young grasshopper! You forget to add asyncio.run(mrsal.start_consumer(...))' )
294
309
if not self ._connection :
295
310
await self .setup_async_connection ()
296
311
@@ -368,40 +383,3 @@ async def async_start_consumer(
368
383
await message .ack ()
369
384
self .log .success (f'Young grasshopper! Message ({ msg_id } ) from { app_id } received and properly processed.' )
370
385
371
- @retry (
372
- retry = retry_if_exception_type ((
373
- AMQPConnectionError ,
374
- ChannelClosedByBroker ,
375
- ConnectionClosedByBroker ,
376
- StreamLostError ,
377
- )),
378
- stop = stop_after_attempt (3 ),
379
- wait = wait_fixed (2 ),
380
- before_sleep = before_sleep_log (log , WARNING )
381
- )
382
- def start_consumer (
383
- self ,
384
- queue_name : str ,
385
- callback : Callable | None = None ,
386
- callback_args : dict [str , str | int | float | bool ] | None = None ,
387
- auto_ack : bool = False ,
388
- auto_declare : bool = True ,
389
- exchange_name : str | None = None ,
390
- exchange_type : str | None = None ,
391
- routing_key : str | None = None ,
392
- payload_model : Type | None = None ,
393
- requeue : bool = True
394
- ):
395
- """The client-facing method that runs the async consumer"""
396
- asyncio .run (self .async_start_consumer (
397
- queue_name = queue_name ,
398
- callback = callback ,
399
- callback_args = callback_args ,
400
- auto_ack = auto_ack ,
401
- auto_declare = auto_declare ,
402
- exchange_name = exchange_name ,
403
- exchange_type = exchange_type ,
404
- routing_key = routing_key ,
405
- payload_model = payload_model ,
406
- requeue = requeue
407
- ))
0 commit comments