Skip to content

Commit

Permalink
Tidy up subscriber (#199)
Browse files Browse the repository at this point in the history
- Define the message type
- Acknowledge callback after processing
- Consistent logger and altering levels
- Docstrings
  • Loading branch information
stefpiatek authored Dec 21, 2023
1 parent 573dea7 commit b64dd69
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pixl_core/src/core/patient_queue/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from ._base import PixlBlockingInterface, PixlQueueInterface

LOGGER = logging.getLogger(__name__)
logger = logging.getLogger(__name__)


class PixlConsumer(PixlQueueInterface):
Expand Down Expand Up @@ -61,25 +61,24 @@ async def run(self, callback: Callable[[Message], Awaitable[None]]) -> None:
be processed. Must take a dictionary and return None.
"""
async with self._queue.iterator() as queue_iter:
# Pre-annotate the message type
message: aio_pika.abc.AbstractIncomingMessage
async for message in queue_iter:
if not self.token_bucket.has_token:
await asyncio.sleep(0.01)
await message.reject(requeue=True)
continue

# Messages need to be acknowledged before a callback otherwise
# RabbitMQ can close the connection. As all messages that raise
# exceptions aren't returned to the queue we're safe to do this
await message.ack()

try:
await asyncio.sleep(0.01) # Avoid very fast callbacks
await callback(deserialise(message.body))
except Exception:
LOGGER.exception(
logger.exception(
"Failed to process %s" "Not re-queuing message",
message.body.decode(),
)
finally:
await message.ack()

async def __aexit__(self, *args: object, **kwargs: Any) -> None:
"""Requirement for the asynchronous context manager"""
Expand All @@ -104,16 +103,17 @@ def consume_all(self, file_path: Path, timeout_in_seconds: int = 5) -> int:
)

def callback(method: Any, properties: Any, body: Any) -> None: # noqa: ARG001
"""Consume to file."""
try:
with Path.open(file_path, "a") as csv_file:
print(str(body.decode()), file=csv_file)
except: # noqa: E722
LOGGER.debug("Failed to consume")
logger.exception("Failed to consume")

counter = 0
for args in generator:
if all(arg is None for arg in args):
LOGGER.info("Stopping")
logger.info("Stopping")
break
callback(*args)
counter += 1
Expand Down

0 comments on commit b64dd69

Please sign in to comment.