Delayers & schedulers¶
These are the objects returned by .options() and .scheduler() on task endpoints. You rarely construct
them directly.
fastapi_gcp_tasks.delayer ¶
BaseDelayer ¶
BaseDelayer(
*,
route: APIRoute,
base_url: str,
queue_path: str,
pre_create_hook: DelayedTaskHook,
task_create_timeout: float = 10.0,
countdown: int = 0,
task_id: str | None = None,
)
Bases: Requester
Shared logic to build Cloud Tasks requests from FastAPI routes.
Attributes:
| Name | Type | Description |
|---|---|---|
queue_path (str) |
The path to the Cloud Tasks queue.
|
countdown (int): The delay in seconds before the task is executed. task_create_timeout (float): Timeout for creating the task. task_id (str): The unique identifier for the task. method (tasks_v2.HttpMethod): The HTTP method for the task. pre_create_hook (DelayedTaskHook): Hook to be called before creating the task. |
Source code in fastapi_gcp_tasks/delayer.py
Delayer ¶
Delayer(
*,
route: APIRoute,
base_url: str,
queue_path: str,
client: CloudTasksClient,
pre_create_hook: DelayedTaskHook,
task_create_timeout: float = 10.0,
countdown: int = 0,
task_id: str | None = None,
)
Bases: BaseDelayer
A class to delay HTTP requests as tasks on Google Cloud Tasks.
See BaseDelayer for the shared attributes.
Attributes:
| Name | Type | Description |
|---|---|---|
client (tasks_v2.CloudTasksClient) |
The Cloud Tasks client.
|
|
Source code in fastapi_gcp_tasks/delayer.py
delay ¶
Delay a task on Cloud Tasks.
fastapi_gcp_tasks.async_delayer ¶
AsyncCloudTasksClientProvider ¶
AsyncCloudTasksClientProvider(
*,
client: CloudTasksAsyncClient
| AsyncCloudTasksClientFactory
| None,
auto_create_queue: bool = False,
)
Bases: AsyncClientProvider[CloudTasksAsyncClient]
Lazily resolves and caches a CloudTasksAsyncClient inside the running event loop.
If auto_create_queue is True, each queue path requested via get_for_queue
is ensured exactly once before the client is handed out; a failed ensure is
retried on the next call while the already-resolved client stays cached.
Attributes:
| Name | Type | Description |
|---|---|---|
auto_create_queue (bool) |
Whether to ensure queues exist on first use.
|
|
Source code in fastapi_gcp_tasks/async_delayer.py
get_for_queue
async
¶
Return the cached client, ensuring the given queue exists first if configured.
Source code in fastapi_gcp_tasks/async_delayer.py
AsyncDelayer ¶
AsyncDelayer(
*,
route: APIRoute,
base_url: str,
queue_path: str,
client_provider: AsyncCloudTasksClientProvider,
pre_create_hook: DelayedTaskHook,
task_create_timeout: float = 10.0,
countdown: int = 0,
task_id: str | None = None,
)
Bases: BaseDelayer
A class to delay HTTP requests as tasks on Google Cloud Tasks, using an async client.
See BaseDelayer for the shared attributes.
Attributes:
| Name | Type | Description |
|---|---|---|
client_provider (AsyncCloudTasksClientProvider) |
Lazy provider for the async client.
|
|
Source code in fastapi_gcp_tasks/async_delayer.py
delay
async
¶
Delay a task on Cloud Tasks without blocking the event loop.
Source code in fastapi_gcp_tasks/async_delayer.py
fastapi_gcp_tasks.scheduler ¶
BaseScheduler ¶
BaseScheduler(
*,
route: APIRoute,
base_url: str,
location_path: str,
schedule: str,
pre_create_hook: ScheduledHook,
name: str = "",
job_create_timeout: float = 10.0,
retry_config: RetryConfig | None = None,
time_zone: str = "UTC",
force: bool = False,
)
Bases: Requester
Shared logic to build Cloud Scheduler job requests from FastAPI routes.
Attributes:
| Name | Type | Description |
|---|---|---|
retry_config (scheduler_v1.RetryConfig) |
Configuration for retrying failed jobs.
|
job_id (str): The unique identifier for the job. time_zone (str): The time zone for the job schedule. location_path (str): The location path for the job. cron_schedule (str): The cron schedule for the job. job_create_timeout (float): Timeout for creating the job. method (scheduler_v1.HttpMethod): The HTTP method for the job. pre_create_hook (ScheduledHook): Hook to be called before creating the job. force (bool): Whether to force create the job if it already exists. |
Source code in fastapi_gcp_tasks/scheduler.py
Scheduler ¶
Scheduler(
*,
route: APIRoute,
base_url: str,
location_path: str,
schedule: str,
client: CloudSchedulerClient,
pre_create_hook: ScheduledHook,
name: str = "",
job_create_timeout: float = 10.0,
retry_config: RetryConfig | None = None,
time_zone: str = "UTC",
force: bool = False,
)
Bases: BaseScheduler
A class to schedule HTTP requests as jobs on Google Cloud Scheduler.
See BaseScheduler for the shared attributes.
Attributes:
| Name | Type | Description |
|---|---|---|
client (scheduler_v1.CloudSchedulerClient) |
The Cloud Scheduler client.
|
|
Source code in fastapi_gcp_tasks/scheduler.py
schedule ¶
Schedule a job on Cloud Scheduler.
Source code in fastapi_gcp_tasks/scheduler.py
delete ¶
Delete the job from the scheduler if it exists.
Source code in fastapi_gcp_tasks/scheduler.py
fastapi_gcp_tasks.async_scheduler ¶
AsyncScheduler ¶
AsyncScheduler(
*,
route: APIRoute,
base_url: str,
location_path: str,
schedule: str,
pre_create_hook: ScheduledHook,
client_provider: AsyncClientProvider[
CloudSchedulerAsyncClient
],
name: str = "",
job_create_timeout: float = 10.0,
retry_config: RetryConfig | None = None,
time_zone: str = "UTC",
force: bool = False,
)
Bases: BaseScheduler
A class to schedule HTTP requests as jobs on Google Cloud Scheduler, using an async client.
See BaseScheduler for the shared attributes.
Attributes:
| Name | Type | Description |
|---|---|---|
client_provider (AsyncClientProvider) |
Lazy provider for the async client. Share one
|
provider across schedulers so they reuse the same client and gRPC channel. |
Source code in fastapi_gcp_tasks/async_scheduler.py
schedule
async
¶
Schedule a job on Cloud Scheduler without blocking the event loop.
Source code in fastapi_gcp_tasks/async_scheduler.py
delete
async
¶
Delete the job from the scheduler if it exists.
Source code in fastapi_gcp_tasks/async_scheduler.py
fastapi_gcp_tasks.async_clients ¶
AsyncClientProvider ¶
AsyncClientProvider(
*,
client: ClientT | Callable[[], ClientT] | None,
client_cls: type[ClientT],
)
Bases: Generic[ClientT]
Lazily resolves and caches an async gRPC client inside the running event loop.
grpc.aio channels bind to the event loop active at construction, so the client (or client factory) is only resolved on first use, from within the loop that will await the RPCs. One provider should be shared per route builder so all calls reuse the same client and channel.
Source code in fastapi_gcp_tasks/async_clients.py
get
async
¶
Return the cached client, resolving it on first call.