2023-08-03 21:30:13 +01:00
|
|
|
from enum import Enum
|
2023-08-31 11:20:16 +01:00
|
|
|
from typing import Any, Dict, Optional
|
2023-08-03 21:30:13 +01:00
|
|
|
|
2023-08-31 11:27:52 +01:00
|
|
|
from primaite import getLogger
|
2023-08-29 12:34:41 +01:00
|
|
|
from primaite.simulator.core import Action, ActionManager
|
2023-08-03 21:30:13 +01:00
|
|
|
from primaite.simulator.system.software import IOSoftware
|
|
|
|
|
|
2023-08-31 11:27:52 +01:00
|
|
|
_LOGGER = getLogger(__name__)
|
|
|
|
|
|
2023-08-03 21:30:13 +01:00
|
|
|
|
|
|
|
|
class ServiceOperatingState(Enum):
|
|
|
|
|
"""Enumeration of Service Operating States."""
|
|
|
|
|
|
|
|
|
|
RUNNING = 1
|
|
|
|
|
"The service is currently running."
|
2023-08-10 13:26:51 +01:00
|
|
|
STOPPED = 2
|
|
|
|
|
"The service is not running."
|
2023-08-03 21:30:13 +01:00
|
|
|
INSTALLING = 3
|
|
|
|
|
"The service is being installed or updated."
|
2023-08-10 13:26:51 +01:00
|
|
|
RESTARTING = 4
|
|
|
|
|
"The service is in the process of restarting."
|
|
|
|
|
PAUSED = 5
|
2023-08-03 21:30:13 +01:00
|
|
|
"The service is temporarily paused."
|
2023-08-10 13:26:51 +01:00
|
|
|
DISABLED = 6
|
2023-08-03 21:30:13 +01:00
|
|
|
"The service is disabled and cannot be started."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Service(IOSoftware):
|
|
|
|
|
"""
|
|
|
|
|
Represents a Service in the simulation environment.
|
|
|
|
|
|
|
|
|
|
Services are programs that run in the background and may perform input/output operations.
|
|
|
|
|
"""
|
2023-08-07 19:33:52 +01:00
|
|
|
|
2023-09-05 17:14:47 +01:00
|
|
|
operating_state: ServiceOperatingState = ServiceOperatingState.STOPPED
|
2023-08-03 21:30:13 +01:00
|
|
|
"The current operating state of the Service."
|
2023-08-31 11:20:16 +01:00
|
|
|
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."
|
2023-08-03 21:30:13 +01:00
|
|
|
|
2023-08-29 14:33:28 +01:00
|
|
|
def _init_action_manager(self) -> ActionManager:
|
|
|
|
|
am = super()._init_action_manager()
|
|
|
|
|
am.add_action("stop", Action(func=lambda request, context: self.stop()))
|
|
|
|
|
am.add_action("start", Action(func=lambda request, context: self.start()))
|
|
|
|
|
am.add_action("pause", Action(func=lambda request, context: self.pause()))
|
|
|
|
|
am.add_action("resume", Action(func=lambda request, context: self.resume()))
|
|
|
|
|
am.add_action("restart", Action(func=lambda request, context: self.restart()))
|
|
|
|
|
am.add_action("disable", Action(func=lambda request, context: self.disable()))
|
|
|
|
|
am.add_action("enable", Action(func=lambda request, context: self.enable()))
|
|
|
|
|
return am
|
|
|
|
|
|
2023-08-03 21:30:13 +01:00
|
|
|
def describe_state(self) -> Dict:
|
|
|
|
|
"""
|
2023-08-17 15:32:12 +01:00
|
|
|
Produce a dictionary describing the current state of this object.
|
2023-08-03 21:30:13 +01:00
|
|
|
|
2023-08-17 15:32:12 +01:00
|
|
|
Please see :py:meth:`primaite.simulator.core.SimComponent.describe_state` for a more detailed explanation.
|
2023-08-03 21:30:13 +01:00
|
|
|
|
2023-08-17 15:32:12 +01:00
|
|
|
:return: Current state of this object and child objects.
|
2023-08-03 21:30:13 +01:00
|
|
|
:rtype: Dict
|
|
|
|
|
"""
|
2023-08-20 18:38:02 +01:00
|
|
|
state = super().describe_state()
|
|
|
|
|
state.update({"operating_state": self.operating_state.name})
|
|
|
|
|
return state
|
2023-08-03 21:30:13 +01:00
|
|
|
|
|
|
|
|
def reset_component_for_episode(self, episode: int):
|
|
|
|
|
"""
|
|
|
|
|
Resets the Service component for a new episode.
|
|
|
|
|
|
|
|
|
|
This method ensures the Service is ready for a new episode, including resetting any
|
|
|
|
|
stateful properties or statistics, and clearing any message queues.
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
2023-08-07 19:33:52 +01:00
|
|
|
def send(self, payload: Any, session_id: str, **kwargs) -> bool:
|
2023-08-03 21:30:13 +01:00
|
|
|
"""
|
2023-08-07 19:33:52 +01:00
|
|
|
Sends a payload to the SessionManager.
|
2023-08-03 21:30:13 +01:00
|
|
|
|
|
|
|
|
The specifics of how the payload is processed and whether a response payload
|
|
|
|
|
is generated should be implemented in subclasses.
|
|
|
|
|
|
|
|
|
|
:param payload: The payload to send.
|
|
|
|
|
:return: True if successful, False otherwise.
|
|
|
|
|
"""
|
|
|
|
|
pass
|
|
|
|
|
|
2023-08-07 19:33:52 +01:00
|
|
|
def receive(self, payload: Any, session_id: str, **kwargs) -> bool:
|
2023-08-03 21:30:13 +01:00
|
|
|
"""
|
|
|
|
|
Receives a payload from the SessionManager.
|
|
|
|
|
|
|
|
|
|
The specifics of how the payload is processed and whether a response payload
|
|
|
|
|
is generated should be implemented in subclasses.
|
|
|
|
|
|
|
|
|
|
:param payload: The payload to receive.
|
|
|
|
|
:return: True if successful, False otherwise.
|
|
|
|
|
"""
|
|
|
|
|
pass
|
2023-08-29 12:34:41 +01:00
|
|
|
|
|
|
|
|
def stop(self) -> None:
|
|
|
|
|
"""Stop the service."""
|
2023-08-31 11:27:52 +01:00
|
|
|
_LOGGER.debug(f"Stopping service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
if self.operating_state in [ServiceOperatingState.RUNNING, ServiceOperatingState.PAUSED]:
|
2023-09-06 11:12:03 +01:00
|
|
|
self.sys_log.info(f"Stopping service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
self.operating_state = ServiceOperatingState.STOPPED
|
|
|
|
|
|
2023-09-06 11:12:03 +01:00
|
|
|
def start(self, **kwargs) -> None:
|
2023-08-29 12:34:41 +01:00
|
|
|
"""Start the service."""
|
2023-08-31 11:27:52 +01:00
|
|
|
_LOGGER.debug(f"Starting service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
if self.operating_state == ServiceOperatingState.STOPPED:
|
2023-09-06 11:12:03 +01:00
|
|
|
self.sys_log.info(f"Starting service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
self.operating_state = ServiceOperatingState.RUNNING
|
|
|
|
|
|
|
|
|
|
def pause(self) -> None:
|
|
|
|
|
"""Pause the service."""
|
2023-08-31 11:27:52 +01:00
|
|
|
_LOGGER.debug(f"Pausing service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
if self.operating_state == ServiceOperatingState.RUNNING:
|
2023-09-06 11:12:03 +01:00
|
|
|
self.sys_log.info(f"Pausing service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
self.operating_state = ServiceOperatingState.PAUSED
|
|
|
|
|
|
|
|
|
|
def resume(self) -> None:
|
|
|
|
|
"""Resume paused service."""
|
2023-08-31 11:27:52 +01:00
|
|
|
_LOGGER.debug(f"Resuming service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
if self.operating_state == ServiceOperatingState.PAUSED:
|
2023-09-06 11:12:03 +01:00
|
|
|
self.sys_log.info(f"Resuming service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
self.operating_state = ServiceOperatingState.RUNNING
|
|
|
|
|
|
|
|
|
|
def restart(self) -> None:
|
|
|
|
|
"""Restart running service."""
|
2023-08-31 11:27:52 +01:00
|
|
|
_LOGGER.debug(f"Restarting service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
if self.operating_state in [ServiceOperatingState.RUNNING, ServiceOperatingState.PAUSED]:
|
2023-09-06 11:12:03 +01:00
|
|
|
self.sys_log.info(f"Pausing service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
self.operating_state = ServiceOperatingState.RESTARTING
|
2023-08-31 11:32:11 +01:00
|
|
|
self.restart_countdown = self.restarting_duration
|
2023-08-29 12:34:41 +01:00
|
|
|
|
|
|
|
|
def disable(self) -> None:
|
|
|
|
|
"""Disable the service."""
|
2023-08-31 11:27:52 +01:00
|
|
|
_LOGGER.debug(f"Disabling service {self.name}")
|
2023-09-06 11:12:03 +01:00
|
|
|
self.sys_log.info(f"Disabling Application {self.name}")
|
2023-08-31 11:20:16 +01:00
|
|
|
self.operating_state = ServiceOperatingState.DISABLED
|
2023-08-29 12:34:41 +01:00
|
|
|
|
|
|
|
|
def enable(self) -> None:
|
|
|
|
|
"""Enable the disabled service."""
|
2023-08-31 11:27:52 +01:00
|
|
|
_LOGGER.debug(f"Enabling service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
if self.operating_state == ServiceOperatingState.DISABLED:
|
2023-09-06 11:12:03 +01:00
|
|
|
self.sys_log.info(f"Enabling Application {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
self.operating_state = ServiceOperatingState.STOPPED
|
|
|
|
|
|
|
|
|
|
def apply_timestep(self, timestep: int) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Apply a single timestep of simulation dynamics to this service.
|
|
|
|
|
|
|
|
|
|
In this instance, if any multi-timestep processes are currently occurring (such as restarting or installation),
|
|
|
|
|
then they are brought one step closer to being finished.
|
|
|
|
|
|
|
|
|
|
:param timestep: The current timestep number. (Amount of time since simulation episode began)
|
|
|
|
|
:type timestep: int
|
|
|
|
|
"""
|
|
|
|
|
super().apply_timestep(timestep)
|
|
|
|
|
if self.operating_state == ServiceOperatingState.RESTARTING:
|
|
|
|
|
if self.restart_countdown <= 0:
|
2023-08-31 11:27:52 +01:00
|
|
|
_LOGGER.debug(f"Restarting finished for service {self.name}")
|
2023-08-29 12:34:41 +01:00
|
|
|
self.operating_state = ServiceOperatingState.RUNNING
|
2023-08-31 11:20:16 +01:00
|
|
|
self.restart_countdown -= 1
|