tanjun.schedules#
Interface and interval implementation for a Tanjun based callback scheduler.
AbstractSchedule #
Abstract callback schedule class.
callback
property
abstractmethod
#
callback: _CallbackSig
Return the callback attached to the schedule.
This will be an asynchronous function which takes zero positional arguments, returns None and may be relying on dependency injection.
copy
abstractmethod
#
copy()
Copy the schedule.
RETURNS | DESCRIPTION |
---|---|
Self
|
The copied schedule. |
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If the schedule is active. |
force_stop
abstractmethod
#
force_stop()
Stop the schedule while cancelling any active tasks.
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If the schedule is not active. |
start
abstractmethod
#
start(client, /, *, loop=None)
Start the schedule.
PARAMETER | DESCRIPTION |
---|---|
client |
The injector client calls should be resolved with. |
loop |
The event loop to use. If not provided, the current event loop will be used.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If the scheduled callback is already running. If the current or provided event loop isn't running. |
stop
abstractmethod
async
#
stop()
Stop the schedule after waiting for any existing tasks to finish.
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If the scheduled callback isn't running. |
IntervalSchedule #
Bases: typing.Generic[_CallbackSigT]
, components.AbstractComponentLoader
, AbstractSchedule
A callback schedule with an interval between calls.
This should be loaded into a component using either Component.load_from_scope, Component.add_schedule or Component.with_schedule, and will be started and stopped with the linked tanjun client.
__init__ #
__init__(callback, interval, /, *, fatal_exceptions=(), ignored_exceptions=(), max_runs=None)
Initialise an interval schedule.
PARAMETER | DESCRIPTION |
---|---|
callback |
The callback for the schedule. This should be an asynchronous function which takes no positional arguments, returns None and may use dependency injection.
TYPE:
|
interval |
The interval between calls. Passed as a timedelta, or a number of seconds. |
fatal_exceptions |
A sequence of exceptions that will cause the schedule to stop if raised by the callback, start callback or stop callback.
TYPE:
|
ignored_exceptions |
A sequence of exceptions that should be ignored if raised by the callback, start callback or stop callback.
TYPE:
|
max_runs |
The maximum amount of times the schedule runs. |
set_fatal_exceptions #
set_fatal_exceptions(*exceptions)
set_ignored_exceptions #
set_ignored_exceptions(*exceptions)
Set the exceptions that a schedule will ignore.
If any of these exceptions are encountered, there will be nothing printed to console.
PARAMETER | DESCRIPTION |
---|---|
*exceptions |
Types of the exceptions to ignore. |
RETURNS | DESCRIPTION |
---|---|
Self
|
The schedule object to enable chained calls. |
set_start_callback #
set_start_callback(callback)
Set the callback executed before the schedule starts to run.
PARAMETER | DESCRIPTION |
---|---|
callback |
The callback to set.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
The schedule instance to enable chained calls. |
set_stop_callback #
set_stop_callback(callback)
Set the callback executed after the schedule is finished.
PARAMETER | DESCRIPTION |
---|---|
callback |
The callback to set.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
The schedule instance to enable chained calls. |
with_start_callback #
with_start_callback(callback)
Set the callback executed before the schedule is finished/stopped.
PARAMETER | DESCRIPTION |
---|---|
callback |
The callback to set.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
collections.abc.Callable[..., collections.abc.Coroutine[Any, Any, None]]
|
The added callback. |
Examples:
@component.with_schedule()
@tanjun.as_interval(1, max_runs=20)
async def interval():
global counter
counter += 1
print(f"Run #{counter}")
@interval.with_start_callback
async def pre():
print("pre callback")
with_stop_callback #
with_stop_callback(callback)
Set the callback executed after the schedule is finished.
PARAMETER | DESCRIPTION |
---|---|
callback |
The callback to set.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
collections.abc.Callable[..., collections.abc.Coroutine[Any, Any, None]]
|
The added callback. |
Examples:
@component.with_schedule()
@tanjun.as_interval(1, max_runs=20)
async def interval():
global counter
counter += 1
print(f"Run #{counter}")
@interval.with_stop_callback
async def post():
print("pre callback")
TimeSchedule #
Bases: typing.Generic[_CallbackSigT]
, components.AbstractComponentLoader
, AbstractSchedule
A schedule that runs at specific times.
This should be loaded into a component using either Component.load_from_scope, Component.add_schedule or Component.with_schedule and will be started and stopped with the linked tanjun client.
__init__ #
__init__(callback, /, *, months=(), weekly=False, days=(), hours=(), minutes=(), seconds=0, fatal_exceptions=(), ignored_exceptions=(), timezone=None)
Initialise the time schedule.
PARAMETER | DESCRIPTION |
---|---|
callback |
The callback for the schedule. This should be an asynchronous function which takes no positional arguments, returns None and may use dependency injection.
TYPE:
|
months |
Either one or multiple months the schedule shouldrun on. If this is not specified or an empty sequence then the schedule will run on all months.
TYPE:
|
weekly |
Whether the schedule should run on a weekly basis.
TYPE:
|
days |
Either one or multiple days the schedule should run on. When Otherwise this will refer to the days of the month ( If this is not specified or an empty sequence, then the schedule will on all days.
TYPE:
|
hours |
Either one or multiple hours the schedule should run on. If this is not specified or an empty sequence then the schedule will run on all hours.
TYPE:
|
minutes |
Either one or multiple minutes the schedule should run on. If this is not specified or an empty sequence then the schedule will run on all minutes.
TYPE:
|
seconds |
Either one or multiple seconds the schedule should run on. Defaults to the start of the minute if not specified or an empty sequence.
TYPE:
|
fatal_exceptions |
A sequence of exceptions that will cause the schedule to stop if raised by the callback, start callback or stop callback.
TYPE:
|
ignored_exceptions |
A sequence of exceptions that should be ignored if raised by the callback, start callback or stop callback.
TYPE:
|
timezone |
The timezone to use for the schedule. If this is not specified then the system's local timezone will be used. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
Raises a value error for any of the following reasons:
|
set_fatal_exceptions #
set_fatal_exceptions(*exceptions)
set_ignored_exceptions #
set_ignored_exceptions(*exceptions)
Set the exceptions that a schedule will ignore.
If any of these exceptions are encountered, there will be nothing printed to console.
PARAMETER | DESCRIPTION |
---|---|
*exceptions |
Types of the exceptions to ignore. |
RETURNS | DESCRIPTION |
---|---|
Self
|
The schedule object to enable chained calls. |
as_interval #
as_interval(interval, /, *, fatal_exceptions=(), ignored_exceptions=(), max_runs=None)
Decorator to create an schedule.
Examples:
@component.with_schedule
@tanjun.as_interval(datetime.timedelta(minutes=5)) # This will run every 5 minutes
async def interval(client: alluka.Injected[tanjun.abc.Client]) -> None:
...
This should be loaded into a component using either Component.with_schedule or Component.load_from_scope, and will be started and stopped with the linked tanjun client.
PARAMETER | DESCRIPTION |
---|---|
interval |
The interval between calls. Passed as a timedelta, or a number of seconds. |
fatal_exceptions |
A sequence of exceptions that will cause the schedule to stop if raised by the callback, start callback or stop callback.
TYPE:
|
ignored_exceptions |
A sequence of exceptions that should be ignored if raised by the callback, start callback or stop callback.
TYPE:
|
max_runs |
The maximum amount of times the schedule runs. |
RETURNS | DESCRIPTION |
---|---|
collections.Callable[[_CallbackSigT], tanjun.scheduling.IntervalSchedule[_CallbackSigT]]
|
The decorator used to create the schedule. This should be decorating an asynchronous function which takes no positional arguments, returns None and may use dependency injection. |
as_time_schedule #
as_time_schedule(*, months=(), weekly=False, days=(), hours=(), minutes=(), seconds=0, fatal_exceptions=(), ignored_exceptions=(), timezone=None)
Create a time schedule through a decorator call.
Examples:
@component.with_schedule
@tanjun.as_time_schedule( # This will run every week day at 8:00 and 16:00 UTC.
minutes=0, hours=[8, 16], days=range(0, 5), weekly=True, timezone=datetime.timezone.utc
)
async def interval(client: alluka.Injected[tanjun.abc.Client]) -> None:
...
This should be loaded into a component using either Component.with_schedule or Component.load_from_scope, and will be started and stopped with the linked tanjun client.
PARAMETER | DESCRIPTION |
---|---|
months |
Either one or multiple months the schedule should run on. If this is not specified or an empty sequence then the schedule will run on all months.
TYPE:
|
weekly |
Whether the schedule should run on a weekly basis.
TYPE:
|
days |
Either one or multiple days the schedule should run on. When Otherwise this will refer to the days of the month ( If this is not specified or an empty sequence, then the schedule will on all days.
TYPE:
|
hours |
Either one or multiple hours the schedule should run on. If this is not specified or an empty sequence then the schedule will run on all hours.
TYPE:
|
minutes |
Either one or multiple minutes the schedule should run on. If this is not specified or an empty sequence then the schedule will run on all minutes.
TYPE:
|
seconds |
Either one or multiple seconds the schedule should run on. Defaults to the start of the minute if not specified or an empty sequence.
TYPE:
|
fatal_exceptions |
A sequence of exceptions that will cause the schedule to stop if raised by the callback, start callback or stop callback.
TYPE:
|
ignored_exceptions |
A sequence of exceptions that should be ignored if raised by the callback, start callback or stop callback.
TYPE:
|
timezone |
The timezone to use for the schedule. If this is not specified then the system's local timezone will be used. |
RETURNS | DESCRIPTION |
---|---|
collections.Callable[[_CallbackSigT], tanjun.scheduling.TimeSchedule[_CallbackSigT]]
|
The decorator used to create the schedule. This should be decorating an asynchronous function which takes no positional arguments, returns None and may use dependency injection. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
Raises a value error for any of the following reasons:
|