The `main` function serves as the entry point for the [[Distillery Master]] script. It sets up the necessary components, starts the main event loop, and handles any exceptions that occur during execution.
```python
async def main():
try:
aws_manager = await AWSManager.get_instance()
aws_manager.setup_logging()
loop = asyncio.get_event_loop()
queue_task = loop.create_task(check_queue_and_create())
while True:
await asyncio.sleep(1)
except asyncio.CancelledError:
aws_manager.print_log('N/A', INSTANCE_IDENTIFIER, "Main function cancelled.", level='INFO')
except Exception as e:
formatted_exception = better_exceptions.format_exception(*sys.exc_info())
formatted_traceback = ''.join(formatted_exception)
aws_manager.print_log('N/A', INSTANCE_IDENTIFIER, formatted_traceback, level='ERROR')
finally:
queue_task.cancel()
try:
await queue_task
except asyncio.CancelledError:
aws_manager.print_log('N/A', INSTANCE_IDENTIFIER, "Queue task cancelled.", level='INFO')
await aws_manager.close_database_conn()
aws_manager.print_log('N/A', INSTANCE_IDENTIFIER, "Main function cleanup complete.", level='INFO')
asyncio.run(main())
```
### Function Breakdown
1. The `main` function is defined as an asynchronous function using the `async def` syntax. [[ELI5 explanation of Asynchronous programming by ChatGPT]]
2. Inside the `try` block:
- It retrieves an instance of the [[AWSManager]] class using `await AWSManager.get_instance()`.
- It sets up the logging configuration using `aws_manager.setup_logging()`.
- It retrieves the event loop using `asyncio.get_event_loop()`.
- It creates a new task for the `check_queue_and_create` function [[Master check_queue_and_create]] using `loop.create_task(check_queue_and_create())` and assigns it to the `queue_task` variable.
- It enters an infinite loop using `while True` and awaits for 1 second using `await asyncio.sleep(1)` to keep the main event loop running.
3. If an `asyncio.CancelledError` is raised (e.g., when the script is interrupted or cancelled), it logs an info message indicating that the main function was cancelled using `aws_manager.print_log()`.
4. If any other exception occurs during the execution of the `try` block, it formats the exception using `better_exceptions.format_exception(*sys.exc_info())` and logs the formatted traceback as an error using `aws_manager.print_log()`.
5. In the `finally` block, which is executed regardless of whether an exception occurred or not:
- It cancels the `queue_task` using `queue_task.cancel()` to stop the `check_queue_and_create` function.
- It wraps the `await queue_task` in a `try` block to handle any `asyncio.CancelledError` that may be raised when cancelling the task.
- If an `asyncio.CancelledError` is raised, it logs an info message indicating that the queue task was cancelled using `aws_manager.print_log()`.
- It closes the database connection using `await aws_manager.close_database_conn()`.
- It logs an info message indicating that the main function cleanup is complete using `aws_manager.print_log()`.
6. Outside the `main` function, the script starts the asynchronous execution using `asyncio.run(main())`, which runs the `main` function until it completes.
### Summary
The `main` function sets up the necessary components for the [[Distillery Master]] script, including retrieving an instance of the [[AWSManager]] class, setting up logging, and creating a task for the `check_queue_and_create` function [[Master check_queue_and_create]]. It enters an infinite loop to keep the main event loop running and handles any exceptions that may occur during execution. In the `finally` block, it performs cleanup tasks such as cancelling the `queue_task`, closing the database connection, and logging relevant messages. The script starts the asynchronous execution by calling `asyncio.run(main())`.