Ir para o conteúdo

tempest_fastapi_sdk.admin.tasks

A fonte de dados do painel de tarefas do admin: agenda declarada e execuções persistidas.

« Admin, dados e API


tasks

The task panel's data source: declared schedule and persisted runs.

Two halves that already existed and had no screen. The schedule is read off the broker's own registry (:func:~tempest_fastapi_sdk.tasks.task_inventory), so it describes what the process would run, not what a queue holds. The runs come from a :class:~tempest_fastapi_sdk.tasks.JobStore, one row per unit of long work, written by the workers themselves.

What this panel deliberately cannot show is live queue state. TaskIQ exposes none, which is already recorded for the dead-letter panel: the screen answers "what is declared" and "what was persisted", never "how many messages are sitting in the broker right now".

Either half may be missing. A service given only a TaskQueue shows the schedule; given only a JobStore it shows the runs. A section with no source does not render, rather than rendering empty and implying there is nothing to see.

JobT module-attribute

JobT = TypeVar('JobT', bound='BaseJobModel')

The concrete job model the panel reads.

TaskTrigger dataclass

TaskTrigger(
    cron: str | None = None,
    cron_offset: str | timedelta | None = None,
    interval_seconds: float | None = None,
    run_at: datetime | None = None,
    extra: Mapping[str, Any] = dict(),
)

One declared trigger of a registered task.

A task may declare several, and a trigger is not always a cron: TaskIQ accepts cron, interval and time (one-shot) entries in the same list. Projecting that list onto a fixed pair of columns has lost a declaration three times in this panel's history, so the row carries the triggers as the registry declares them.

Attributes:

Name Type Description
cron str | None

The cron expression, for a cron trigger.

cron_offset str | timedelta | None

The timezone the expression is anchored to. A cron expression without one reads as complete while meaning UTC, which is why it is shown rather than dropped.

interval_seconds float | None

The interval in seconds, for an interval trigger.

run_at datetime | None

The instant, for a one-shot time trigger.

extra Mapping[str, Any]

Any other key on the entry, kept so a key TaskIQ adds later is reachable rather than silently dropped. Excluded from equality, and not rendered.

cron_offset_label property

cron_offset_label: str | None

The offset as display text.

Returns:

Type Description
str | None

str | None: "±HH:MM" for a fixed offset, the IANA key for

str | None

a named zone, or None when the trigger declares none.

ScheduledTask dataclass

ScheduledTask(
    name: str,
    cron: str | None,
    interval_seconds: float | None,
    retry_on_error: bool,
    max_retries: int | None,
    cron_offset: str | timedelta | None = None,
    run_at: datetime | None = None,
    triggers: tuple[TaskTrigger, ...] = (),
)

One registered task, as the schedule source will read it.

:attr:triggers is the whole declaration; the flat fields summarize the first trigger of each kind, so a caller that only needs "the cron" keeps reading one attribute.

Attributes:

Name Type Description
name str

The registered task name.

cron str | None

The first cron expression the task declares.

interval_seconds float | None

The first interval in seconds the task declares.

retry_on_error bool

Whether the task opts into retries.

max_retries int | None

The attempt cap, when the task set one.

cron_offset str | timedelta | None

The timezone paired with :attr:cron. Without it the expression reads as complete while meaning UTC.

run_at datetime | None

The instant, when the task declares a one-shot time trigger.

triggers tuple[TaskTrigger, ...]

Every declared trigger, in registry order. Empty for an on-demand task.

is_scheduled property

is_scheduled: bool

Whether the task runs on its own, rather than on demand.

Returns:

Name Type Description
bool bool

True when the task declares any trigger — a cron, an

bool

interval, or a one-shot instant.

cron_offset_label property

cron_offset_label: str | None

The offset paired with :attr:cron, as display text.

Returns:

Type Description
str | None

str | None: "±HH:MM" for a fixed offset, the IANA key for

str | None

a named zone, or None when none was declared.

TaskPanelService

TaskPanelService(
    *,
    queue: TaskQueue | None = None,
    job_store: JobStore[JobT] | None = None,
    recent_limit: int = 25,
)

Bases: Generic[JobT]

Reads what the admin task panel renders.

Neither half is required, and neither is invented: the schedule is the broker's registry and the runs are rows a worker wrote.

Example

panel = TaskPanelService(queue=tq, job_store=jobs) make_admin_router(site, db=db, auth_backend=backend, ... secret_key=secret, tasks=panel)

Attributes:

Name Type Description
recent_limit int

How many runs the list shows.

Configure the panel.

Parameters:

Name Type Description Default
queue TaskQueue | None

The queue whose registry the schedule section reads. None hides that section.

None
job_store JobStore[JobT] | None

The store the runs section reads. None hides that section.

None
recent_limit int

Rows in the runs list. Defaults to 25.

25

Raises:

Type Description
ValueError

When neither half is given — a panel with no source would render two empty sections and say nothing.

shows_schedule property

shows_schedule: bool

Whether the schedule section has a source.

Returns:

Name Type Description
bool bool

True when a queue was given.

shows_runs property

shows_runs: bool

Whether the runs section has a source.

Returns:

Name Type Description
bool bool

True when a job store was given.

schedule

schedule() -> list[ScheduledTask]

Return every registered task, scheduled ones first.

Reads the broker's registry, so it is the declared task set — no queue is contacted and nothing is executed.

The row carries the cron expression, not the next run time. Computing that is not free with what the SDK already depends on: pycron, which arrives with TaskIQ, exposes is_now and has_been and no next(), so a next-run column would mean sweeping candidate minutes on every render — up to ~44k iterations for a monthly cron — or taking a new dependency for one column.

Every declared trigger reaches the row. Reading a chosen pair of keys off the registry has dropped a declaration three times here — an interval shown as on demand, a one-shot time doing the same, and a cron_offset that made the panel state an hour three hours off what fires — so :attr:ScheduledTask.triggers carries the entries as declared and the flat fields summarize them.

Returns:

Type Description
list[ScheduledTask]

list[ScheduledTask]: Scheduled tasks first, then on-demand

list[ScheduledTask]

ones, each group by name. Empty when no queue was given.

recent_runs async

recent_runs(*, status: str | None = None) -> list[JobT]

Return the most recent job rows, newest first.

Parameters:

Name Type Description Default
status str | None

Restrict to one :class:~tempest_fastapi_sdk.tasks.JobStatusEnum value.

None

Returns:

Type Description
list[JobT]

list[JobT]: The rows, newest first. Empty when no job

list[JobT]

store was given, and empty when nothing matches — "no runs

list[JobT]

yet" is an answer, not a 404.

run async

run(job_id: UUID) -> JobT | None

Return one job row, or None when it is gone.

Parameters:

Name Type Description Default
job_id UUID

The row to read.

required

Returns:

Type Description
JobT | None

JobT | None: The job, or None when no store was

JobT | None

given or the row no longer exists.

cancel async

cancel(job_id: UUID) -> bool

Ask a queued or running job to stop.

Cancelling is cooperative: the store flips the row, and the worker notices at its next progress tick. A job already finished is left alone.

Parameters:

Name Type Description Default
job_id UUID

The row to cancel.

required

Returns:

Name Type Description
bool bool

True when the row moved to CANCELLED, False

bool

when there was nothing to stop.