Skip to content

Commit

Permalink
Timer: Allow setting a new interval in reset
Browse files Browse the repository at this point in the history
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
  • Loading branch information
Marenz committed Sep 19, 2024
1 parent 085fba0 commit 82986fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Frequenz channels Release Notes

## New Features

- `Timer.reset()` now supports setting the interval and will restart the timer with the new interval.

## Bug Fixes

- `FileWatcher`: Fixed `ready()` method to return False when an error occurs. Before this fix, `select()` (and other code using `ready()`) never detected the `FileWatcher` was stopped and the `select()` loop was continuously waking up to inform the receiver was ready.
Expand Down
12 changes: 11 additions & 1 deletion src/frequenz/channels/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,12 @@ def is_running(self) -> bool:
"""Whether the timer is running."""
return not self._stopped

def reset(self, *, start_delay: timedelta = timedelta(0)) -> None:
def reset(
self,
*,
interval: timedelta | None = None,
start_delay: timedelta = timedelta(0),
) -> None:
"""Reset the timer to start timing from now (plus an optional delay).
If the timer was stopped, or not started yet, it will be started.
Expand All @@ -595,6 +600,8 @@ def reset(self, *, start_delay: timedelta = timedelta(0)) -> None:
more details.
Args:
interval: The new interval between ticks. If `None`, the current
interval is kept.
start_delay: The delay before the timer should start. This has microseconds
resolution, anything smaller than a microsecond means no delay.
Expand All @@ -607,6 +614,9 @@ def reset(self, *, start_delay: timedelta = timedelta(0)) -> None:
if start_delay_ms < 0:
raise ValueError(f"`start_delay` can't be negative, got {start_delay}")

if interval is not None:
self._interval = _to_microseconds(interval)

self._next_tick_time = self._now() + start_delay_ms + self._interval

if self.is_running:
Expand Down

0 comments on commit 82986fa

Please sign in to comment.