Commit a8f74893 authored by David Trattnig's avatar David Trattnig
Browse files

Init Monitor in Engine.

parent 10d45d44
......@@ -28,13 +28,14 @@ import json
from modules.communication.liquidsoap.playerclient import LiquidSoapPlayerClient
# from modules.communication.liquidsoap.recorderclient import LiquidSoapRecorderClient
from modules.core.startup import StartupThread
from modules.core.state import PlayerStateService
from modules.core.startup import StartupThread
from modules.core.state import PlayerStateService
from modules.core.monitor import Monitoring
from modules.communication.mail import AuraMailer
from modules.base.enum import ChannelType, Channel, TransitionType
from modules.base.utils import TerminalColors, SimpleUtil
from modules.base.exceptions import LQConnectionError, InvalidChannelException, NoActiveEntryException
from modules.base.enum import ChannelType, Channel, TransitionType
from modules.base.utils import TerminalColors, SimpleUtil
from modules.base.exceptions import LQConnectionError, InvalidChannelException, NoActiveEntryException, EngineMalfunctionException
class SoundSystem():
......@@ -48,6 +49,7 @@ class SoundSystem():
transaction = 0
channels = None
scheduler = None
monitoring = None
#error_data = None #FIXME Can be removed
auramailer = None
is_liquidsoap_running = False
......@@ -76,6 +78,7 @@ class SoundSystem():
self.client = LiquidSoapPlayerClient(config, "engine.sock")
# self.lqcr = LiquidSoapRecorderClient(config, "record.sock")
self.monitoring = Monitoring(config, self)
self.auramailer = AuraMailer(self.config)
self.is_active()
......@@ -111,6 +114,17 @@ class SoundSystem():
self.is_liquidsoap_running = True
self.logger.info(SimpleUtil.green("Engine Core ------[ connected ]-------- Liquidsoap"))
# Start Monitoring
is_valid = self.monitoring.has_valid_status(False)
status = self.monitoring.get_status()
self.logger.info("Status Monitor:\n%s" % json.dumps(status, indent=4))
if not is_valid:
self.logger.info("Engine Status: " + SimpleUtil.red(status["engine"]["status"]))
raise EngineMalfunctionException
else:
self.logger.info("Engine Status: " + SimpleUtil.green("OK"))
def is_ready(self):
"""
......
......@@ -108,7 +108,8 @@ class Monitoring:
Args:
update_vitality_only (Boolean): Refreshes only the vital parts required for the heartbeat
"""
is_valid = False
if update_vitality_only:
self.update_vitality_status()
else:
......@@ -121,16 +122,15 @@ class Monitoring:
and self.status["audio_store"]["exists"]:
self.status["engine"]["status"] = MonitorResponseCode.OK.value
return True
is_valid = True
else:
self.status["engine"]["status"] = MonitorResponseCode.INVALID_STATE.value
return False
except Exception as e:
self.logger.error("Exception while validating engine status: " + str(e))
self.status["engine"]["status"] = MonitorResponseCode.INVALID_STATE.value
return False
return is_valid
......@@ -178,7 +178,7 @@ class Monitoring:
self.status["redis_ready"] = self.validate_redis_connection()
self.status["audio_store"] = self.validate_directory(self.config.get("audiofolder"))
# After first update start the Heartbeat Monitior
# After first update start the Heartbeat Monitor
if not self.heartbeat_running:
self.heartbeat_running = True
if self.config.get("heartbeat_frequency") > 0:
......@@ -189,10 +189,10 @@ class Monitoring:
def heartbeat(self):
"""
Every `heartbeat_frequency` seconds the current vitality status is checked. If it's okay,
as heartbeat is sent to the configured server.
a heartbeat is sent to the configured server.
"""
if self.has_valid_status(True):
self.heartbeat_socket.sendto(b"OK", (self.heartbeat_server, self.heartbeat_port))
self.heartbeat_socket.sendto(str.encode("OK"), (self.heartbeat_server, self.heartbeat_port))
threading.Timer(self.config.get("heartbeat_frequency"), self.heartbeat).start()
......
......@@ -25,10 +25,9 @@ import threading
import meta
import json
from modules.base.exceptions import NoActiveScheduleException, EngineMalfunctionException
from modules.base.exceptions import NoActiveScheduleException
from modules.base.enum import Channel, ChannelType
from modules.base.utils import TerminalColors, SimpleUtil, EngineUtil
from modules.core.monitor import Monitoring
class StartupThread(threading.Thread):
"""
......@@ -50,7 +49,6 @@ class StartupThread(threading.Thread):
self.logger = logging.getLogger("AuraEngine")
self.soundsystem = soundsystem
self.scheduler = soundsystem.scheduler
self.monitoring = Monitoring(soundsystem.config, soundsystem)
def run(self):
......@@ -58,24 +56,14 @@ class StartupThread(threading.Thread):
Boots the soundsystem.
"""
try:
self.soundsystem.start()
is_valid = self.monitoring.has_valid_status(False)
status = self.monitoring.get_status()
self.logger.info("Status Monitor:\n%s" % json.dumps(status, indent=4))
if not is_valid:
self.logger.info("Engine Status: " + SimpleUtil.red(status["engine"]["status"]))
raise EngineMalfunctionException
else:
self.logger.info("Engine Status: " + SimpleUtil.green("OK"))
self.soundsystem.start()
self.logger.info(EngineUtil.engine_info("Engine Core", meta.__version__))
self.scheduler.on_ready()
except NoActiveScheduleException as e:
self.logger.info("Nothing scheduled at startup time. Please check if there are follow-up schedules.")
except Exception as e:
self.logger.error(SimpleUtil.red("Error while initializing the sound-system: " + str(e)))
self.logger.error(SimpleUtil.red("Error while initializing the soundsystem: " + str(e)))
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