Task
- Start here: Task Scheduler guide
- Common recipes:
Tasks,Interval Task,TaskRouter - Cluster-wide: gate per-process tasks with
TaskLockorLeaderElection.
grelmicro.task
Task.
TaskError
Bases: GrelmicroError
Base grelmicro Task error.
Tasks
Tasks(
*,
auto_start: bool = True,
tasks: list[Task] | None = None,
timezone: str | None = None,
shutdown_timeout: float | None = None,
env_prefix: str | None = None,
env_load: bool | None = None,
)
Bases: TaskRouter, Reconfigurable[TasksConfig]
Tasks.
Tasks class, the main entrypoint to manage scheduled tasks.
Supports live reconfiguration of shutdown_timeout via
reconfigure(new_config). A swap applies to the next shutdown, not
to a drain already under way. timezone is startup-only: changing
the zone under a running cron task would move which wall-clock fire
counts as due, and the durable last-fire state would either replay a
fire or swallow one. See
Live reconfiguration.
Initialize Tasks.
| PARAMETER | DESCRIPTION |
|---|---|
auto_start
|
Automatically start all tasks.
TYPE:
|
tasks
|
A list of tasks to be started.
TYPE:
|
timezone
|
The IANA timezone name every cron task uses. A cron task that passes its own Default:
TYPE:
|
shutdown_timeout
|
Seconds to let running tasks finish their current unit of work on shutdown before they are force-cancelled. On exit a stop signal is raised so tasks unwind as soon as their in-flight work completes; this only bounds how long a task stuck mid-work delays shutdown. Defaults to When unset and env reads are enabled, resolves from
TYPE:
|
env_prefix
|
Override the auto-derived environment variable prefix. Default:
TYPE:
|
env_load
|
Whether to read environment variables. When None (the default), follow the process-wide
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
SettingsValidationError
|
If a setting fails validation. |
timezone
property
timezone: str
The IANA timezone name cron tasks use unless they set their own.
from_config
classmethod
from_config(
config: TasksConfig,
*,
auto_start: bool = True,
tasks: list[Task] | None = None,
) -> Self
Construct a Tasks from a pre-built TasksConfig.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
The pre-built tasks configuration. Use this path when the configuration is assembled at
startup from a settings tree (for example YAML, Vault,
or a
TYPE:
|
auto_start
|
Automatically start all tasks.
TYPE:
|
tasks
|
A list of tasks to be started.
TYPE:
|
start
async
start() -> None
Start all tasks manually.
TasksConfig
Bases: BaseModel
Tasks Config.
timezone
class-attribute
instance-attribute
timezone: TimeZoneName = TimeZoneName(UTC_NAME)
IANA timezone every cron task uses unless it, or the TaskRouter holding it, sets its own.
shutdown_timeout
class-attribute
instance-attribute
shutdown_timeout: NonNegativeFloat = 30.0
Seconds to let running tasks finish their current unit of work on shutdown before they are force-cancelled.
TaskRouter
TaskRouter(
*,
tasks: list[Task] | None = None,
timezone: str | None = None,
)
Task Router.
TaskRouter class, used to group task schedules, for example to structure an app in
multiple files. It would then be included in the Tasks, or in another
TaskRouter.
Initialize the task router.
| PARAMETER | DESCRIPTION |
|---|---|
tasks
|
A list of tasks to be scheduled.
TYPE:
|
timezone
|
The IANA timezone name every cron task in this router uses. A cron task that passes its own
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
TimezoneError
|
If no timezone of that name can be loaded. |
tasks
property
tasks: list[Task]
List of scheduled tasks.
timezone
property
timezone: str | None
The timezone this router declares, or None when it declares none.
A router reports only what it was given. The timezone its tasks
end up using is resolved by the owning Tasks when it starts, and
a router does not read the value it would inherit.
add_task
add_task(task: Task) -> None
Add a task to the scheduler.
every
every(
*,
seconds: float | timedelta,
name: str | None = None,
lock: TaskLock | None = None,
leader: LeaderElection | None = None,
sync: LockPrimitive | None = None,
) -> Callable[
[Callable[..., Any | Awaitable[Any]]],
Callable[..., Any | Awaitable[Any]],
]
Decorate a function to run it on a fixed interval.
Supports three modes:
- Local: No
lockorleader, runs on every worker, every interval. - Distributed lock: Pass a
lockto run at most once per interval across all workers. - Leader-gated: Set
leaderto restrict execution to the leader worker (a lock is implied).
| PARAMETER | DESCRIPTION |
|---|---|
seconds
|
The duration between each task run. Accepts a number of seconds or a Accuracy is not guaranteed and may vary with system load. Consider the execution time of the task when setting the interval.
TYPE:
|
name
|
The name of the task. If None, a name will be generated automatically from the function.
TYPE:
|
lock
|
Optional distributed lock for at-most-once scheduling. Pass a
TYPE:
|
leader
|
Optional leader election for leader gating. When provided, the task only executes on the leader worker.
Implies distributed locking (a lock is automatically
configured with interval-aware defaults when no
TYPE:
|
sync
|
Optional resource-level synchronization primitive. Layered on top of any distributed scheduling chosen via
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FunctionTypeError
|
If the task name generation fails. |
ValueError
|
If seconds is less than or equal to 0. |
ValueError
|
If the lock lease_duration is less than seconds. |
cron
cron(
expr: str,
*,
timezone: str | None = None,
name: str | None = None,
misfire_grace_seconds: float | None = None,
backend: ScheduleBackend | None = None,
sync: LockPrimitive | None = None,
) -> Callable[
[Callable[..., Any | Awaitable[Any]]],
Callable[..., Any | Awaitable[Any]],
]
Decorate function to add it as a cron task.
Runs the task whenever the wall-clock time matches the cron expression in the given timezone.
Each fire is claimed against a durable last-fire state, so the task
runs at most once across every worker per fire. A fire missed while
every worker was down replays once on restart, bounded by
misfire_grace_seconds, and only the most recent missed fire runs.
Without a backend, the task runs on every worker, every fire.
The guarantee is at-most-once. A worker that claims a fire and then
crashes mid-run does not retry it, because the last-fire state already
advanced. Make the body idempotent, or wrap it with @retry, when
correctness depends on completion.
| PARAMETER | DESCRIPTION |
|---|---|
expr
|
The 5-field cron expression: Each field supports
TYPE:
|
timezone
|
The IANA timezone name used to compute fire times. When None (the default), the task takes the timezone
configured on the
TYPE:
|
name
|
The name of the task. If None, a name will be generated automatically from the function. Also used as the schedule name for the durable last-fire state.
TYPE:
|
misfire_grace_seconds
|
How late a missed fire may run when a worker comes back. A fire missed while every worker was down replays once on
restart only when now is within this many seconds of the fire.
Past the budget, the fire is dropped.
TYPE:
|
backend
|
The durable schedule backend. By default, resolves through the active
TYPE:
|
sync
|
Optional resource-level synchronization primitive. Wraps the body once this worker wins the fire. Use a
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FunctionTypeError
|
If the task name generation fails. |
CronError
|
If the cron expression is invalid. |
TimezoneError
|
If the timezone is not an IANA timezone name. |
include_router
include_router(router: TaskRouter) -> None
Include another router in this router.
| RAISES | DESCRIPTION |
|---|---|
TaskAddOperationError
|
If the tasks have already started. |
started
started() -> bool
Check if the task manager has started.
do_mark_as_started
do_mark_as_started() -> None
Mark the task manager as started.
Do not call this method directly. It is called by the task manager when the task manager is started.