diff options
| author | Josh B <[email protected]> | 2020-01-21 15:07:50 +1000 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2020-01-21 03:18:25 -0500 |
| commit | d7e925eb8943e1bcc169f896abef473b48d2962e (patch) | |
| tree | 32cb3fd5062ec7a0e05267e5df1a222831bb3020 | |
| parent | [tasks] Add Loop.next_iteration property (diff) | |
| download | discord.py-d7e925eb8943e1bcc169f896abef473b48d2962e.tar.xz discord.py-d7e925eb8943e1bcc169f896abef473b48d2962e.zip | |
[tasks] Fix issue with next_iteration when task overruns time allotted
| -rw-r--r-- | discord/ext/tasks/__init__.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/discord/ext/tasks/__init__.py b/discord/ext/tasks/__init__.py index 41a96bbe..e9d1e177 100644 --- a/discord/ext/tasks/__init__.py +++ b/discord/ext/tasks/__init__.py @@ -46,8 +46,8 @@ class Loop: raise ValueError('count must be greater than 0 or None.') self.change_interval(seconds=seconds, minutes=minutes, hours=hours) - self._last_iteration = datetime.datetime.now(datetime.timezone.utc) - self._next_iteration = self._get_next_sleep_time() + self._last_iteration = None + self._next_iteration = None if not inspect.iscoroutinefunction(self.coro): raise TypeError('Expected coroutine function, not {0.__name__!r}.'.format(type(self.coro))) @@ -65,12 +65,16 @@ class Loop: async def _loop(self, *args, **kwargs): backoff = ExponentialBackoff() await self._call_loop_function('before_loop') + self._next_iteration = datetime.datetime.now(datetime.timezone.utc) try: while True: self._last_iteration = self._next_iteration self._next_iteration = self._get_next_sleep_time() try: await self.coro(*args, **kwargs) + now = datetime.datetime.now(datetime.timezone.utc) + if now > self._next_iteration: + self._next_iteration = now except self._valid_exception as exc: if not self.reconnect: raise @@ -329,7 +333,7 @@ class Loop: async def _sleep_until(self, dt): now = datetime.datetime.now(datetime.timezone.utc) delta = (dt - now).total_seconds() - await asyncio.sleep(delta) + await asyncio.sleep(max(delta, 0)) def _get_next_sleep_time(self): return self._last_iteration + datetime.timedelta(seconds=self._sleep) |