Add logging and service restarting

This commit is contained in:
Marek Wolan
2023-08-31 11:20:16 +01:00
parent 62be66205c
commit 7759c178bb
2 changed files with 17 additions and 9 deletions

View File

@@ -1007,6 +1007,7 @@ class Node(SimComponent):
self.services[service.uuid] = service self.services[service.uuid] = service
service.parent = self service.parent = self
service.install() # Perform any additional setup, such as creating files for this service on the node. service.install() # Perform any additional setup, such as creating files for this service on the node.
self.sys_log.info(f"Installed service {service.name}")
_LOGGER.info(f"Added service {service.uuid} to node {self.uuid}") _LOGGER.info(f"Added service {service.uuid} to node {self.uuid}")
def uninstall_service(self, service: Service) -> None: def uninstall_service(self, service: Service) -> None:
@@ -1021,6 +1022,7 @@ class Node(SimComponent):
service.uninstall() # Perform additional teardown, such as removing files or restarting the machine. service.uninstall() # Perform additional teardown, such as removing files or restarting the machine.
self.services.pop(service.uuid) self.services.pop(service.uuid)
service.parent = None service.parent = None
self.sys_log.info(f"Uninstalled service {service.name}")
_LOGGER.info(f"Removed service {service.uuid} from node {self.uuid}") _LOGGER.info(f"Removed service {service.uuid} from node {self.uuid}")
def __contains__(self, item: Any) -> bool: def __contains__(self, item: Any) -> bool:

View File

@@ -1,6 +1,6 @@
from abc import abstractmethod from abc import abstractmethod
from enum import Enum from enum import Enum
from typing import Any, Dict from typing import Any, Dict, Optional
from primaite.simulator.core import Action, ActionManager from primaite.simulator.core import Action, ActionManager
from primaite.simulator.system.software import IOSoftware from primaite.simulator.system.software import IOSoftware
@@ -32,6 +32,10 @@ class Service(IOSoftware):
operating_state: ServiceOperatingState operating_state: ServiceOperatingState
"The current operating state of the Service." "The current operating state of the Service."
restart_duration: int = 5
"How many timesteps does it take to restart this service."
_restart_countdown: Optional[int] = None
"If currently restarting, how many timesteps remain until the restart is finished."
def _init_action_manager(self) -> ActionManager: def _init_action_manager(self) -> ActionManager:
am = super()._init_action_manager() am = super()._init_action_manager()
@@ -97,41 +101,43 @@ class Service(IOSoftware):
def stop(self) -> None: def stop(self) -> None:
"""Stop the service.""" """Stop the service."""
if self.operating_state in [ServiceOperatingState.RUNNING, ServiceOperatingState.PAUSED]: if self.operating_state in [ServiceOperatingState.RUNNING, ServiceOperatingState.PAUSED]:
self.parent.sys_log.info(f"Stopping service {self.name}")
self.operating_state = ServiceOperatingState.STOPPED self.operating_state = ServiceOperatingState.STOPPED
def start(self) -> None: def start(self) -> None:
"""Start the service.""" """Start the service."""
if self.operating_state == ServiceOperatingState.STOPPED: if self.operating_state == ServiceOperatingState.STOPPED:
self.parent.sys_log.info(f"Starting service {self.name}")
self.operating_state = ServiceOperatingState.RUNNING self.operating_state = ServiceOperatingState.RUNNING
def pause(self) -> None: def pause(self) -> None:
"""Pause the service.""" """Pause the service."""
if self.operating_state == ServiceOperatingState.RUNNING: if self.operating_state == ServiceOperatingState.RUNNING:
self.parent.sys_log.info(f"Pausing service {self.name}")
self.operating_state = ServiceOperatingState.PAUSED self.operating_state = ServiceOperatingState.PAUSED
def resume(self) -> None: def resume(self) -> None:
"""Resume paused service.""" """Resume paused service."""
if self.operating_state == ServiceOperatingState.PAUSED: if self.operating_state == ServiceOperatingState.PAUSED:
self.parent.sys_log.info(f"Resuming service {self.name}")
self.operating_state = ServiceOperatingState.RUNNING self.operating_state = ServiceOperatingState.RUNNING
def restart(self) -> None: def restart(self) -> None:
"""Restart running service.""" """Restart running service."""
if self.operating_state in [ServiceOperatingState.RUNNING, ServiceOperatingState.PAUSED]: if self.operating_state in [ServiceOperatingState.RUNNING, ServiceOperatingState.PAUSED]:
self.parent.sys_log.info(f"Pausing service {self.name}")
self.operating_state = ServiceOperatingState.RESTARTING self.operating_state = ServiceOperatingState.RESTARTING
self.restart_countdown = 5 # TODO: implement restart duration self.restart_countdown = self.restarting_duration # TODO: implement restart duration
def disable(self) -> None: def disable(self) -> None:
"""Disable the service.""" """Disable the service."""
if self.operating_state in [ self.parent.sys_log.info(f"Disabling Application {self.name}")
ServiceOperatingState.RUNNING, self.operating_state = ServiceOperatingState.DISABLED
ServiceOperatingState.STOPPED,
ServiceOperatingState.PAUSED,
]:
self.operating_state = ServiceOperatingState.DISABLED
def enable(self) -> None: def enable(self) -> None:
"""Enable the disabled service.""" """Enable the disabled service."""
if self.operating_state == ServiceOperatingState.DISABLED: if self.operating_state == ServiceOperatingState.DISABLED:
self.parent.sys_log.info(f"Enabling Application {self.name}")
self.operating_state = ServiceOperatingState.STOPPED self.operating_state = ServiceOperatingState.STOPPED
def apply_timestep(self, timestep: int) -> None: def apply_timestep(self, timestep: int) -> None:
@@ -146,6 +152,6 @@ class Service(IOSoftware):
""" """
super().apply_timestep(timestep) super().apply_timestep(timestep)
if self.operating_state == ServiceOperatingState.RESTARTING: if self.operating_state == ServiceOperatingState.RESTARTING:
self.restart_countdown -= 1
if self.restart_countdown <= 0: if self.restart_countdown <= 0:
self.operating_state = ServiceOperatingState.RUNNING self.operating_state = ServiceOperatingState.RUNNING
self.restart_countdown -= 1