Skip to content

Commit

Permalink
hive-service: Rename ServiceStatus as ServiceCondition
Browse files Browse the repository at this point in the history
  • Loading branch information
gbenson committed Sep 24, 2024
1 parent e8b12e5 commit fa33fcd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion libs/service/hive/service/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .restart_monitor import RestartMonitor, ServiceStatus
from .restart_monitor import RestartMonitor, ServiceCondition
18 changes: 9 additions & 9 deletions libs/service/hive/service/restart_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@

logger = logging.getLogger(__name__)

ServiceStatus = Enum("ServiceStatus", "HEALTHY DUBIOUS IN_ERROR")
ServiceCondition = Enum("ServiceCondition", "HEALTHY DUBIOUS IN_ERROR")


@dataclass
class RestartMonitor:
try:
DEFAULT_NAME = os.path.basename(sys.argv[0])
DEFAULT_INITIAL_STATUS = ServiceStatus.HEALTHY
DEFAULT_INITIAL_STATUS = ServiceCondition.HEALTHY
except Exception as e:
DEFAULT_NAME = f"[ERROR: {e}]"
DEFAULT_INITIAL_STATUS = ServiceStatus.DUBIOUS
DEFAULT_INITIAL_STATUS = ServiceCondition.DUBIOUS

name: str = DEFAULT_NAME
basename: str = ".hive-service-restart.stamp"
dirname: str = field(default_factory=os.getcwd)
status: ServiceStatus = DEFAULT_INITIAL_STATUS
status: ServiceCondition = DEFAULT_INITIAL_STATUS
rapid_restart_cutoff: float = 5 * MINUTES
rapid_restart_cooldown_time: Optional[float] = None

Expand All @@ -54,15 +54,15 @@ def __post_init__(self):
try:
self._run()
except Exception:
self.status = ServiceStatus.IN_ERROR
self.status = ServiceCondition.IN_ERROR
self.log_exception()

def log(self, message, level=logging.INFO):
if self.status is not ServiceStatus.IN_ERROR:
if self.status is not ServiceCondition.IN_ERROR:
if level > logging.WARNING:
self.status = ServiceStatus.IN_ERROR
self.status = ServiceCondition.IN_ERROR
elif level > logging.INFO:
self.status = ServiceStatus.DUBIOUS
self.status = ServiceCondition.DUBIOUS
logger.log(level, message)
self._messages.append(message)

Expand All @@ -80,7 +80,7 @@ def log_rapid_cycling(self, interval: float):
self.log_error("is restarting rapidly")

def log_exception(self):
self.status = ServiceStatus.IN_ERROR
self.status = ServiceCondition.IN_ERROR
logger.exception("LOGGED EXCEPTION")

@property
Expand Down
4 changes: 2 additions & 2 deletions libs/service/tests/test_restart_monitor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from hive.service import RestartMonitor, ServiceStatus
from hive.service import RestartMonitor, ServiceCondition


def test_init():
Expand All @@ -10,7 +10,7 @@ def __post_init__(self):

got = TestRestartMonitor()
assert got.name == "pytest"
assert got.status == ServiceStatus.HEALTHY
assert got.status == ServiceCondition.HEALTHY

basenames = tuple(map(os.path.basename, got.filenames))
assert basenames == (
Expand Down
6 changes: 3 additions & 3 deletions services/matrix-connector/hive/matrix_connector/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from hive.common import ArgumentParser
from hive.common.units import SECONDS, MINUTES
from hive.messaging import Channel, blocking_connection
from hive.service import RestartMonitor, ServiceStatus
from hive.service import RestartMonitor, ServiceCondition

logger = logging.getLogger(__name__)
d = logger.debug
Expand Down Expand Up @@ -105,8 +105,8 @@ class ReportingRestartMonitor(RestartMonitor):
@property
def status_emoji(self):
return {
ServiceStatus.HEALTHY: "",
ServiceStatus.DUBIOUS: ":white_question_mark:",
ServiceCondition.HEALTHY: "",
ServiceCondition.DUBIOUS: ":white_question_mark:",
}.get(self.status, ":fire:")

def report(self, sender: Sender):
Expand Down

0 comments on commit fa33fcd

Please sign in to comment.