feature(servers): set default timeout of server operations to 10 min

pull/35/head
Houkime 2023-07-12 17:02:54 +00:00 committed by Inex Code
parent b001e198bf
commit c53f35c947
1 changed files with 19 additions and 10 deletions

View File

@ -12,6 +12,8 @@ from selfprivacy_api.services.generic_size_counter import get_storage_usage
from selfprivacy_api.services.owned_path import OwnedPath from selfprivacy_api.services.owned_path import OwnedPath
from selfprivacy_api.utils.waitloop import wait_until_true from selfprivacy_api.utils.waitloop import wait_until_true
DEFAULT_START_STOP_TIMEOUT = 10 * 60
class ServiceStatus(Enum): class ServiceStatus(Enum):
"""Enum for service status""" """Enum for service status"""
@ -250,16 +252,17 @@ class Service(ABC):
class StoppedService: class StoppedService:
""" """
A context manager that stops the service if needed and reactivates it A context manager that stops the service if needed and reactivates it
after you are done if it was active after you are done if it was active
Example: Example:
``` ```
assert service.get_status() == ServiceStatus.ACTIVE assert service.get_status() == ServiceStatus.ACTIVE
with StoppedService(service) [as stopped_service]: with StoppedService(service) [as stopped_service]:
assert service.get_status() == ServiceStatus.INACTIVE assert service.get_status() == ServiceStatus.INACTIVE
``` ```
""" """
def __init__(self, service: Service): def __init__(self, service: Service):
self.service = service self.service = service
self.original_status = service.get_status() self.original_status = service.get_status()
@ -268,10 +271,16 @@ class StoppedService:
self.original_status = self.service.get_status() self.original_status = self.service.get_status()
if self.original_status != ServiceStatus.INACTIVE: if self.original_status != ServiceStatus.INACTIVE:
self.service.stop() self.service.stop()
wait_until_true(lambda: self.service.get_status() == ServiceStatus.INACTIVE) wait_until_true(
lambda: self.service.get_status() == ServiceStatus.INACTIVE,
timeout_sec=DEFAULT_START_STOP_TIMEOUT,
)
return self.service return self.service
def __exit__(self, type, value, traceback): def __exit__(self, type, value, traceback):
if self.original_status in [ServiceStatus.ACTIVATING, ServiceStatus.ACTIVE]: if self.original_status in [ServiceStatus.ACTIVATING, ServiceStatus.ACTIVE]:
self.service.start() self.service.start()
wait_until_true(lambda: self.service.get_status() == ServiceStatus.ACTIVE) wait_until_true(
lambda: self.service.get_status() == ServiceStatus.ACTIVE,
timeout_sec=DEFAULT_START_STOP_TIMEOUT,
)