Skip to content
Snippets Groups Projects
Commit f4706e33 authored by David Trattnig's avatar David Trattnig
Browse files

refact: executor update logic

parent 7972e78e
No related branches found
No related tags found
No related merge requests found
......@@ -47,6 +47,7 @@ class EngineExecutor(Timer):
child_timer: Timer = None
timer_id: str = None
timer_type: str = None
update_count: int = None
func: func = None
param = None
diff = None
......@@ -85,7 +86,7 @@ class EngineExecutor(Timer):
self.direct_exec = False
self.is_aborted = False
self.timer_type = timer_type
self.timer_id = f"{timer_type}:{func.__name__}:{due_time}"
self.update_count = 0
diff = 0
if due_time:
......@@ -93,6 +94,8 @@ class EngineExecutor(Timer):
self.diff = diff
self.dt = datetime.now() + timedelta(seconds=diff)
dt_str = SU.round_seconds(self.dt).strftime("%Y-%m-%d_%H:%M:%S")
self.timer_id = f"{timer_type}:{func.__name__}:{dt_str}"
self.func = func
self.param = param
......@@ -149,7 +152,7 @@ class EngineExecutor(Timer):
"""
Do timed execution in a thread.
This method instroduces a slight delay to ensure the thread is properly initialized before
This method introduces a slight delay to ensure the thread is properly initialized before
starting it.
It also assigns the `timer_id` as the thread name.
......@@ -197,20 +200,20 @@ class EngineExecutor(Timer):
self.logger.debug(msg)
return False
# Still waiting for execution -> update
# Only update living timer when there's no completed parent
elif self.parent_timer and not self.parent_timer.is_alive():
self.logger.debug("Parent finished, leave the existing child alone.")
return False
# Parent and child are still waiting for execution -> update
else:
msg = f"Cancelling existingTimer with ID: {self.timer_id}"
msg = f"Cancelling existing timer with ID: {self.timer_id}"
self.logger.debug(msg)
existing_command.cancel()
if existing_command.child_timer:
msg = (
f"Cancelling existingTimer:childTimer with ID:"
f" {existing_command.child_timer.timer_id}"
)
self.logger.debug(msg)
self.update_count = existing_command.update_count + 1
EngineExecutor.timer_store[self.timer_id] = self
self.logger.debug(f"Created command timer with ID: {self.timer_id}")
self.logger.debug(f"Stored command timer with ID: {self.timer_id}")
return True
def is_alive(self):
......@@ -229,7 +232,8 @@ class EngineExecutor(Timer):
"""
Make a String representation of the timer.
"""
return f"[{self.timer_id}] exec at {str(self.dt)} (alive: {self.is_alive()})"
return f"[{self.timer_id}] exec at {str(self.dt)} \
(alive: {self.is_alive()}, updates: {self.update_count})"
@staticmethod
def remove_stale_timers():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment