feature(services): add perma-active services (api itself)

This commit is contained in:
Houkime 2024-07-25 17:23:02 +00:00
parent 35e2e8cc78
commit 0329addd1f

View file

@ -110,6 +110,11 @@ class Service(ABC):
"""
return cls.get_user()
@staticmethod
def is_always_active() -> bool:
"""`True` if the service cannot be stopped, which is true for api itself"""
return False
@staticmethod
@abstractmethod
def is_movable() -> bool:
@ -498,7 +503,10 @@ class StoppedService:
def __enter__(self) -> Service:
self.original_status = self.service.get_status()
if self.original_status not in [ServiceStatus.INACTIVE, ServiceStatus.FAILED]:
if (
self.original_status not in [ServiceStatus.INACTIVE, ServiceStatus.FAILED]
and not self.service.is_always_active()
):
try:
self.service.stop()
wait_until_true(
@ -513,7 +521,10 @@ class StoppedService:
return self.service
def __exit__(self, type, value, traceback):
if self.original_status in [ServiceStatus.ACTIVATING, ServiceStatus.ACTIVE]:
if (
self.original_status in [ServiceStatus.ACTIVATING, ServiceStatus.ACTIVE]
and not self.service.is_always_active()
):
try:
self.service.start()
wait_until_true(