Skip to content

Commit 9075cac

Browse files
committed
monitors loader uses task manager
1 parent 58f391d commit 9075cac

File tree

4 files changed

+16
-27
lines changed

4 files changed

+16
-27
lines changed

src/components/monitors_loader/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
check_monitor,
44
init,
55
register_monitor,
6-
wait_stop,
6+
run,
77
)
88

99
__all__ = [
1010
"MonitorValidationError",
1111
"check_monitor",
1212
"init",
1313
"register_monitor",
14-
"wait_stop",
14+
"run",
1515
]

src/components/monitors_loader/monitors_loader.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shutil
44
from datetime import datetime, timedelta
55
from pathlib import Path
6-
from typing import Any, Generator, cast
6+
from typing import Generator, cast
77

88
from pydantic.dataclasses import dataclass
99

@@ -27,8 +27,6 @@
2727
EARLY_LOAD_TIME = 5
2828
COOL_DOWN_TIME = 2
2929

30-
_task: asyncio.Task[Any]
31-
3230

3331
@dataclass
3432
class AdditionalFile:
@@ -289,7 +287,7 @@ async def _load_monitors(last_load_time: datetime | None) -> None:
289287
registry.monitors_pending.clear()
290288

291289

292-
async def _run() -> None:
290+
async def run() -> None:
293291
"""Monitors loading loop, loading them recurrently. Stops automatically when the app stops"""
294292
last_load_time: datetime | None = None
295293

@@ -326,21 +324,13 @@ async def _run() -> None:
326324
if time_since_last_load < COOL_DOWN_TIME:
327325
await app.sleep(COOL_DOWN_TIME - time_since_last_load)
328326

327+
_logger.info("Removing temporary monitors paths")
328+
shutil.rmtree(Path(RELATIVE_PATH) / MONITORS_LOAD_PATH, ignore_errors=True)
329+
shutil.rmtree(Path(RELATIVE_PATH) / MONITORS_PATH, ignore_errors=True)
330+
329331

330332
async def init(controller_enabled: bool) -> None:
331333
"""Load the internal monitors and sample monitors if controller is enabled, and start the
332334
monitors load task"""
333335
if controller_enabled:
334336
await _register_monitors()
335-
336-
global _task
337-
_task = asyncio.create_task(_run())
338-
339-
340-
async def wait_stop() -> None:
341-
"""Wait for the Monitors load task to finish"""
342-
global _task
343-
await _task
344-
_logger.info("Removing temporary monitors paths")
345-
shutil.rmtree(Path(RELATIVE_PATH) / MONITORS_LOAD_PATH, ignore_errors=True)
346-
shutil.rmtree(Path(RELATIVE_PATH) / MONITORS_PATH, ignore_errors=True)

src/main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ async def init(controller_enabled: bool, executor_enabled: bool) -> None:
4444
async def finish(controller_enabled: bool, executor_enabled: bool) -> None:
4545
"""Finish the application, making sure any exception won't impact other closing tasks"""
4646
await protected_task(_logger, http_server.wait_stop())
47-
await protected_task(_logger, monitors_loader.wait_stop())
4847
await protected_task(_logger, databases.close())
4948
await protected_task(_logger, internal_database.close())
5049
await protected_task(

tests/components/monitors_loader/test_monitors_loader.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -820,18 +820,18 @@ async def test_run_as_controller(mocker, monkeypatch, clear_database):
820820
_load_monitors_spy: AsyncMock = mocker.spy(monitors_loader, "_load_monitors")
821821

822822
await monitors_loader.init(controller_enabled=True)
823+
run_task = asyncio.create_task(monitors_loader.run())
823824

824825
for _ in range(3):
825826
await asyncio.sleep(0.1)
826827
registry.monitors_pending.set()
827828

828829
await asyncio.sleep(0.1)
829830
app.stop()
830-
await monitors_loader.wait_stop()
831831

832832
assert _load_monitors_spy.call_count == 4
833833

834-
assert monitors_loader._task.done()
834+
await asyncio.wait_for(run_task, timeout=0.1)
835835

836836
assert len(registry._monitors) == 2
837837

@@ -862,18 +862,18 @@ async def test_run_as_executor(mocker, monkeypatch, clear_database):
862862
)
863863

864864
await monitors_loader.init(controller_enabled=False)
865+
run_task = asyncio.create_task(monitors_loader.run())
865866

866867
for _ in range(3):
867868
await asyncio.sleep(0.1)
868869
registry.monitors_pending.set()
869870

870871
await asyncio.sleep(0.1)
871872
app.stop()
872-
await monitors_loader.wait_stop()
873873

874874
assert _load_monitors_spy.call_count == 4
875875

876-
assert monitors_loader._task.done()
876+
await asyncio.wait_for(run_task, timeout=0.1)
877877

878878
assert len(registry._monitors) == 2
879879
assert isinstance(registry._monitors[9999123]["module"], ModuleType)
@@ -905,18 +905,18 @@ async def test_run_cool_down(mocker, monkeypatch, clear_database):
905905
)
906906

907907
await monitors_loader.init(controller_enabled=False)
908+
run_task = asyncio.create_task(monitors_loader.run())
908909

909910
for _ in range(3):
910911
await asyncio.sleep(0.1)
911912
registry.monitors_pending.set()
912913

913914
await asyncio.sleep(0.1)
914915
app.stop()
915-
await monitors_loader.wait_stop()
916916

917917
assert _load_monitors_spy.call_count == 1
918918

919-
assert monitors_loader._task.done()
919+
await asyncio.wait_for(run_task, timeout=0.1)
920920

921921
assert len(registry._monitors) == 2
922922
assert isinstance(registry._monitors[9999123]["module"], ModuleType)
@@ -959,10 +959,10 @@ async def test_run_sleep_time(mocker, monkeypatch, clear_database, seconds, expe
959959
)
960960

961961
await monitors_loader.init(controller_enabled=False)
962+
run_task = asyncio.create_task(monitors_loader.run())
962963
await asyncio.sleep(1)
963964
app.stop()
964-
await monitors_loader.wait_stop()
965965

966-
assert monitors_loader._task.done()
966+
await asyncio.wait_for(run_task, timeout=0.1)
967967

968968
sleep_spy.assert_called_once_with(expected_sleep_time)

0 commit comments

Comments
 (0)