Getting Started
Installation
Installation is as simple as:
pip install fastapi-apscheduler4
rye add fastapi-apscheduler4
poetry add fastapi-apscheduler4
FastAPI-APScheduler4 has the following main dependencies:
This is not intended to use with earlier versions of APScheduler or Pydantic. The goals is to provide the latest features.
Optional Dependencies
If you are running FastAPI-APScheduler4 in multiple nodes (i.e. Kubernetes, Docker Swarm, etc.), you must have a shared database to store the scheduled tasks and a event broker to distribute the tasks between the nodes. Otherwise, the tasks will be executed multiple times.
Currently the following dependencies are supported:
- SQLAlchemy: Needed for APScheduler data store.
- Redis: Needed for APScheduler event broker.
pip install sqlalchemy redis
rye add sqlalchemy redis
poetry add sqlalchemy redis
Basic Usage
Quick start with configuration from environment variables:
from fastapi import FastAPI
from fastapi_apscheduler4 import SchedulerApp
scheduler_app = SchedulerApp()
app = FastAPI(lifespan=scheduler_app.lifespan)
@scheduler_app.interval(seconds=5)
def say_hello_world() -> None:
print("test1")
Configuration
The following configuration options are available.
General:
SCHEDULER_AUTO_START
: IfTrue
, the scheduler will start automatically with FastAPI. Default isTrue
.SCHEDULER_EVENT_BROKER
: The event broker to use. By default, it will be selected automatically.SCHEDULER_DATA_STORE
: The data store to use. By default, it will be selected automatically.
Scheduler API:
SCHEDULER_API_ENABLED
: IfTrue
, the API routes will be added. Default isTrue
.SCHEDULER_API_PREFIX
: The API prefix. Default is/api/v1/
.SCHEDULER_API_TAGS
: The API tags. Default isscheduler
.SCHEDULER_API_LIMIT_DEFAULT
: The API pagination default limit. Default is100
.SCHEDULER_API_LIMIT_MAX
: The API pagination max limit. Default is1000
.SCHEDULER_API_INCLUDE_IN_SCHEMA
: IfTrue
, the API will be included in the schema. Default isTrue
.
PostgreSQL:
POSTGRES_HOST
: The PostgreSQL host.POSTGRES_PORT
: The PostgreSQL port. Default is5432
.POSTGRES_USER
orPOSTGRES_USERNAME
: The PostgreSQL user.POSTGRES_PASSWORD
: The PostgreSQL password.POSTGRES_DB
: The PostgreSQL database.
Redis:
REDIS_HOST
: The Redis host.REDIS_PORT
: The Redis port. Default is6379
.REDIS_USER
orREDIS_USERNAME
: The Redis user.REDIS_PASSWORD
: The Redis password.REDIS_DB
: The Redis database.SCHEDULER_REDIS_CHANNEL
: The Redis channel. Default isapscheduler
.
The configuration models are available in the fastapi_apscheduler.config
module.
SchedulerConfig
: The main configuration model.SchedulerAPIConfig
: The API configuration model.PostgresConfig
: The PostgreSQL configuration model.RedisConfig
: The Redis configuration model.
You can use these models to create your own configuration.
Example:
from fastapi import FastAPI
from fastapi_apscheduler4 import SchedulerConfig
from fastapi_apscheduler4.app import SchedulerApp
from fastapi_apscheduler4.config import (
DataStoreType,
EventBrokerType,
PostgresConfig,
RedisConfig,
SchedulerAPIConfig,
)
from pydantic import SecretStr
scheduler_config = SchedulerConfig(
auto_start=True,
event_broker=EventBrokerType.REDIS,
data_store=DataStoreType.POSTGRES,
)
redis_config = RedisConfig(
host="localhost",
username="redis",
password=SecretStr("postgres"),
db=0,
)
postgres = PostgresConfig(
host="localhost",
username="postgres",
password=SecretStr("postgres"),
db="scheduler",
)
api_config = SchedulerAPIConfig(
enabled=True,
prefix="/",
tags=["scheduler"],
include_in_schema=True,
)
scheduler_app = SchedulerApp(scheduler=scheduler_config, redis=redis_config, postgres=postgres, api=api_config)
app = FastAPI(lifespan=scheduler_app.lifespan)