From bc76d7e7404a3d032d63f13e49d2ff9054de4e63 Mon Sep 17 00:00:00 2001 From: "Mathias L. Baumann" Date: Tue, 24 Sep 2024 18:05:59 +0200 Subject: [PATCH] fixup! Fix wrong running --- src/frequenz/dispatch/_dispatch.py | 8 ++++---- src/frequenz/dispatch/actor.py | 8 +++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/frequenz/dispatch/_dispatch.py b/src/frequenz/dispatch/_dispatch.py index 7d6bdad..08fe977 100644 --- a/src/frequenz/dispatch/_dispatch.py +++ b/src/frequenz/dispatch/_dispatch.py @@ -119,11 +119,11 @@ def running(self, type_: str) -> RunningState: now = datetime.now(tz=timezone.utc) - # A dispatch without duration is always running once it started - if self.duration is None: - if self.start_time <= now: - return RunningState.RUNNING + if now < self.start_time: return RunningState.STOPPED + # A dispatch without duration is always running once it started + elif self.duration is None: + return RunningState.RUNNING if until := self._until(now): return RunningState.RUNNING if now < until else RunningState.STOPPED diff --git a/src/frequenz/dispatch/actor.py b/src/frequenz/dispatch/actor.py index 104ae57..bb47e77 100644 --- a/src/frequenz/dispatch/actor.py +++ b/src/frequenz/dispatch/actor.py @@ -130,6 +130,8 @@ async def _execute_scheduled_event(self, dispatch: Dispatch) -> None: else: self._schedule_start(dispatch) + self._update_timer() + async def _fetch(self) -> None: """Fetch all relevant dispatches using list. @@ -234,10 +236,14 @@ async def _update_dispatch_schedule_and_notify( self._schedule_start(dispatch) # We modified the schedule, so we need to reset the timer + self._update_timer() + + def _update_timer(self) -> None: + """Update the timer to the next event.""" if self._scheduled_events: - _logger.debug("Next event scheduled at %s", self._scheduled_events[0][0]) due_at: datetime = self._scheduled_events[0][0] self._next_event_timer.reset(interval=due_at - datetime.now(timezone.utc)) + _logger.debug("Next event scheduled at %s", self._scheduled_events[0][0]) def _remove_scheduled(self, dispatch: Dispatch) -> bool: """Remove a dispatch from the scheduled events.