83 Commits

Author SHA1 Message Date
Rapptz
3ff88db768 Update pyright to 1.1.289 2023-01-16 21:57:54 -05:00
rdrescher909
01915fbc09
[tasks] Clarify Loop.stop interaction with before_loop 2022-10-19 21:36:05 -04:00
Omkaar
c30a2f3ca0
Refactor certain f-strings and literals 2022-10-02 17:50:56 -04:00
Ionite
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
Bryan Forbes
d707019348
Bump Pyright to 1.1.265, fix type errors, and remove unnecessary ignores 2022-08-04 22:46:02 -04:00
Rapptz
903e2e64e9 [tasks] Only update the time interval if the body has run once
Fix #8151
2022-06-14 07:41:36 -04:00
Rapptz
53685b9b86 Change stderr prints to use the logging module instead 2022-06-13 01:06:15 -04:00
Lilly Rose Berner
15a6a04622
[tasks] Fix Task.failed() only being True while error handler runs 2022-04-18 06:22:53 -04:00
Rapptz
abcec5da9d [tasks] Use the local timezone when comparing for the start time
Fix #7676
2022-03-14 04:54:56 -04:00
Stocker
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
Rapptz
603681940f [tasks] Only correct for clock drift if an explicit time is given 2022-03-13 22:51:10 -04:00
Rapptz
f2586e9fe7 [tasks] Handle imaginary or ambiguous times due to DST transitions 2022-03-13 22:45:18 -04:00
Rapptz
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
Rapptz
64c6639f4b [tasks] Handle loop functions running multiple times due to clock drift 2022-03-13 11:36:00 -04:00
Rapptz
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
Han Seung Min - 한승민
93af158b0c
Refactor loop code to allow usage of asyncio.run 2022-03-13 04:54:14 -04:00
Sebastian Law
04535e4e1d
[tasks] use None instead of MISSING for internal attributes 2022-03-10 23:12:22 -05:00
Lilly Rose Berner
b308b54b89
[tasks] Fix change_interval raising when called during execution 2022-03-07 17:35:42 -05:00
Chrovo
aa725f4a4a
[docs] Fix typo in tasks docs 2022-03-06 19:03:36 -05:00
Sebastian Law
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
Sebastian Law
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
Sebastian Law
19ad64adda
[tasks] Fix initial loop execution running prematurely 2022-02-21 20:45:03 -05:00
Rapptz
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
Nadir Chowdhury
a2a7b0f076
[tasks] Improve typing parity 2021-08-27 17:18:15 -04:00
Rapptz
ea2d972666 Make global log variable in modules private 2021-08-22 02:33:51 -04:00
NiumXp
76c9e390f1
remove repeat 'to' in Task.restart doc 2021-06-27 23:42:43 -04:00
Josh
42a538edda
[tasks] Replace None check with MISSING check in task loop 2021-05-15 02:10:00 -04:00
Josh
ef22178dee
[tasks] Type hint the tasks extension 2021-05-12 06:31:40 -04:00
Sebastian Law
f5727ff0d0
[tasks] fix regular task loops 2021-05-10 20:25:16 -04:00
Sebastian Law
8bc489dba8
[tasks] Add support for explicit time parameter 2021-05-09 23:27:43 -04:00
Steve C
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
Rapptz
99fc950510 Use f-strings in more places that were missed. 2021-04-08 06:02:47 -04:00
Nadir Chowdhury
89456022cf
Add __all__ to remaining modules 2021-04-07 02:30:32 -04:00
Rapptz
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
Sebastian Law
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
Nadir Chowdhury
63ec23bac2
Code optimisations and refactoring via Sourcery 2021-02-24 21:26:51 -05:00
Sebastian Law
ceab8ff638
[tasks] make __call__ actually appear in the docs 2021-02-21 07:19:10 -05:00
Nihaal Sangha
69bdc3a184
Change copyright year to present 2021-01-15 05:28:11 -05:00
Rapptz
6515f33978 [tasks] Fix a typo in documentation 2020-12-14 22:18:40 -05:00
Rapptz
0216db0c0a [tasks] Add support for manually calling the wrapped coroutine 2020-12-14 22:11:37 -05:00
Rapptz
ccdf4c4ad6 [tasks] Lazily fetch event loop if one isn't provided
Fixes #5808
2020-09-10 00:00:58 -04:00
Josh
6baacb2c23
[tasks] Don't update _next_iteration on retry 2020-08-28 23:12:07 -04:00
Dan Hess
fc951873a1
[tasks] Remove HTTPException as an exception to silently continue for 2020-08-05 03:09:04 -04:00
Rapptz
b8154e365f Rewrite gateway to use aiohttp instead of websockets 2020-07-25 09:59:38 -04:00
James
61216112d0 Add a licence and encoding declaration to missing files 2020-07-08 23:01:00 -04:00
Josh
6b5be39cd2
[tasks] Fix issue with default error handler in class context 2020-06-28 03:53:29 -04:00
Steve C
4b3a7fbe16 [tasks] Allow Loop.cancel in Loop.before_loop
Task cancel raises on the next awaited coro, so I've added this 0-sleep "hack"

I'm internally debating if leaving the comment there, but I'm sure it would confuse the uninformed of this trick.
2020-05-10 17:00:23 -04:00
Steve C
0fd5eca0d5 [tasks] Fix tasks decorators being discarded
At this moment, when a task seems to be first loaded, it immediately throws away the decorators you give it, and just generates a new instance of itself.

In your cog's `__init__`, once you do `self.my_task.start()`, the Loop is remade when it gets to `self.my_task` before executing the `start` function. The original Loop that the cog starts with is where the decorated values are. This fixes that.
2020-05-07 02:30:24 -04:00
Rapptz
540a88b762 [tasks] Replace stack-based implementation with a cleaner one. 2020-04-14 04:56:52 -04:00
Rapptz
d5211fb327 [tasks] Create different Loop objects for different instances
Fixes #2294
2020-04-14 04:35:49 -04:00