Skip to content

Commit

Permalink
Trying exception handling in a different way
Browse files Browse the repository at this point in the history
  • Loading branch information
nss10 committed Oct 5, 2021
1 parent 2b97611 commit 59784fd
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions src/audit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,29 @@ async def startup_event():
and config["QUEUE_CONFIG"].get("type") == "aws_sqs"
):
loop = asyncio.get_running_loop()
loop.create_task(pull_from_queue_loop())
loop.set_exception_handler(handle_exception)
try:
loop.create_task(pull_from_queue_loop())
except Exception as e:
logger.error(e)
loop.stop()

# loop.set_exception_handler(handle_exception)

@app.on_event("shutdown")
async def shutdown_event():
logger.info("Closing async client.")
await app.async_client.aclose()

def handle_exception(loop, context):
"""
Whenever an exception occurs in the asyncio loop, the loop still continues to execute without crashing.
Therefore, we implement a custom exception handler that will ensure that the loop is stopped upon an Exception.
"""
msg = context.get("exception", context.get("message"))
logger.error(f"Caught exception: {msg}")
loop.stop()
logger.info("Shutting down...")
asyncio.create_task(shutdown_event())
# def handle_exception(loop, context):
# """
# Whenever an exception occurs in the asyncio loop, the loop still continues to execute without crashing.
# Therefore, we implement a custom exception handler that will ensure that the loop is stopped upon an Exception.
# """
# msg = context.get("exception", context.get("message"))
# logger.error(f"Caught exception: {msg}")
# loop.stop()
# logger.info("Shutting down...")
# asyncio.create_task(shutdown_event())

return app

Expand Down

0 comments on commit 59784fd

Please sign in to comment.