FastAPI-Cronsv2.4.0

Top-level imports

Everything below is importable from fastapi_crons. The SQL backends are the exception — see SQL backends.

python
from fastapi_crons import (
    # Core
    Crons,
    CronJob,
    CronConfig,
    cron_job,
    get_cron_router,
    # Retry and timeout
    RetryConfig,
    retry_on_failure,
    execute_with_retry,
    JobTimeoutError,
    # State
    SQLiteStateBackend,
    RedisStateBackend,
    # Locking
    DistributedLockManager,
    LocalLockBackend,
    RedisLockBackend,
    # Hooks
    log_job_start,
    log_job_success,
    log_job_error,
    alert_on_failure,
    alert_on_long_duration,
    webhook_notification,
    metrics_collector,
    # OpenTelemetry
    OpenTelemetryHooks,
    is_otel_available,
)

Crons

The scheduler. Constructing it registers the FastAPI lifecycle handlers.

python
Crons(
    app=None,
    state_backend=None,
    lock_manager=None,
    config=None,
)
app
FastAPI | None
Your application. Can be omitted and supplied later with init_app().
state_backend
StateBackend | None
Defaults to SQLiteStateBackend using the configured path.
lock_manager
DistributedLockManager | None
Built from config when distributed locking is enabled.
config
CronConfig | None
Defaults to CronConfig().

Methods

MethodReturnsDescription
init_app(app)NoneAttach to an app constructed later.
cron(expr, **kwargs)decoratorRegister a job. See Defining Jobs.
get_jobs()list[CronJob]Every registered job.
get_job(name)CronJob | NoneOne job by name.
add_before_run_hook(hook, job_name=None)CronsRegister a before-run hook.
add_after_run_hook(hook, job_name=None)CronsRegister an after-run hook.
add_on_error_hook(hook, job_name=None)CronsRegister an error hook.
start()coroutineStart the loops. Called on startup.
stop()coroutineStop the loops. Called on shutdown.

CronJob

A single registered job. Retrieve one with crons.get_job(name).

func
Callable
The function to run.
expr
str
The cron expression.
name
str
Unique job name.
tags
list[str]
Job tags.
next_run
datetime
When the job is next due.
MethodDescription
update_next_run()Recompute next_run.
add_before_run_hook(hook)Attach a hook to this job only.
add_after_run_hook(hook)Attach a hook to this job only.
add_on_error_hook(hook)Attach a hook to this job only.

CronConfig

Settings, read from the environment at construction. See Configuration for the full variable list and defaults.

Retry helpers

NameKindDescription
RetryConfigdataclassFields: max_retries, retry_delay, backoff_multiplier, max_delay, jitter, retry_on, on_retry.
retry_on_failure(...)decoratorApply retries to any callable.
execute_with_retry(...)coroutineRun a callable under a retry policy.
JobTimeoutErrorexceptionRaised when a job exceeds its timeout.

SQL backends

These live in submodules because they need the optional SQLAlchemy dependency. Importing them without the extra raises an ImportError naming the extra to install.

ClassImport from
SQLAlchemyStateBackendfastapi_crons.state.sqlalchemy
SQLModelStateBackendfastapi_crons.state.sqlalchemy (alias of the above)
SQLAlchemyLockBackendfastapi_crons.locking.sqlalchemy
PostgreSQLAdvisoryLockBackendfastapi_crons.locking.sqlalchemy

get_cron_router()

Returns the management APIRouter. See HTTP Endpoints.

Esc
IntroductionGetting StartedInstallationGetting StartedQuick StartGetting StartedCron ExpressionsCore ConceptsDefining JobsCore ConceptsHooksCore ConceptsConfigurationCore ConceptsState BackendsBackendsDistributed LockingBackendsDashboardOperationsHTTP EndpointsOperationsCLIOperations