Enabled NACK on exception

This commit is contained in:
Ashish D'Souza 2023-11-10 00:18:57 -06:00
parent da4c1ba2c4
commit b1c16a00ff
1 changed files with 64 additions and 59 deletions

View File

@ -1,8 +1,9 @@
import os
import json
import datetime
import functools
import threading
import traceback
import functools
from abc import ABC, abstractmethod
import pika
@ -28,6 +29,7 @@ class Player(ABC):
def subscribe(self, rabbitmq_host: str, rabbitmq_port: str, rabbitmq_topics: list[str]) -> None:
def handle_message(connection: pika.adapters.blocking_connection.BlockingConnection, channel: pika.channel.Channel, method: pika.spec.Basic.Deliver, body: bytes):
try:
topic = method.routing_key
message_type = topic.split('.', 1)[0]
@ -83,18 +85,21 @@ class Player(ABC):
reminder_audio = Audio.from_tts(f'Reminder to {reminder_text}')
alarm_audio.concat(reminder_audio)
case _:
# Send NACK, do not requeue
connection.add_callback_threadsafe(functools.partial(channel.basic_nack, delivery_tag=method.delivery_tag, requeue=False))
raise ValueError(f'No alarm type {_}')
audio_save_path = os.path.join(os.environ['AUDIO_ROOT_PATH'], 'alarm.mp3')
ringtone_audio.save(audio_save_path)
alarm_audio.save(audio_save_path)
self.play(audio_save_path)
case _:
# Send NACK, do not requeue
connection.add_callback_threadsafe(functools.partial(channel.basic_nack, delivery_tag=method.delivery_tag, requeue=False))
raise ValueError(f'No message type {_}')
# Send ACK
connection.add_callback_threadsafe(functools.partial(channel.basic_ack, delivery_tag=method.delivery_tag))
except Exception:
traceback.print_exc()
# Send NACK, do not requeue
connection.add_callback_threadsafe(functools.partial(channel.basic_nack, delivery_tag=method.delivery_tag, requeue=False))
def on_message(connection: pika.adapters.blocking_connection.BlockingConnection, channel: pika.channel.Channel, method: pika.spec.Basic.Deliver, properties: pika.spec.BasicProperties, body: bytes) -> None:
thread = threading.Thread(target=handle_message, args=(connection, channel, method, body))