2021-06-03 15:20:39 +02:00
|
|
|
import json
|
2012-02-28 20:44:39 +01:00
|
|
|
import time
|
2021-06-03 15:20:39 +02:00
|
|
|
from threading import Thread
|
|
|
|
|
2012-02-28 20:44:39 +01:00
|
|
|
# For RabbitMQ
|
2019-11-03 01:48:32 +01:00
|
|
|
from kombu.connection import Connection
|
2012-03-17 19:16:11 +01:00
|
|
|
from kombu.messaging import Exchange, Queue
|
2019-11-03 01:48:32 +01:00
|
|
|
from kombu.mixins import ConsumerMixin
|
2022-01-18 20:59:11 +01:00
|
|
|
from libretime_shared.config import RabbitMQConfig
|
2022-01-13 16:11:37 +01:00
|
|
|
from loguru import logger
|
2012-02-28 20:44:39 +01:00
|
|
|
|
|
|
|
|
2019-11-03 01:48:32 +01:00
|
|
|
class RabbitConsumer(ConsumerMixin):
|
|
|
|
def __init__(self, connection, queues, handler):
|
|
|
|
self.connection = connection
|
|
|
|
self.queues = queues
|
|
|
|
self.handler = handler
|
|
|
|
|
|
|
|
def get_consumers(self, Consumer, channel):
|
|
|
|
return [
|
2021-05-27 16:23:02 +02:00
|
|
|
Consumer(self.queues, callbacks=[self.on_message], accept=["text/plain"]),
|
2019-11-03 01:48:32 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
def on_message(self, body, message):
|
|
|
|
self.handler.handle_message(message.payload)
|
|
|
|
message.ack()
|
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2012-02-28 20:44:39 +01:00
|
|
|
class PypoMessageHandler(Thread):
|
2022-08-14 19:55:39 +02:00
|
|
|
name = "message_handler"
|
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
def __init__(self, pq, rq, config: RabbitMQConfig):
|
2012-02-28 20:44:39 +01:00
|
|
|
Thread.__init__(self)
|
|
|
|
self.pypo_queue = pq
|
|
|
|
self.recorder_queue = rq
|
2013-04-26 04:11:26 +02:00
|
|
|
self.config = config
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2012-02-28 20:44:39 +01:00
|
|
|
def init_rabbit_mq(self):
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Initializing RabbitMQ stuff")
|
2012-02-28 20:44:39 +01:00
|
|
|
try:
|
2021-05-27 16:23:02 +02:00
|
|
|
schedule_exchange = Exchange(
|
|
|
|
"airtime-pypo", "direct", durable=True, auto_delete=True
|
|
|
|
)
|
2012-02-28 20:44:39 +01:00
|
|
|
schedule_queue = Queue("pypo-fetch", exchange=schedule_exchange, key="foo")
|
2021-05-27 16:23:02 +02:00
|
|
|
with Connection(
|
2022-01-18 20:59:11 +01:00
|
|
|
f"amqp://{self.config.user}:{self.config.password}"
|
|
|
|
f"@{self.config.host}:{self.config.port}"
|
|
|
|
f"/{self.config.vhost}",
|
2021-05-27 16:23:02 +02:00
|
|
|
heartbeat=5,
|
|
|
|
) as connection:
|
2019-11-03 01:48:32 +01:00
|
|
|
rabbit = RabbitConsumer(connection, [schedule_queue], self)
|
|
|
|
rabbit.run()
|
2022-08-09 21:05:21 +02:00
|
|
|
except Exception as exception:
|
|
|
|
logger.exception(exception)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2022-07-01 12:23:18 +02:00
|
|
|
# Handle a message from RabbitMQ, put it into our yucky global var.
|
|
|
|
# Hopefully there is a better way to do this.
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2012-02-28 20:44:39 +01:00
|
|
|
def handle_message(self, message):
|
2012-06-27 04:41:11 +02:00
|
|
|
try:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Received event from RabbitMQ: %s" % message)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2020-05-04 13:24:57 +02:00
|
|
|
try:
|
|
|
|
message = message.decode()
|
|
|
|
except (UnicodeDecodeError, AttributeError):
|
|
|
|
pass
|
2012-06-27 04:41:11 +02:00
|
|
|
m = json.loads(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
command = m["event_type"]
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Handling command: " + command)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2021-05-27 16:23:02 +02:00
|
|
|
if command == "update_schedule":
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Updating schedule...")
|
2012-02-28 20:44:39 +01:00
|
|
|
self.pypo_queue.put(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
elif command == "reset_liquidsoap_bootstrap":
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Resetting bootstrap vars...")
|
2012-09-15 01:01:21 +02:00
|
|
|
self.pypo_queue.put(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
elif command == "update_stream_setting":
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Updating stream setting...")
|
2012-02-28 20:44:39 +01:00
|
|
|
self.pypo_queue.put(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
elif command == "update_stream_format":
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Updating stream format...")
|
2012-02-28 20:44:39 +01:00
|
|
|
self.pypo_queue.put(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
elif command == "update_station_name":
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Updating station name...")
|
2012-02-28 20:44:39 +01:00
|
|
|
self.pypo_queue.put(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
elif command == "switch_source":
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("switch_source command received...")
|
2012-03-08 23:42:38 +01:00
|
|
|
self.pypo_queue.put(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
elif command == "update_transition_fade":
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Updating trasition fade...")
|
2012-03-21 03:16:17 +01:00
|
|
|
self.pypo_queue.put(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
elif command == "disconnect_source":
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("disconnect_source command received...")
|
2012-03-14 16:09:59 +01:00
|
|
|
self.pypo_queue.put(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
elif command == "update_recorder_schedule":
|
2012-02-28 20:44:39 +01:00
|
|
|
self.recorder_queue.put(message)
|
2021-05-27 16:23:02 +02:00
|
|
|
elif command == "cancel_recording":
|
2012-02-28 20:44:39 +01:00
|
|
|
self.recorder_queue.put(message)
|
2012-09-15 01:01:21 +02:00
|
|
|
else:
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.info("Unknown command: %s" % command)
|
2022-08-09 21:05:21 +02:00
|
|
|
except Exception as exception:
|
|
|
|
logger.exception(exception)
|
2012-02-28 20:44:39 +01:00
|
|
|
|
|
|
|
def main(self):
|
2015-06-26 23:11:04 +02:00
|
|
|
try:
|
2019-11-03 01:48:32 +01:00
|
|
|
self.init_rabbit_mq()
|
2022-08-09 21:05:21 +02:00
|
|
|
except Exception as exception:
|
|
|
|
logger.exception(exception)
|
2022-01-13 16:11:37 +01:00
|
|
|
logger.error("Error connecting to RabbitMQ Server. Trying again in few seconds")
|
2015-06-26 23:11:04 +02:00
|
|
|
time.sleep(5)
|
2012-02-28 20:44:39 +01:00
|
|
|
|
2022-07-01 12:23:18 +02:00
|
|
|
# Main loop of the thread:
|
|
|
|
# Wait for schedule updates from RabbitMQ, but in case there aren't any,
|
|
|
|
# poll the server to get the upcoming schedule.
|
2021-05-27 16:23:02 +02:00
|
|
|
|
2012-02-28 20:44:39 +01:00
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
self.main()
|