Skeleton ready
This commit is contained in:
56
shared/application/rabbitmq_application.py
Normal file
56
shared/application/rabbitmq_application.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from typing import Callable, Awaitable, override
|
||||
from asyncio import CancelledError
|
||||
from logging import Logger
|
||||
from punq import Container
|
||||
from aio_pika.abc import AbstractIncomingMessage
|
||||
|
||||
from kernel.application.stop_event import StopEvent
|
||||
from kernel.application.view.configuration import Configuration
|
||||
from kernel.application.application import Application
|
||||
from kernel.rabbitmq.client import Client
|
||||
|
||||
from shared.application.view.listener_arguments import ListenerArguments
|
||||
|
||||
|
||||
class RabbitMQApplication(Application):
|
||||
@override
|
||||
async def do(
|
||||
self,
|
||||
callback: Callable[[Container, AbstractIncomingMessage], Awaitable[None]]
|
||||
) -> None:
|
||||
rabbitmq = await self.__connect_rabbitmq_client()
|
||||
arguments = self.get_container().resolve(ListenerArguments)
|
||||
|
||||
async def consumer(message: AbstractIncomingMessage) -> None:
|
||||
await callback(self.get_container(), message)
|
||||
|
||||
queue = arguments.queue
|
||||
exchange = arguments.exchange
|
||||
routing_key = arguments.routing_key
|
||||
|
||||
await rabbitmq.consume(queue, exchange, consumer, routing_key)
|
||||
|
||||
logger = self.get_container().resolve(Logger)
|
||||
|
||||
logger.info(f"Queue: {queue}")
|
||||
logger.info(f"Exchange: `{exchange}`")
|
||||
|
||||
if routing_key != "":
|
||||
logger.info(f"Routing key: {routing_key}")
|
||||
|
||||
try:
|
||||
await self.get_container().resolve(StopEvent).wait()
|
||||
except CancelledError as e:
|
||||
logger.exception(e)
|
||||
|
||||
async def __connect_rabbitmq_client(self) -> Client:
|
||||
rabbitmq_client = self.get_container().resolve(Client)
|
||||
|
||||
stop_event = self.get_container().resolve(StopEvent)
|
||||
configuration = self.get_container().resolve(Configuration)
|
||||
|
||||
await rabbitmq_client\
|
||||
.connect(stop_event, configuration.rabbitmq().timeout)
|
||||
|
||||
return rabbitmq_client
|
||||
|
||||
Reference in New Issue
Block a user