Commit 4dfb3f9a authored by David Trattnig's avatar David Trattnig
Browse files

Improve graceful shutdown. #88

parent 11d6e39f
Pipeline #1156 passed with stage
in 1 minute and 9 seconds
......@@ -26,6 +26,7 @@ import os
import sys
import signal
import logging
import threading
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
......@@ -54,7 +55,6 @@ class EngineRunner:
logger = None
config = None
engine = None
lqs = None
def __init__(self):
......@@ -64,12 +64,12 @@ class EngineRunner:
self.config = config
AuraLogger(self.config)
self.logger = logging.getLogger("AuraEngine")
def run(self):
"""
Starts Engine Core.
"""
"""
from src.engine import Engine
self.engine = Engine()
......@@ -86,15 +86,14 @@ class EngineRunner:
"""
Shutdown of the engine. Also terminates the Liquidsoap thread.
"""
for thread in threading.enumerate():
self.logger.info(thread.name)
if self.engine:
self.engine.terminate()
if self.lqs:
self.lqs.terminate()
self.logger.info("Terminated Liquidsoap")
self.logger.info("Gracefully terminated Aura Engine! (signum:%s, frame:%s)" % (signum, frame))
sys.exit(0)
self.logger.info(f"Gracefully terminated Aura Engine! (signum:{signum}, frame:{frame})")
sys.exit(0)
......@@ -103,7 +102,7 @@ class EngineRunner:
#
if __name__ == "__main__":
if __name__ == "__main__":
runner = EngineRunner()
signal.signal(signal.SIGINT, runner.exit_gracefully)
signal.signal(signal.SIGTERM, runner.exit_gracefully)
......@@ -112,5 +111,5 @@ if __name__ == "__main__":
if "--recreate-database" in sys.argv:
runner.recreate_db()
sys.exit(0)
runner.run()
......@@ -167,10 +167,12 @@ class Engine():
"""
Terminates the engine and all related processes.
"""
if self.scheduler: self.scheduler.terminate()
if self.eci: self.eci.terminate()
#
# PLAYER
#
......
......@@ -304,5 +304,5 @@ class ApiFetcher(threading.Thread):
"""
Terminates the thread.
"""
self.logger.info("Shutting down API fetcher...")
self.logger.info(SU.yellow("[ApiFetcher] Shutting down..."))
self.stop_event.set()
......@@ -230,7 +230,7 @@ class ProgrammeService():
"""
Called when thread is stopped or a signal to terminate is received.
"""
self.logger.info("Shutting down programme service ...")
self.logger.info(SU.yellow("[ProgrammeService] Shutting down..."))
if self.api_fetcher:
self.api_fetcher.terminate()
......
......@@ -407,7 +407,7 @@ class AuraScheduler(threading.Thread):
"""
Called when thread is stopped or a signal to terminate is received.
"""
self.logger.info("Shutting down scheduler ...")
self.logger.info(SU.yellow("[Scheduler] Shutting down..."))
self.programme.terminate()
self.exit_event.set()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment