From 6328f17307bd1850b48fde0b20ec6398b7988091 Mon Sep 17 00:00:00 2001 From: Robert Sivilli Date: Mon, 14 Aug 2023 07:30:45 -0400 Subject: [PATCH 01/15] initial cut --- twitchio/ext/routines/__init__.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/twitchio/ext/routines/__init__.py b/twitchio/ext/routines/__init__.py index 89d4867c..990e03dd 100644 --- a/twitchio/ext/routines/__init__.py +++ b/twitchio/ext/routines/__init__.py @@ -99,6 +99,7 @@ def __init__( self._args: tuple | None = None self._kwargs: dict | None = None + self.next_event_time:datetime.datetime|None = None #type : ignore def __get__(self, instance, owner): if instance is None: @@ -174,6 +175,8 @@ def cancel(self) -> None: Consider using :meth:`stop` if a graceful stop, which will complete the current iteration, is desired. """ if self._can_be_cancelled(): + self.next_event_time =None + self._task.cancel() if not self._restarting: @@ -319,7 +322,17 @@ def completed_iterations(self) -> int: def remaining_iterations(self) -> Optional[int]: """A count of remaining iterations.""" return self._remaining_iterations - + + @property + def time_till_execution(self) -> datetime.timedelta: + """Return the time left as a datetime object before the next execution.""" + + if self.next_event_time is None: + #this might be misleading... + return datetime.timedelta(seconds=0) + return max(self.next_event_time - datetime.datetime.now(),datetime.timedelta(seconds=0)) + + @property def start_time(self) -> Optional[datetime.datetime]: """The time the routine was started. @@ -351,10 +364,12 @@ async def _routine(self, *args, **kwargs) -> None: return self.cancel() if self._time: + self.next_event_time = self._time wait = compute_timedelta(self._time) await asyncio.sleep(wait) if self._wait_first and not self._time: + self.next_event_time = self._delta+datetime.datetime.now() await asyncio.sleep(self._delta) if self._remaining_iterations == 0: @@ -394,6 +409,7 @@ async def _routine(self, *args, **kwargs) -> None: sleep = max((start - datetime.datetime.now(datetime.timezone.utc)).total_seconds() + self._delta, 0) self._completed_loops += 1 + self.next_event_time = datetime.datetime.now() + datetime.timedelta(seconds=sleep) await asyncio.sleep(sleep) try: @@ -452,7 +468,7 @@ def routine( def decorator(coro: Callable) -> Routine: time_ = time - + if any((seconds, minutes, hours)) and time_: raise RuntimeError( "Argument