Route builders¶
The four route builders return an APIRoute subclass to pass as route_class= to an APIRouter. Endpoints
registered on that router gain .delay/.options (delayed) or .scheduler (scheduled) methods.
fastapi_gcp_tasks.delayed_route.DelayedRouteBuilder ¶
DelayedRouteBuilder(
*,
base_url: str,
queue_path: str,
task_create_timeout: float = 10.0,
pre_create_hook: DelayedTaskHook | None = None,
client: CloudTasksClient | None = None,
auto_create_queue: bool = True,
) -> type[APIRoute]
Returns a Mixin that should be used to override route_class.
It adds a .delay and .options methods to the original endpoint.
Example:
delayed_router = APIRouter(route_class=DelayedRouteBuilder(...), prefix="/delayed")
class UserData(BaseModel):
name: str
@delayed_router.post("/on_user_create/{user_id}")
@as_delayed_task # optional: makes .delay visible to type checkers
def on_user_create(user_id: str, data: UserData):
# do work here
# Return values are meaningless
# Call .delay to trigger
on_user_create.delay(user_id="007", data=UserData(name="Piyush"))
app.include_router(delayed_router)
Source code in fastapi_gcp_tasks/delayed_route.py
fastapi_gcp_tasks.async_delayed_route.AsyncDelayedRouteBuilder ¶
AsyncDelayedRouteBuilder(
*,
base_url: str,
queue_path: str,
task_create_timeout: float = 10.0,
pre_create_hook: DelayedTaskHook | None = None,
client: CloudTasksAsyncClient
| AsyncCloudTasksClientFactory
| None = None,
auto_create_queue: bool = False,
) -> type[APIRoute]
Returns a Mixin that should be used to override route_class, with an awaitable .delay.
It adds awaitable .delay and sync .options methods to the original endpoint.
client may be a CloudTasksAsyncClient, a zero-argument factory returning one,
or None (default client). grpc.aio channels bind to the event loop active at
construction, so the client is resolved lazily on the first awaited .delay()
inside the running loop; pass a factory if your client needs custom construction
(e.g. the emulator).
Unlike DelayedRouteBuilder, auto_create_queue defaults to False so no
unexpected RPCs run inside request handlers. Either pass
auto_create_queue=True to lazily ensure the queue on the first
.delay(), or call fastapi_gcp_tasks.utils.ensure_queue_async from your
FastAPI lifespan (recommended).
Example:
async_delayed_router = APIRouter(route_class=AsyncDelayedRouteBuilder(...), prefix="/delayed")
class UserData(BaseModel):
name: str
@async_delayed_router.post("/on_user_create/{user_id}")
@as_async_delayed_task # optional: makes .delay visible to type checkers
async def on_user_create(user_id: str, data: UserData):
# do work here
# Return values are meaningless
# Await .delay to trigger
await on_user_create.delay(user_id="007", data=UserData(name="Piyush"))
app.include_router(async_delayed_router)
Source code in fastapi_gcp_tasks/async_delayed_route.py
fastapi_gcp_tasks.scheduled_route.ScheduledRouteBuilder ¶
ScheduledRouteBuilder(
*,
base_url: str,
location_path: str,
job_create_timeout: float = 10.0,
pre_create_hook: ScheduledHook | None = None,
client: CloudSchedulerClient | None = None,
) -> type[APIRoute]
Returns a Mixin that should be used to override route_class.
It adds a .scheduler method to the original endpoint.
Example:
scheduled_router = APIRouter(route_class=ScheduledRouteBuilder(...), prefix="/scheduled")
@scheduled_router.get("/simple_scheduled_task")
@as_scheduled_task # optional: makes .scheduler visible to type checkers
def simple_scheduled_task():
# Do work here
simple_scheduled_task.scheduler(name="simple_scheduled_task", schedule="* * * * *").schedule()
app.include_router(scheduled_router)
Source code in fastapi_gcp_tasks/scheduled_route.py
fastapi_gcp_tasks.async_scheduled_route.AsyncScheduledRouteBuilder ¶
AsyncScheduledRouteBuilder(
*,
base_url: str,
location_path: str,
job_create_timeout: float = 10.0,
pre_create_hook: ScheduledHook | None = None,
client: CloudSchedulerAsyncClient
| AsyncCloudSchedulerClientFactory
| None = None,
) -> type[APIRoute]
Returns a Mixin that should be used to override route_class, with an awaitable scheduler.
It adds a .scheduler method to the original endpoint whose .schedule and .delete coroutines must be awaited. Unlike ScheduledRouteBuilder, .schedule() cannot run at module import time — await it from a FastAPI lifespan or a request handler.
client may be a CloudSchedulerAsyncClient, a zero-argument factory returning
one, or None (default client). grpc.aio channels bind to the event loop active at
construction, so the client is resolved lazily on the first awaited call; pass a
factory if your client needs custom construction.
Example:
async_scheduled_router = APIRouter(route_class=AsyncScheduledRouteBuilder(...), prefix="/scheduled")
@async_scheduled_router.get("/simple_scheduled_task")
@as_async_scheduled_task # optional: makes .scheduler visible to type checkers
async def simple_scheduled_task():
# Do work here
@asynccontextmanager
async def lifespan(app: FastAPI):
await simple_scheduled_task.scheduler(name="simple_scheduled_task", schedule="* * * * *").schedule()
yield
app.include_router(async_scheduled_router)
Source code in fastapi_gcp_tasks/async_scheduled_route.py
fastapi_gcp_tasks.decorators.task_default_options ¶
Wrapper to set default options for a cloud task.