Merge pull request #159 from tiqi-group/158-defining-task-without-autostart-fails

fix: defining task without autostart fails
This commit is contained in:
Mose Müller 2024-09-21 11:43:31 +02:00 committed by GitHub
commit bbf479a440
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 13 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry]
name = "pydase"
version = "0.10.1"
version = "0.10.2"
description = "A flexible and robust Python library for creating, managing, and interacting with data services, with built-in support for web and RPC servers, and customizable features for diverse use cases."
authors = ["Mose Mueller <mosmuell@ethz.ch>"]
readme = "README.md"

View File

@ -17,16 +17,18 @@ def autostart_service_tasks(
"""
for attr in dir(service):
if is_property_attribute(service, attr): # prevent eval of property attrs
if is_property_attribute(service, attr) or attr in {
"_observers",
"__dict__",
}: # prevent eval of property attrs and recursion
continue
val = getattr(service, attr)
if (
isinstance(val, pydase.task.task.Task)
and val.autostart
and val.status == TaskStatus.NOT_RUNNING
):
if isinstance(val, pydase.task.task.Task):
if val.autostart and val.status == TaskStatus.NOT_RUNNING:
val.start()
else:
continue
else:
autostart_nested_service_tasks(val)
@ -38,7 +40,7 @@ def autostart_nested_service_tasks(
autostart_service_tasks(service)
elif isinstance(service, list):
for entry in service:
autostart_service_tasks(entry)
autostart_nested_service_tasks(entry)
elif isinstance(service, dict):
for entry in service.values():
autostart_service_tasks(entry)
autostart_nested_service_tasks(entry)

View File

@ -18,23 +18,30 @@ async def test_start_and_stop_task(caplog: LogCaptureFixture) -> None:
class MyService(pydase.DataService):
@task()
async def my_task(self) -> None:
logger.info("Triggered task.")
while True:
logger.debug("Logging message")
await asyncio.sleep(0.01)
await asyncio.sleep(1)
# Your test code here
service_instance = MyService()
state_manager = StateManager(service_instance)
DataServiceObserver(state_manager)
autostart_service_tasks(service_instance)
await asyncio.sleep(0.1)
assert service_instance.my_task.status == TaskStatus.NOT_RUNNING
service_instance.my_task.start()
await asyncio.sleep(0.1)
assert service_instance.my_task.status == TaskStatus.RUNNING
assert "'my_task.status' changed to 'TaskStatus.RUNNING'" in caplog.text
assert "Logging message" in caplog.text
assert "Triggered task." in caplog.text
caplog.clear()
service_instance.my_task.stop()
await asyncio.sleep(0.1)
assert service_instance.my_task.status == TaskStatus.NOT_RUNNING
assert "Task 'my_task' was cancelled" in caplog.text
@ -44,6 +51,8 @@ async def test_autostart_task(caplog: LogCaptureFixture) -> None:
@task(autostart=True)
async def my_task(self) -> None:
logger.info("Triggered task.")
while True:
await asyncio.sleep(1)
# Your test code here
service_instance = MyService()
@ -53,6 +62,7 @@ async def test_autostart_task(caplog: LogCaptureFixture) -> None:
autostart_service_tasks(service_instance)
await asyncio.sleep(0.1)
assert service_instance.my_task.status == TaskStatus.RUNNING
assert "'my_task.status' changed to 'TaskStatus.RUNNING'" in caplog.text
@ -65,6 +75,8 @@ async def test_nested_list_autostart_task(
@task(autostart=True)
async def my_task(self) -> None:
logger.info("Triggered task.")
while True:
await asyncio.sleep(1)
class MyService(pydase.DataService):
sub_services_list = [MySubService() for i in range(2)]
@ -75,6 +87,8 @@ async def test_nested_list_autostart_task(
autostart_service_tasks(service_instance)
await asyncio.sleep(0.1)
assert service_instance.sub_services_list[0].my_task.status == TaskStatus.RUNNING
assert service_instance.sub_services_list[1].my_task.status == TaskStatus.RUNNING
assert (
"'sub_services_list[0].my_task.status' changed to 'TaskStatus.RUNNING'"
@ -111,6 +125,10 @@ async def test_nested_dict_autostart_task(
assert (
service_instance.sub_services_dict["first"].my_task.status == TaskStatus.RUNNING
)
assert (
service_instance.sub_services_dict["second"].my_task.status
== TaskStatus.RUNNING
)
assert (
"'sub_services_dict[\"first\"].my_task.status' changed to 'TaskStatus.RUNNING'"