2010-11-05 15:54:15 +01:00
|
|
|
"""
|
|
|
|
Python part of radio playout (pypo)
|
|
|
|
"""
|
2020-01-16 15:32:51 +01:00
|
|
|
|
2023-02-26 01:27:00 +01:00
|
|
|
import logging
|
2018-12-23 11:01:25 +01:00
|
|
|
import sys
|
|
|
|
import time
|
2021-06-03 15:20:39 +02:00
|
|
|
from datetime import datetime
|
2022-01-13 16:11:37 +01:00
|
|
|
from pathlib import Path
|
|
|
|
from queue import Queue
|
2023-02-28 17:50:53 +01:00
|
|
|
from typing import Any, Dict, Optional
|
2011-03-03 06:22:28 +01:00
|
|
|
|
2022-01-13 16:11:37 +01:00
|
|
|
import click
|
2022-07-22 16:26:43 +02:00
|
|
|
from libretime_api_client.v1 import ApiClient as LegacyClient
|
|
|
|
from libretime_api_client.v2 import ApiClient
|
2022-01-18 20:59:11 +01:00
|
|
|
from libretime_shared.cli import cli_config_options, cli_logging_options
|
2022-01-13 16:11:37 +01:00
|
|
|
from libretime_shared.config import DEFAULT_ENV_PREFIX
|
2023-02-26 01:27:00 +01:00
|
|
|
from libretime_shared.logging import setup_logger
|
2012-03-16 04:14:19 +01:00
|
|
|
|
2022-01-18 20:59:11 +01:00
|
|
|
from .config import CACHE_DIR, RECORD_DIR, Config
|
2022-08-09 21:14:19 +02:00
|
|
|
from .history.stats import StatsCollectorThread
|
2022-08-16 13:34:02 +02:00
|
|
|
from .liquidsoap.client import LiquidsoapClient
|
|
|
|
from .liquidsoap.version import LIQUIDSOAP_MIN_VERSION
|
2022-09-15 23:16:07 +02:00
|
|
|
from .message_handler import MessageListener
|
2023-02-26 00:11:49 +01:00
|
|
|
from .player.events import Events, FileEvents
|
2022-07-18 15:11:47 +02:00
|
|
|
from .player.fetch import PypoFetch
|
|
|
|
from .player.file import PypoFile
|
|
|
|
from .player.liquidsoap import PypoLiquidsoap
|
|
|
|
from .player.push import PypoPush
|
2018-12-23 11:01:25 +01:00
|
|
|
from .recorder import Recorder
|
2010-11-05 15:54:15 +01:00
|
|
|
|
2023-02-26 01:27:00 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-02-28 17:51:43 +01:00
|
|
|
for module in ("amqp",):
|
|
|
|
logging.getLogger(module).setLevel(logging.INFO)
|
|
|
|
logging.getLogger(module).propagate = False
|
|
|
|
|
2015-01-28 19:19:50 +01:00
|
|
|
|
2022-05-05 09:41:32 +02:00
|
|
|
@click.command(context_settings={"auto_envvar_prefix": DEFAULT_ENV_PREFIX})
|
2022-01-20 06:30:35 +01:00
|
|
|
@cli_logging_options()
|
2022-01-18 20:59:11 +01:00
|
|
|
@cli_config_options()
|
|
|
|
def cli(log_level: str, log_filepath: Optional[Path], config_filepath: Optional[Path]):
|
2022-01-13 16:11:37 +01:00
|
|
|
"""
|
|
|
|
Run playout.
|
|
|
|
"""
|
2023-02-26 01:27:00 +01:00
|
|
|
setup_logger(log_level, log_filepath)
|
2022-08-12 15:12:39 +02:00
|
|
|
config = Config(config_filepath)
|
2022-01-18 05:55:16 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
for dir_path in [CACHE_DIR, RECORD_DIR]:
|
|
|
|
dir_path.mkdir(exist_ok=True)
|
|
|
|
except OSError as exception:
|
|
|
|
logger.error(exception)
|
2022-01-13 16:11:37 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2018-12-23 11:01:25 +01:00
|
|
|
# Although all of our calculations are in UTC, it is useful to know what timezone
|
|
|
|
# the local machine is, so that we have a reference for what time the actual
|
|
|
|
# log entries were made
|
2023-02-26 12:01:59 +01:00
|
|
|
logger.info("Timezone: %s", time.tzname)
|
|
|
|
logger.info("UTC time: %s", datetime.utcnow())
|
2012-06-28 18:12:22 +02:00
|
|
|
|
2022-07-22 16:26:43 +02:00
|
|
|
legacy_client = LegacyClient()
|
2022-07-22 15:41:38 +02:00
|
|
|
api_client = ApiClient(
|
|
|
|
base_url=config.general.public_url,
|
|
|
|
api_key=config.general.api_key,
|
|
|
|
)
|
2011-05-13 22:08:04 +02:00
|
|
|
|
2022-08-21 11:24:54 +02:00
|
|
|
while not legacy_client.is_server_compatible():
|
2012-06-28 22:15:39 +02:00
|
|
|
time.sleep(5)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2013-05-06 17:42:39 +02:00
|
|
|
success = False
|
|
|
|
while not success:
|
|
|
|
try:
|
2022-07-22 16:26:43 +02:00
|
|
|
legacy_client.register_component("pypo")
|
2013-05-06 17:42:39 +02:00
|
|
|
success = True
|
2022-08-09 21:05:21 +02:00
|
|
|
except Exception as exception:
|
|
|
|
logger.exception(exception)
|
2013-05-06 17:42:39 +02:00
|
|
|
time.sleep(10)
|
|
|
|
|
2022-08-16 13:34:02 +02:00
|
|
|
liq_client = LiquidsoapClient(
|
|
|
|
host=config.playout.liquidsoap_host,
|
|
|
|
port=config.playout.liquidsoap_port,
|
|
|
|
)
|
2013-05-13 21:45:56 +02:00
|
|
|
|
2022-08-16 13:34:02 +02:00
|
|
|
logger.debug("Checking if Liquidsoap is running")
|
|
|
|
liq_version = liq_client.wait_for_version()
|
|
|
|
if not LIQUIDSOAP_MIN_VERSION <= liq_version:
|
2023-02-01 20:38:58 +01:00
|
|
|
raise RuntimeError(f"Invalid liquidsoap version {liq_version}")
|
2010-11-05 15:54:15 +01:00
|
|
|
|
2023-02-28 17:50:53 +01:00
|
|
|
fetch_queue: Queue[Dict[str, Any]] = Queue()
|
2022-09-09 20:23:35 +02:00
|
|
|
recorder_queue: Queue[Dict[str, Any]] = Queue()
|
2023-02-26 00:11:49 +01:00
|
|
|
push_queue: Queue[Events] = Queue()
|
2022-07-01 12:23:18 +02:00
|
|
|
# This queue is shared between pypo-fetch and pypo-file, where pypo-file
|
|
|
|
# is the consumer. Pypo-fetch will send every schedule it gets to pypo-file
|
|
|
|
# and pypo will parse this schedule to determine which file has the highest
|
|
|
|
# priority, and retrieve it.
|
2023-02-26 00:11:49 +01:00
|
|
|
file_queue: Queue[FileEvents] = Queue()
|
2022-08-21 11:28:57 +02:00
|
|
|
|
|
|
|
pypo_liquidsoap = PypoLiquidsoap(liq_client)
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2022-08-21 11:28:57 +02:00
|
|
|
file_thread = PypoFile(file_queue, api_client)
|
|
|
|
file_thread.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2022-08-21 11:28:57 +02:00
|
|
|
fetch_thread = PypoFetch(
|
|
|
|
fetch_queue,
|
|
|
|
push_queue,
|
|
|
|
file_queue,
|
2022-08-16 13:34:02 +02:00
|
|
|
liq_client,
|
2022-01-18 20:59:11 +01:00
|
|
|
pypo_liquidsoap,
|
|
|
|
config,
|
2022-07-22 16:26:43 +02:00
|
|
|
api_client,
|
|
|
|
legacy_client,
|
2018-12-23 11:01:25 +01:00
|
|
|
)
|
2022-08-21 11:28:57 +02:00
|
|
|
fetch_thread.start()
|
2012-06-27 04:41:11 +02:00
|
|
|
|
2022-08-21 11:28:57 +02:00
|
|
|
push_thread = PypoPush(push_queue, pypo_liquidsoap, config)
|
|
|
|
push_thread.start()
|
2011-03-21 00:34:43 +01:00
|
|
|
|
2022-08-21 11:28:57 +02:00
|
|
|
recorder_thread = Recorder(recorder_queue, config, legacy_client)
|
|
|
|
recorder_thread.start()
|
2012-02-24 19:12:50 +01:00
|
|
|
|
2022-08-10 17:35:06 +02:00
|
|
|
stats_collector_thread = StatsCollectorThread(config, legacy_client)
|
2022-08-21 11:28:57 +02:00
|
|
|
stats_collector_thread.start()
|
2012-11-02 22:50:43 +01:00
|
|
|
|
2022-09-15 23:16:07 +02:00
|
|
|
message_listener = MessageListener(config, fetch_queue, recorder_queue)
|
|
|
|
message_listener.run_forever()
|