Commit Graph

89 Commits

Author SHA1 Message Date
ab6efd02bc [tasks] Log handled exceptions before sleeping
Fix #10276
2025-08-22 19:59:56 -04:00
d00ac622bc Remove uses of deprecated Logger.warn method 2025-08-22 19:52:12 -04:00
f2aa0b833c [tasks] Fix race condition with set_result 2025-01-15 18:18:02 -05:00
9b9c93ad49 [tasks] Fix typing for tasks.SleepHandle.future set calls 2023-12-10 11:38:55 -05:00
646ab85bb6 [tasks] Fix missing name parameter in loop copy 2023-06-26 05:51:04 -04:00
03140c0163 [tasks] Add name parameter to give the internal task a name 2023-06-26 04:31:36 -04:00
3ff88db768 Update pyright to 1.1.289 2023-01-16 21:57:54 -05:00
01915fbc09 [tasks] Clarify Loop.stop interaction with before_loop 2022-10-19 21:36:05 -04:00
c30a2f3ca0 Refactor certain f-strings and literals 2022-10-02 17:50:56 -04:00
6981eb69c4 Normalize type formatting in TypeError
Normalize most mixed usages of `__class__`, `__class__!r`, 
`__class__.__name__!r` to the standard form of 
`__class__.__name__`
2022-09-12 15:25:55 -04:00
d707019348 Bump Pyright to 1.1.265, fix type errors, and remove unnecessary ignores 2022-08-04 22:46:02 -04:00
903e2e64e9 [tasks] Only update the time interval if the body has run once
Fix #8151
2022-06-14 07:41:36 -04:00
53685b9b86 Change stderr prints to use the logging module instead 2022-06-13 01:06:15 -04:00
15a6a04622 [tasks] Fix Task.failed() only being True while error handler runs 2022-04-18 06:22:53 -04:00
abcec5da9d [tasks] Use the local timezone when comparing for the start time
Fix #7676
2022-03-14 04:54:56 -04:00
5aa696ccfa Fix typing issues and improve typing completeness across the library
Co-authored-by: Danny <Rapptz@users.noreply.github.com>
Co-authored-by: Josh <josh.ja.butt@gmail.com>
2022-03-13 23:52:10 -04:00
603681940f [tasks] Only correct for clock drift if an explicit time is given 2022-03-13 22:51:10 -04:00
f2586e9fe7 [tasks] Handle imaginary or ambiguous times due to DST transitions 2022-03-13 22:45:18 -04:00
a339e01047 [tasks] Compare using full datetime rather than sole time
Comparisons using just the time object without an attached date
are pretty buggy and incomplete -- comparisons only make sense when
given a particular instant of time.

Ref: #7676
2022-03-13 20:45:05 -04:00
64c6639f4b [tasks] Handle loop functions running multiple times due to clock drift 2022-03-13 11:36:00 -04:00
6a43d60acf [tasks] Refactor tasks to not store a time index state
It's better to recompute it every time rather than suffer from
maintaining the extra state.
2022-03-13 06:20:44 -04:00
93af158b0c Refactor loop code to allow usage of asyncio.run 2022-03-13 04:54:14 -04:00
04535e4e1d [tasks] use None instead of MISSING for internal attributes 2022-03-10 23:12:22 -05:00
b308b54b89 [tasks] Fix change_interval raising when called during execution 2022-03-07 17:35:42 -05:00
aa725f4a4a [docs] Fix typo in tasks docs 2022-03-06 19:03:36 -05:00
5439a67056 [tasks] Fix sleep handling behaviour depending on interval type
Relative time intervals can be thought of as:

  for _ in range(count):
    await body()
    await asyncio.sleep(interval)

While explicit time intervals should be thought of as:

  times = [1pm, 2pm, 3pm, 12am]
  current = 0
  for _ in range(count):
    time = times.wrapping_index(current)  # magic to wrap around
    await utils.sleep_until(time)
    await body()
    current += 1
2022-03-05 05:12:22 -05:00
f1ac25809c [tasks] Fix behavior when task overruns interval
In a scenario with `tasks.loop(seconds=5)`:

The task takes 30 seconds to run on the first two iterations, and then
is nearly instant for iterations afterward. The behavior should be
that the task runs at:

t = 0  (on time)
t = 30 (late, should've been at t = 5)
t = 60 (late, should've been at t = 10)
t = 60 (late, should've been at t = 15)
t = 60 (late, should've been at t = 20)
t = 60 (late, should've been at t = 25)
... 6 more iterations
t = 60 (on time)
t = 65 (on time)

In a scenario with a loop with explicit times set at UTC 1pm, 2pm, 
3pm, 4pm, and 5pm:

- The task takes 6 hour to run on the first iteration, and then is
  nearly instant for iterations afterward. Assuming the task is started
  at noon, the behavior should be that the task runs at `t = 0` and
  then at `t = 3600` 4 times ("catching up" on the missed iterations
  at 2pm, 3pm, 4pm, and 5pm).

- The task takes 30 days to run on the first iteration, and then is
  nearly instant for iterations afterward. Assuming the task is started
  at noon, the behavior should be that the task runs at `t = 0` and
  then at `t = 43200` 149 times ("catching up" on the missed
  iterations for the past month).

This behavior should be documented in the ext.tasks docs
2022-02-26 01:48:27 -05:00
19ad64adda [tasks] Fix initial loop execution running prematurely 2022-02-21 20:45:03 -05:00
88b520b5ab Reformat code using black
Segments where readability was hampered were fixed by appropriate
format skipping directives. New code should hopefully be black
compatible. The moment they remove the -S option is probably the moment
I stop using black though.
2022-02-20 08:04:58 -05:00
a2a7b0f076 [tasks] Improve typing parity 2021-08-27 17:18:15 -04:00
ea2d972666 Make global log variable in modules private 2021-08-22 02:33:51 -04:00
76c9e390f1 remove repeat 'to' in Task.restart doc 2021-06-27 23:42:43 -04:00
42a538edda [tasks] Replace None check with MISSING check in task loop 2021-05-15 02:10:00 -04:00
ef22178dee [tasks] Type hint the tasks extension 2021-05-12 06:31:40 -04:00
f5727ff0d0 [tasks] fix regular task loops 2021-05-10 20:25:16 -04:00
8bc489dba8 [tasks] Add support for explicit time parameter 2021-05-09 23:27:43 -04:00
ef9bb79e91 [tasks] Move the Loop's sleep to be before exit conditions
This change makes it more so that `Loop.stop()` gracefully makes the
current iteration the final one, by waiting AND THEN returning.
The current implementation is closer to `cancel`, while also not.

I encountered this because I was trying to run a
`@tasks.loop(count=1)`, and inside it I print some text and change the
interval, and in an `after_loop`, I restart the loop.

Without this change, it immediately floods my console, due to
not waiting before executing `after_loop`.
2021-04-16 22:35:18 -04:00
99fc950510 Use f-strings in more places that were missed. 2021-04-08 06:02:47 -04:00
89456022cf Add __all__ to remaining modules 2021-04-07 02:30:32 -04:00
9d39b135f4 Modernize code to use f-strings
This also removes the encoding on the top, since Python 3 does it by
default. It also changes some methods to use `yield from`.
2021-04-04 07:03:53 -04:00
f7a4bef4ff [tasks] remove redundant condition in Loop.next_iteration
self._task is only None if the Loop has never been started before, 
which means None should be returned always, regardless of how
many seconds was passed into the constructor

this didn't break anything before because self._next_iteration will
be None as well if self._task is None.
2021-02-28 23:54:44 -05:00
63ec23bac2 Code optimisations and refactoring via Sourcery 2021-02-24 21:26:51 -05:00
ceab8ff638 [tasks] make __call__ actually appear in the docs 2021-02-21 07:19:10 -05:00
69bdc3a184 Change copyright year to present 2021-01-15 05:28:11 -05:00
6515f33978 [tasks] Fix a typo in documentation 2020-12-14 22:18:40 -05:00
0216db0c0a [tasks] Add support for manually calling the wrapped coroutine 2020-12-14 22:11:37 -05:00
ccdf4c4ad6 [tasks] Lazily fetch event loop if one isn't provided
Fixes #5808
2020-09-10 00:00:58 -04:00
6baacb2c23 [tasks] Don't update _next_iteration on retry 2020-08-28 23:12:07 -04:00
fc951873a1 [tasks] Remove HTTPException as an exception to silently continue for 2020-08-05 03:09:04 -04:00
b8154e365f Rewrite gateway to use aiohttp instead of websockets 2020-07-25 09:59:38 -04:00