Tasks API

Task

Task definition.

Constructor

Task(name: str, func: Callable, *, retry_policy: RetryPolicy | None = None, timeout: int | None = None, queue: str = "default", priority: int = 0)

Methods

  • async run(*args, **kwargs) -> Any — Run the task
  • cancel() -> None — Cancel the task
  • to_result() -> TaskResult — Convert to TaskResult

Properties

  • id — Task ID
  • name — Task name
  • status — Task status
  • created_at — Created timestamp
  • started_at — Started timestamp
  • completed_at — Completed timestamp
  • retry_count — Retry count

TaskStatus

Task status enum.

Value Description
PENDINGTask is pending
RUNNINGTask is running
COMPLETEDTask completed
FAILEDTask failed
RETRYTask will retry
CANCELLEDTask cancelled
TIMEOUTTask timed out

TaskQueue

Task queue.

Constructor

TaskQueue(name: str = "default", max_size: int = 1000)

Methods

  • async push(task) — Push a task to the queue
  • async pop(timeout) — Pop a task from the queue
  • async get(task_id) — Get a task by ID
  • async remove(task_id) — Remove a task
  • async cancel(task_id) — Cancel a task
  • async clear() — Clear the queue
  • async size() — Get the queue size
  • async pending_count() — Get pending count
  • async running_count() — Get running count
  • async completed_count() — Get completed count
  • async failed_count() — Get failed count

Worker

Task worker.

Constructor

Worker(registry: TaskRegistry, queue: TaskQueue | None = None, concurrency: int = 10, queue_name: str = "default")

Methods

  • async start() — Start the worker
  • stop() — Stop the worker
  • is_running() — Check if running

Scheduler

Task scheduler.

Constructor

Scheduler(queue: TaskQueue)

Methods

  • schedule(task, delay=None, at=None, interval=None) — Schedule a task
  • async start() — Start the scheduler
  • async stop() — Stop the scheduler

RetryPolicy

Retry policy.

Constructor

RetryPolicy(max_retries: int = 3, delay: float = 1.0, backoff: float = 2.0, max_delay: float = 60.0, random_jitter: float = 0.1, retry_on: list[type[Exception]] | None = None)

Methods

  • should_retry(retry_count, error) — Check if retry should be attempted
  • get_delay(retry_count) — Get the delay before retry
CLI Command

Start a worker: flaxon worker app:app --concurrency 4