From 8da2b59137bebcd7dceb549f5ccb48dc8dc3bc63 Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Mon, 9 Dec 2024 12:39:16 +0100 Subject: [PATCH] fixed rule lifecycle --- .../rules/habapp/test_event_listener.py | 4 +- run/conf_testing/rules/test_runner.py | 41 ++++++++++++++----- src/HABApp/rule_ctx/rule_ctx.py | 4 +- src/HABApp/rule_manager/rule_file.py | 4 +- src/HABApp/rule_manager/rule_manager.py | 6 +-- tests/rule_runner/rule_runner.py | 3 -- 6 files changed, 40 insertions(+), 22 deletions(-) diff --git a/run/conf_testing/rules/habapp/test_event_listener.py b/run/conf_testing/rules/habapp/test_event_listener.py index 573ebf74..480a7e19 100644 --- a/run/conf_testing/rules/habapp/test_event_listener.py +++ b/run/conf_testing/rules/habapp/test_event_listener.py @@ -14,7 +14,7 @@ def __init__(self) -> None: self.add_test('CheckWarning', self.test_unload) - def test_unload(self) -> None: + async def test_unload(self) -> None: item = Item.get_create_item(get_random_name('HABApp')) grp = EventListenerGroup().add_listener(item, self.cb, ValueChangeEventFilter()) @@ -23,7 +23,7 @@ def test_unload(self) -> None: grp.listen() grp.cancel() - self._habapp_ctx.unload_rule() + await self._habapp_ctx.unload_rule() # Workaround to so we don't crash self.on_rule_unload = lambda: None diff --git a/run/conf_testing/rules/test_runner.py b/run/conf_testing/rules/test_runner.py index 71f3f2e8..eb2f3f18 100644 --- a/run/conf_testing/rules/test_runner.py +++ b/run/conf_testing/rules/test_runner.py @@ -9,7 +9,11 @@ from HABApp.core.items import Item -class TestRunnterImpl(TestRunnerRule): +class TestRunnerImpl(TestRunnerRule): + + def __init__(self) -> None: + super().__init__() + self._file_pos = 0 async def tests_done(self, results: list[TestResult]) -> None: results.extend( @@ -18,10 +22,30 @@ async def tests_done(self, results: list[TestResult]) -> None: # show errors of HABApp.log log_file = CONFIG.directories.logging / 'HABApp.log' - errors = [line for line in log_file.read_text() if ' ERROR ' in line] - if errors: + + seek = self._file_pos + self._file_pos = log_file.stat().st_size + + with log_file.open('r') as f: + f.seek(seek) + text = f.read() + + show = [] + for line in text.splitlines(): + if ' ERROR ' in line: + show.append(line) + continue + + if ' WARNING ' in line: + if 'DeprecationWarning' in line or 'is a UoM item but "unit" is not found in item metadata' in line: + continue + if 'Item type changed from' in line: + continue + show.append(line) + + if show: print('-' * 120) - for line in errors: + for line in show: print(line) print('-' * 120) print() @@ -29,15 +53,12 @@ async def tests_done(self, results: list[TestResult]) -> None: async def test_lifecycle_methods(self) -> None: item = Item.get_item('RuleLifecycleMethods') - assert item.value == ['on_rule_loaded_thread', 'on_rule_loaded_task'] + assert item.value[0:2] == ['on_rule_loaded_thread', 'on_rule_loaded_task'] self.post_event(TOPIC_FILES, RequestFileUnloadEvent('rules/habapp/test_internals.py')) await asyncio.sleep(1) - assert item.value == [ - 'on_rule_loaded_thread', 'on_rule_loaded_task', - 'on_rule_unloaded_thread', 'on_rule_unloaded_task' - ] + assert item.value[2:4] == ['on_rule_removed_thread', 'on_rule_removed_task'] -TestRunnterImpl() +TestRunnerImpl() diff --git a/src/HABApp/rule_ctx/rule_ctx.py b/src/HABApp/rule_ctx/rule_ctx.py index 8b2b4d54..6d756346 100644 --- a/src/HABApp/rule_ctx/rule_ctx.py +++ b/src/HABApp/rule_ctx/rule_ctx.py @@ -40,7 +40,7 @@ def remove_event_listener(self, listener: TB) -> TB: event_bus.remove_listener(listener) return listener - def unload_rule(self) -> None: + async def unload_rule(self) -> None: with HABApp.core.wrapper.ExceptionToHABApp(log): rule = self.rule @@ -61,7 +61,7 @@ def unload_rule(self) -> None: rule._habapp_rule_ctx = None # user implementation - rule.on_rule_removed() + await wrap_func(rule.on_rule_removed).async_run() async def check_rule(self) -> None: with HABApp.core.wrapper.ExceptionToHABApp(log): diff --git a/src/HABApp/rule_manager/rule_file.py b/src/HABApp/rule_manager/rule_file.py index dba06226..58ac2e4d 100644 --- a/src/HABApp/rule_manager/rule_file.py +++ b/src/HABApp/rule_manager/rule_file.py @@ -42,7 +42,7 @@ async def check_all_rules(self) -> None: for rule in self.rules.values(): # type: HABApp.Rule await get_current_context(rule).check_rule() - def unload(self): + async def unload(self) -> None: # If we don't have any rules we can not unload if not self.rules: @@ -50,7 +50,7 @@ def unload(self): # unload all registered callbacks for rule in self.rules.values(): # type: HABApp.Rule - get_current_context(rule).unload_rule() + await get_current_context(rule).unload_rule() log.debug(f'File {self.name} successfully unloaded!') return None diff --git a/src/HABApp/rule_manager/rule_manager.py b/src/HABApp/rule_manager/rule_manager.py index 9f5d1af7..ad727787 100644 --- a/src/HABApp/rule_manager/rule_manager.py +++ b/src/HABApp/rule_manager/rule_manager.py @@ -125,7 +125,7 @@ async def request_file_unload(self, name: str, path: Path, request_lock=True): with self.__files_lock: rule = self.files.pop(path_str) - await wrap_func(rule.unload).async_run() + await rule.unload() finally: if request_lock: self.__load_lock.release() @@ -165,6 +165,6 @@ async def request_file_load(self, name: str, path: Path): # Do simple checks which prevent errors await file.check_all_rules() - def shutdown(self) -> None: + async def shutdown(self) -> None: for f in self.files.values(): - f.unload() + await f.unload() diff --git a/tests/rule_runner/rule_runner.py b/tests/rule_runner/rule_runner.py index 125d85b1..4b9c12a8 100644 --- a/tests/rule_runner/rule_runner.py +++ b/tests/rule_runner/rule_runner.py @@ -99,9 +99,6 @@ def set_up(self) -> None: self.monkeypatch.setattr(job_builder_module, 'AsyncHABAppScheduler', SyncScheduler) def tear_down(self) -> None: - - for rule in self.loaded_rules: - rule._habapp_ctx.unload_rule() self.loaded_rules.clear() # restore patched