Skip to content

Commit

Permalink
fixed rule lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Dec 9, 2024
1 parent 255e5c8 commit 8da2b59
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 22 deletions.
4 changes: 2 additions & 2 deletions run/conf_testing/rules/habapp/test_event_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand Down
41 changes: 31 additions & 10 deletions run/conf_testing/rules/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -18,26 +22,43 @@ 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()

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()
4 changes: 2 additions & 2 deletions src/HABApp/rule_ctx/rule_ctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/HABApp/rule_manager/rule_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ 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:
return None

# 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
Expand Down
6 changes: 3 additions & 3 deletions src/HABApp/rule_manager/rule_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
3 changes: 0 additions & 3 deletions tests/rule_runner/rule_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8da2b59

Please sign in to comment.