From 2b4cfec06900a9a32b695303d7e06144c3ba9365 Mon Sep 17 00:00:00 2001 From: David Trattnig <david.trattnig@o94.at> Date: Wed, 28 Oct 2020 22:15:45 +0100 Subject: [PATCH] Adapt to new startup logic. #39 --- .vscode/launch.json | 8 ++--- docs/developer-guide.md | 4 +-- run.py | 78 ++++++++++++++++------------------------- run.sh | 8 ++--- 4 files changed, 40 insertions(+), 58 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 56ec3c0f..f13fe3ed 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,14 +8,14 @@ "name": "Start Aura Engine", "type": "python", "request": "launch", - "program": "${workspaceFolder}/engine-core.py", + "program": "${workspaceFolder}/run.py", "console": "integratedTerminal" }, { "name": "Start Engine Core Only", "type": "python", "request": "launch", - "program": "${workspaceFolder}/engine-core.py", + "program": "${workspaceFolder}/run.py", "args": [ "--without-lqs" ], @@ -25,7 +25,7 @@ "name": "Start Engine LQS Only", "type": "python", "request": "launch", - "program": "${workspaceFolder}/engine-core.py", + "program": "${workspaceFolder}/run.py", "args": [ "--lqs-only" ], @@ -49,7 +49,7 @@ "name": "Aura Engine - Recreate Database", "type": "python", "request": "launch", - "program": "${workspaceFolder}/engine-core.py", + "program": "${workspaceFolder}/run.py", "args": ["--recreate-database"], "console": "integratedTerminal" } diff --git a/docs/developer-guide.md b/docs/developer-guide.md index addcd596..ac483de2 100644 --- a/docs/developer-guide.md +++ b/docs/developer-guide.md @@ -59,8 +59,8 @@ OpenAPI definition for Engine API: https://app.swaggerhub.com/apis/AURA-Engine/e When you start Engine the following is happening: -1. Python `engine-core.py`: Initializes `modules/core/engine.py` (The virtual mixer; class for remote-controlling Liquidsoap), Scheduler -2. Python `engine-core.py`: Start Liquidsoap. +1. Python `run.py`: Initializes `modules/core/engine.py` (The virtual mixer; class for remote-controlling Liquidsoap), Scheduler +2. Python `run.py`: Start Liquidsoap. 3. Liquidsoap: When Liquidsoap finished its startup, it creates a socket file as configured in `socketdir` of `engine.ini`. 4. Python `modules/core/liquidsoap/client.py`: Connects to that socket file. 5. Python `modules/schedulung/scheduler.py`: Continously loads schedules from the API endpoints, stores them in the local database and starts the playout as per the schedules. diff --git a/run.py b/run.py index 5224196f..a8242834 100755 --- a/run.py +++ b/run.py @@ -28,12 +28,14 @@ import signal import logging import subprocess +import time + from flask import Flask from flask_sqlalchemy import SQLAlchemy from modules.base.logger import AuraLogger from modules.base.config import AuraConfig -from modules.base.utils import SimpleUtil +from modules.base.utils import SimpleUtil as SU config = AuraConfig() @@ -48,29 +50,23 @@ configure_flask() DB = SQLAlchemy(app) -class AuraEngine: +class EngineRunner: """ - AuraEngine does the following: + EngineRunner is in charge of - 1. Initialize the engine and scheduler - 3. Start Liquidsoap in a separate thread which connects to the engine + 1. Initializing the engine and all dependencies + 3. Starting Liquidsoap in a separate thread, if requested """ logger = None config = None - server = None - messenger = None - controller = None engine = None - scheduler = None lqs = None - lqs_startup = None - def __init__(self): """ - Initializes Engine Core. + Constructor """ self.config = config AuraLogger(self.config) @@ -78,43 +74,32 @@ class AuraEngine: - - def startup(self, lqs_startup): + def run(self, start_lqs, lqs_debug_flags): """ Starts Engine Core. """ from modules.scheduling.scheduler import AuraScheduler from modules.core.engine import Engine - # If Liquidsoap should be started automatically - self.lqs_startup = lqs_startup - # Check if the database has to be re-created if self.config.get("recreate_db") is not None: AuraScheduler.init_database() - # Create scheduler and Liquidsoap communicator - self.engine = Engine() - - # Sleep needed, because the socket is created too slowly by Liquidsoap - # time.sleep(1) + if start_lqs: + runner.run_lqs(lqs_debug_flags) + else: + self.logger.info(SU.yellow("Please note, Liquidsoap needs to be started manually.")) - # def on_initialized(): - # """ - # Called when the engine is initialized, before the Liquidsoap connection is established." - # """ - # self.logger.info(SU.green("Engine Core initialized - Waiting for Liquidsoap connection ...")) - # if self.lqs_startup: - # self.start_lqs(False, False) - # else: - # self.logger.info(SU.yellow("Please note, Liquidsoap needs to be started manually.")) + self.engine = Engine() - def start_lqs(self, debug_output, verbose_output): + def run_lqs(self, lqs_debug_flags): """ Starts Liquidsoap. """ + debug_output = lqs_debug_flags.get("debug_output") + verbose_output = lqs_debug_flags.get("verbose_output") lqs_path = self.config.get("liquidsoap_path") lqs_cwd = os.getcwd() + "/" + self.config.get("liquidsoap_working_dir") lqs_output = "" @@ -155,16 +140,13 @@ class AuraEngine: """ Shutdown of the engine. Also terminates the Liquidsoap thread. """ + if self.engine: + self.engine.terminate() + if self.lqs: self.lqs.terminate() self.logger.info("Terminated Liquidsoap") - if self.engine: - self.engine.terminate() - - if self.messenger: - self.messenger.terminate() - self.logger.info("Gracefully terminated Aura Engine! (signum:%s, frame:%s)" % (signum, frame)) sys.exit(0) @@ -176,24 +158,24 @@ class AuraEngine: if __name__ == "__main__": - engine = AuraEngine() - start_lqs = True + runner = EngineRunner() + do_start_lqs = True lqs_cmd = False - signal.signal(signal.SIGINT, engine.exit_gracefully) - signal.signal(signal.SIGTERM, engine.exit_gracefully) + signal.signal(signal.SIGINT, runner.exit_gracefully) + signal.signal(signal.SIGTERM, runner.exit_gracefully) if len(sys.argv) >= 2: if "--without-lqs" in sys.argv: - start_lqs = False + do_start_lqs = False if "--get-lqs-command" in sys.argv: lqs_cmd = True if "--use-test-data" in sys.argv: - engine.config.set("use_test_data", True) + runner.config.set("use_test_data", True) if "--recreate-database" in sys.argv: - engine.config.set("recreate_db", True) + runner.config.set("recreate_db", True) if lqs_cmd: - print(engine.get_lqs_cmd(True, True)) - else: - engine.startup(start_lqs) + print(runner.get_lqs_cmd(True, True)) + else: + runner.run(do_start_lqs, { "debug_output": False, "verbose_output": False }) diff --git a/run.sh b/run.sh index 94277539..09cbcad5 100755 --- a/run.sh +++ b/run.sh @@ -58,26 +58,26 @@ if [[ $docker == "false" ]]; then if [[ $mode == "engine" ]]; then eval $(opam env) - /usr/bin/env $PYTHON_EXEC engine-core.py + /usr/bin/env $PYTHON_EXEC run.py fi ### Runs Engine Core only ### if [[ $mode == "core" ]]; then - /usr/bin/env $PYTHON_EXEC engine-core.py --without-lqs + /usr/bin/env $PYTHON_EXEC run.py --without-lqs fi ### Runs Liquidsoap only ### if [[ $mode == "lqs" ]]; then - lqs=$(/usr/bin/env $PYTHON_EXEC engine-core.py --get-lqs-command) + lqs=$(/usr/bin/env $PYTHON_EXEC run.py --get-lqs-command) eval "$lqs" fi ### CAUTION: This deletes everything in your database ### if [[ $mode == "recreate-database" ]]; then - /usr/bin/env $PYTHON_EXEC engine-core.py --recreate-database + /usr/bin/env $PYTHON_EXEC run.py --recreate-database fi fi -- GitLab