Enabled NACK on exception
This commit is contained in:
parent
da4c1ba2c4
commit
b1c16a00ff
|
@ -1,8 +1,9 @@
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
import functools
|
|
||||||
import threading
|
import threading
|
||||||
|
import traceback
|
||||||
|
import functools
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
import pika
|
import pika
|
||||||
|
@ -28,6 +29,7 @@ class Player(ABC):
|
||||||
|
|
||||||
def subscribe(self, rabbitmq_host: str, rabbitmq_port: str, rabbitmq_topics: list[str]) -> None:
|
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):
|
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
|
topic = method.routing_key
|
||||||
message_type = topic.split('.', 1)[0]
|
message_type = topic.split('.', 1)[0]
|
||||||
|
|
||||||
|
@ -83,18 +85,21 @@ class Player(ABC):
|
||||||
reminder_audio = Audio.from_tts(f'Reminder to {reminder_text}')
|
reminder_audio = Audio.from_tts(f'Reminder to {reminder_text}')
|
||||||
alarm_audio.concat(reminder_audio)
|
alarm_audio.concat(reminder_audio)
|
||||||
case _:
|
case _:
|
||||||
# Send NACK, do not requeue
|
raise ValueError(f'No alarm type {_}')
|
||||||
connection.add_callback_threadsafe(functools.partial(channel.basic_nack, delivery_tag=method.delivery_tag, requeue=False))
|
|
||||||
|
|
||||||
audio_save_path = os.path.join(os.environ['AUDIO_ROOT_PATH'], 'alarm.mp3')
|
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)
|
self.play(audio_save_path)
|
||||||
case _:
|
case _:
|
||||||
# Send NACK, do not requeue
|
raise ValueError(f'No message type {_}')
|
||||||
connection.add_callback_threadsafe(functools.partial(channel.basic_nack, delivery_tag=method.delivery_tag, requeue=False))
|
|
||||||
|
|
||||||
# Send ACK
|
# Send ACK
|
||||||
connection.add_callback_threadsafe(functools.partial(channel.basic_ack, delivery_tag=method.delivery_tag))
|
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:
|
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))
|
thread = threading.Thread(target=handle_message, args=(connection, channel, method, body))
|
||||||
|
|
Loading…
Reference in New Issue