chore: refactored task autostart

This commit is contained in:
Mose Müller 2024-09-16 14:17:20 +02:00
parent 78964be506
commit 2f5a640c4c

View File

@ -1,5 +1,8 @@
from typing import Any
import pydase.data_service.data_service import pydase.data_service.data_service
import pydase.task.task import pydase.task.task
from pydase.task.task_status import TaskStatus
from pydase.utils.helpers import is_property_attribute from pydase.utils.helpers import is_property_attribute
@ -13,15 +16,28 @@ def autostart_service_tasks(
""" """
for attr in dir(service): for attr in dir(service):
if not is_property_attribute(service, attr): # prevent eval of property attrs if is_property_attribute(service, attr): # prevent eval of property attrs
continue
val = getattr(service, attr) val = getattr(service, attr)
if isinstance(val, pydase.task.task.Task) and val.autostart: if (
isinstance(val, pydase.task.task.Task)
and val.autostart
and val.status == TaskStatus.NOT_RUNNING
):
val.start() val.start()
elif isinstance(val, pydase.DataService): else:
autostart_service_tasks(val) autostart_nested_service_tasks(val)
elif isinstance(val, list):
for entry in val:
def autostart_nested_service_tasks(
service: pydase.data_service.data_service.DataService | list[Any] | dict[Any, Any],
) -> None:
if isinstance(service, pydase.DataService):
autostart_service_tasks(service)
elif isinstance(service, list):
for entry in service:
autostart_service_tasks(entry) autostart_service_tasks(entry)
elif isinstance(val, dict): elif isinstance(service, dict):
for entry in val.values(): for entry in service.values():
autostart_service_tasks(entry) autostart_service_tasks(entry)