fastapi-gcp-tasks 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastapi_gcp_tasks/__init__.py +5 -0
- fastapi_gcp_tasks/decorators.py +13 -0
- fastapi_gcp_tasks/delayed_route.py +85 -0
- fastapi_gcp_tasks/delayer.py +106 -0
- fastapi_gcp_tasks/dependencies.py +44 -0
- fastapi_gcp_tasks/exception.py +22 -0
- fastapi_gcp_tasks/hooks.py +115 -0
- fastapi_gcp_tasks/requester.py +105 -0
- fastapi_gcp_tasks/scheduled_route.py +67 -0
- fastapi_gcp_tasks/scheduler.py +152 -0
- fastapi_gcp_tasks/utils.py +48 -0
- fastapi_gcp_tasks-0.1.0.dist-info/LICENSE +22 -0
- fastapi_gcp_tasks-0.1.0.dist-info/METADATA +399 -0
- fastapi_gcp_tasks-0.1.0.dist-info/RECORD +15 -0
- fastapi_gcp_tasks-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from typing import Any, Callable, TypeVar
|
|
2
|
+
|
|
3
|
+
F = TypeVar("F", bound=Callable[..., Any])
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def task_default_options(**kwargs: Any) -> Callable[[F], F]:
|
|
7
|
+
"""Wrapper to set default options for a cloud task."""
|
|
8
|
+
|
|
9
|
+
def wrapper(fn: F) -> F:
|
|
10
|
+
fn._delay_options = kwargs # type: ignore[attr-defined]
|
|
11
|
+
return fn
|
|
12
|
+
|
|
13
|
+
return wrapper
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# Standard Library Imports
|
|
2
|
+
from typing import Callable, Type
|
|
3
|
+
|
|
4
|
+
# Third Party Imports
|
|
5
|
+
from fastapi.routing import APIRoute
|
|
6
|
+
from google.cloud import tasks_v2
|
|
7
|
+
|
|
8
|
+
# Imports from this repository
|
|
9
|
+
from fastapi_gcp_tasks.delayer import Delayer
|
|
10
|
+
from fastapi_gcp_tasks.hooks import DelayedTaskHook, noop_hook
|
|
11
|
+
from fastapi_gcp_tasks.utils import ensure_queue
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def DelayedRouteBuilder( # noqa: N802
|
|
15
|
+
*,
|
|
16
|
+
base_url: str,
|
|
17
|
+
queue_path: str,
|
|
18
|
+
task_create_timeout: float = 10.0,
|
|
19
|
+
pre_create_hook: DelayedTaskHook | None = None,
|
|
20
|
+
client: tasks_v2.CloudTasksClient | None = None,
|
|
21
|
+
auto_create_queue: bool = True,
|
|
22
|
+
) -> Type[APIRoute]:
|
|
23
|
+
"""
|
|
24
|
+
Returns a Mixin that should be used to override route_class.
|
|
25
|
+
|
|
26
|
+
It adds a .delay and .options methods to the original endpoint.
|
|
27
|
+
|
|
28
|
+
Example:
|
|
29
|
+
-------
|
|
30
|
+
```
|
|
31
|
+
delayed_router = APIRouter(route_class=DelayedRouteBuilder(...), prefix="/delayed")
|
|
32
|
+
|
|
33
|
+
class UserData(BaseModel):
|
|
34
|
+
name: str
|
|
35
|
+
|
|
36
|
+
@delayed_router.post("/on_user_create/{user_id}")
|
|
37
|
+
def on_user_create(user_id: str, data: UserData):
|
|
38
|
+
# do work here
|
|
39
|
+
# Return values are meaningless
|
|
40
|
+
|
|
41
|
+
# Call .delay to trigger
|
|
42
|
+
on_user_create.delay(user_id="007", data=UserData(name="Piyush"))
|
|
43
|
+
|
|
44
|
+
app.include_router(delayed_router)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
"""
|
|
48
|
+
if client is None:
|
|
49
|
+
client = tasks_v2.CloudTasksClient()
|
|
50
|
+
|
|
51
|
+
if pre_create_hook is None:
|
|
52
|
+
pre_create_hook = noop_hook
|
|
53
|
+
|
|
54
|
+
if auto_create_queue:
|
|
55
|
+
ensure_queue(client=client, path=queue_path)
|
|
56
|
+
|
|
57
|
+
class TaskRouteMixin(APIRoute):
|
|
58
|
+
def get_route_handler(self) -> Callable:
|
|
59
|
+
original_route_handler = super().get_route_handler()
|
|
60
|
+
self.endpoint.options = self.delay_options # type: ignore[attr-defined]
|
|
61
|
+
self.endpoint.delay = self.delay # type: ignore[attr-defined]
|
|
62
|
+
return original_route_handler
|
|
63
|
+
|
|
64
|
+
def delay_options(self, **options: dict) -> Delayer:
|
|
65
|
+
delay_opts = {
|
|
66
|
+
"base_url": base_url,
|
|
67
|
+
"queue_path": queue_path,
|
|
68
|
+
"task_create_timeout": task_create_timeout,
|
|
69
|
+
"client": client,
|
|
70
|
+
"pre_create_hook": pre_create_hook,
|
|
71
|
+
}
|
|
72
|
+
if hasattr(self.endpoint, "_delay_options"):
|
|
73
|
+
delay_opts |= self.endpoint._delay_options
|
|
74
|
+
delay_opts |= options
|
|
75
|
+
|
|
76
|
+
# ignoring the type here because the dictionary values are unpacked
|
|
77
|
+
return Delayer(
|
|
78
|
+
route=self,
|
|
79
|
+
**delay_opts, # type: ignore[arg-type]
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
def delay(self, **kwargs: dict) -> tasks_v2.Task:
|
|
83
|
+
return self.delay_options().delay(**kwargs)
|
|
84
|
+
|
|
85
|
+
return TaskRouteMixin
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Standard Library Imports
|
|
2
|
+
import datetime
|
|
3
|
+
from typing import Any, Iterable
|
|
4
|
+
|
|
5
|
+
# Third Party Imports
|
|
6
|
+
from fastapi.routing import APIRoute
|
|
7
|
+
from google.cloud import tasks_v2
|
|
8
|
+
from google.protobuf import timestamp_pb2
|
|
9
|
+
|
|
10
|
+
# Imports from this repository
|
|
11
|
+
from fastapi_gcp_tasks.exception import BadMethodError
|
|
12
|
+
from fastapi_gcp_tasks.hooks import DelayedTaskHook
|
|
13
|
+
from fastapi_gcp_tasks.requester import Requester
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Delayer(Requester):
|
|
17
|
+
"""
|
|
18
|
+
A class to delay HTTP requests as tasks on Google Cloud Tasks.
|
|
19
|
+
|
|
20
|
+
Attributes
|
|
21
|
+
----------
|
|
22
|
+
queue_path (str): The path to the Cloud Tasks queue.
|
|
23
|
+
countdown (int): The delay in seconds before the task is executed.
|
|
24
|
+
task_create_timeout (float): Timeout for creating the task.
|
|
25
|
+
task_id (str): The unique identifier for the task.
|
|
26
|
+
method (tasks_v2.HttpMethod): The HTTP method for the task.
|
|
27
|
+
client (tasks_v2.CloudTasksClient): The Cloud Tasks client.
|
|
28
|
+
pre_create_hook (DelayedTaskHook): Hook to be called before creating the task.
|
|
29
|
+
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
*,
|
|
35
|
+
route: APIRoute,
|
|
36
|
+
base_url: str,
|
|
37
|
+
queue_path: str,
|
|
38
|
+
client: tasks_v2.CloudTasksClient,
|
|
39
|
+
pre_create_hook: DelayedTaskHook,
|
|
40
|
+
task_create_timeout: float = 10.0,
|
|
41
|
+
countdown: int = 0,
|
|
42
|
+
task_id: str | None = None,
|
|
43
|
+
) -> None:
|
|
44
|
+
super().__init__(route=route, base_url=base_url)
|
|
45
|
+
self.queue_path = queue_path
|
|
46
|
+
self.countdown = countdown
|
|
47
|
+
self.task_create_timeout = task_create_timeout
|
|
48
|
+
|
|
49
|
+
self.task_id = task_id
|
|
50
|
+
self.method = _task_method(route.methods)
|
|
51
|
+
self.client = client
|
|
52
|
+
self.pre_create_hook = pre_create_hook
|
|
53
|
+
|
|
54
|
+
def delay(self, **kwargs: Any) -> tasks_v2.Task:
|
|
55
|
+
"""Delay a task on Cloud Tasks."""
|
|
56
|
+
# Create http request
|
|
57
|
+
request = tasks_v2.HttpRequest()
|
|
58
|
+
request.http_method = self.method
|
|
59
|
+
request.url = self._url(values=kwargs)
|
|
60
|
+
request.headers = self._headers(values=kwargs)
|
|
61
|
+
|
|
62
|
+
if body := self._body(values=kwargs):
|
|
63
|
+
request.body = body
|
|
64
|
+
|
|
65
|
+
# Scheduled the task
|
|
66
|
+
task = tasks_v2.Task(http_request=request)
|
|
67
|
+
if schedule_time := self._schedule():
|
|
68
|
+
task.schedule_time = schedule_time
|
|
69
|
+
|
|
70
|
+
# Make task name for deduplication
|
|
71
|
+
if self.task_id:
|
|
72
|
+
task.name = f"{self.queue_path}/tasks/{self.task_id}"
|
|
73
|
+
|
|
74
|
+
request = tasks_v2.CreateTaskRequest(parent=self.queue_path, task=task)
|
|
75
|
+
|
|
76
|
+
request = self.pre_create_hook(request)
|
|
77
|
+
|
|
78
|
+
return self.client.create_task(request=request, timeout=self.task_create_timeout)
|
|
79
|
+
|
|
80
|
+
def _schedule(self) -> timestamp_pb2.Timestamp | None:
|
|
81
|
+
if self.countdown is None or self.countdown <= 0:
|
|
82
|
+
return None
|
|
83
|
+
d = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=self.countdown)
|
|
84
|
+
timestamp = timestamp_pb2.Timestamp()
|
|
85
|
+
timestamp.FromDatetime(d)
|
|
86
|
+
return timestamp
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _task_method(methods: Iterable[str]) -> tasks_v2.HttpMethod:
|
|
90
|
+
method_map = {
|
|
91
|
+
"POST": tasks_v2.HttpMethod.POST,
|
|
92
|
+
"GET": tasks_v2.HttpMethod.GET,
|
|
93
|
+
"HEAD": tasks_v2.HttpMethod.HEAD,
|
|
94
|
+
"PUT": tasks_v2.HttpMethod.PUT,
|
|
95
|
+
"DELETE": tasks_v2.HttpMethod.DELETE,
|
|
96
|
+
"PATCH": tasks_v2.HttpMethod.PATCH,
|
|
97
|
+
"OPTIONS": tasks_v2.HttpMethod.OPTIONS,
|
|
98
|
+
}
|
|
99
|
+
methods = list(methods)
|
|
100
|
+
# Only crash if we're being bound
|
|
101
|
+
if len(methods) > 1:
|
|
102
|
+
raise BadMethodError("Can't trigger task with multiple methods")
|
|
103
|
+
method = method_map.get(methods[0])
|
|
104
|
+
if method is None:
|
|
105
|
+
raise BadMethodError(f"Unknown method {methods[0]}")
|
|
106
|
+
return tasks_v2.HttpMethod(method)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Standard Library Imports
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
from typing import Any, Callable
|
|
4
|
+
|
|
5
|
+
# Third Party Imports
|
|
6
|
+
from fastapi import Depends, Header, HTTPException
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def max_retries(count: int = 20) -> Callable[[Any], bool]:
|
|
10
|
+
"""Raises an http exception (with status 200) after max retries are exhausted."""
|
|
11
|
+
|
|
12
|
+
def retries_dep(meta: CloudTasksHeaders = Depends()) -> bool:
|
|
13
|
+
# count starts from 0 so equality check is required
|
|
14
|
+
if meta.retry_count >= count:
|
|
15
|
+
raise HTTPException(status_code=200, detail="Max retries exhausted")
|
|
16
|
+
return True
|
|
17
|
+
|
|
18
|
+
return retries_dep
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class CloudTasksHeaders:
|
|
22
|
+
"""
|
|
23
|
+
Extracts known headers sent by Cloud Tasks.
|
|
24
|
+
|
|
25
|
+
Full list: https://cloud.google.com/tasks/docs/creating-http-target-tasks#handler
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
x_cloudtasks_taskretrycount: int = Header(0),
|
|
31
|
+
x_cloudtasks_taskexecutioncount: int = Header(0),
|
|
32
|
+
x_cloudtasks_queuename: str = Header(""),
|
|
33
|
+
x_cloudtasks_taskname: str = Header(""),
|
|
34
|
+
x_cloudtasks_tasketa: float = Header(0),
|
|
35
|
+
x_cloudtasks_taskpreviousresponse: int = Header(0),
|
|
36
|
+
x_cloudtasks_taskretryreason: str = Header(""),
|
|
37
|
+
) -> None:
|
|
38
|
+
self.retry_count = x_cloudtasks_taskretrycount
|
|
39
|
+
self.execution_count = x_cloudtasks_taskexecutioncount
|
|
40
|
+
self.queue_name = x_cloudtasks_queuename
|
|
41
|
+
self.task_name = x_cloudtasks_taskname
|
|
42
|
+
self.eta = datetime.fromtimestamp(x_cloudtasks_tasketa)
|
|
43
|
+
self.previous_response = x_cloudtasks_taskpreviousresponse
|
|
44
|
+
self.retry_reason = x_cloudtasks_taskretryreason
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Third Party Imports
|
|
2
|
+
from pydantic.v1.errors import MissingError, PydanticValueError
|
|
3
|
+
|
|
4
|
+
# TODO: Migrate to Pydantic v2.0 Errors
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MissingParamError(MissingError):
|
|
8
|
+
"""Error raised when a required parameter is missing."""
|
|
9
|
+
|
|
10
|
+
msg_template = "field required: {param}"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class WrongTypeError(PydanticValueError):
|
|
14
|
+
"""Error raised when a parameter is of the wrong type."""
|
|
15
|
+
|
|
16
|
+
msg_template = "Expected {field} to be of type {type}"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BadMethodError(Exception):
|
|
20
|
+
"""Error raised when an invalid method is passed to a task."""
|
|
21
|
+
|
|
22
|
+
pass
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# Standard Library Imports
|
|
2
|
+
from typing import Any, Callable
|
|
3
|
+
|
|
4
|
+
# Third Party Imports
|
|
5
|
+
from google.cloud import scheduler_v1, tasks_v2
|
|
6
|
+
from google.protobuf import duration_pb2
|
|
7
|
+
|
|
8
|
+
DelayedTaskHook = Callable[[tasks_v2.CreateTaskRequest], tasks_v2.CreateTaskRequest]
|
|
9
|
+
ScheduledHook = Callable[[scheduler_v1.CreateJobRequest], scheduler_v1.CreateJobRequest]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def noop_hook(request: Any) -> Any:
|
|
13
|
+
"""Inspired by https://github.com/kelseyhightower/nocode."""
|
|
14
|
+
return request
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def chained_hook(*hooks: Callable[[Any], Any]) -> Callable[[Any], Any]:
|
|
18
|
+
"""Call all hooks sequentially with the result from the previous hook."""
|
|
19
|
+
|
|
20
|
+
def chain(request: Any) -> Any:
|
|
21
|
+
for hook in hooks:
|
|
22
|
+
request = hook(request)
|
|
23
|
+
return request
|
|
24
|
+
|
|
25
|
+
return chain
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def oidc_scheduled_hook(token: scheduler_v1.OidcToken) -> ScheduledHook:
|
|
29
|
+
"""
|
|
30
|
+
Returns a hook for ScheduledRouteBuilder to add OIDC token to all requests.
|
|
31
|
+
|
|
32
|
+
https://cloud.google.com/scheduler/docs/reference/rpc/google.cloud.scheduler.v1#google.cloud.scheduler.v1.HttpTarget
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def add_token(
|
|
36
|
+
request: scheduler_v1.CreateJobRequest,
|
|
37
|
+
) -> scheduler_v1.CreateJobRequest:
|
|
38
|
+
request.job.http_target.oidc_token = token
|
|
39
|
+
return request
|
|
40
|
+
|
|
41
|
+
return add_token
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def oidc_delayed_hook(token: tasks_v2.OidcToken) -> DelayedTaskHook:
|
|
45
|
+
"""
|
|
46
|
+
Returns a hook for DelayedRouteBuilder to add OIDC token to all requests.
|
|
47
|
+
|
|
48
|
+
https://cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2#google.cloud.tasks.v2.HttpRequest
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def add_token(request: tasks_v2.CreateTaskRequest) -> tasks_v2.CreateTaskRequest:
|
|
52
|
+
request.task.http_request.oidc_token = token
|
|
53
|
+
return request
|
|
54
|
+
|
|
55
|
+
return add_token
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def oauth_scheduled_hook(token: scheduler_v1.OAuthToken) -> ScheduledHook:
|
|
59
|
+
"""
|
|
60
|
+
Returns a hook for ScheduledRouteBuilder to add OAuth token to all requests.
|
|
61
|
+
|
|
62
|
+
https://cloud.google.com/scheduler/docs/reference/rpc/google.cloud.scheduler.v1#google.cloud.scheduler.v1.HttpTarget
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
def add_token(
|
|
66
|
+
request: scheduler_v1.CreateJobRequest,
|
|
67
|
+
) -> scheduler_v1.CreateJobRequest:
|
|
68
|
+
request.job.http_target.oauth_token = token
|
|
69
|
+
return request
|
|
70
|
+
|
|
71
|
+
return add_token
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def oauth_delayed_hook(token: tasks_v2.OAuthToken) -> DelayedTaskHook:
|
|
75
|
+
"""
|
|
76
|
+
Returns a hook for DelayedRouteBuilder to add OAuth token to all requests.
|
|
77
|
+
|
|
78
|
+
https://cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2#google.cloud.tasks.v2.HttpRequest
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
def add_token(request: tasks_v2.CreateTaskRequest) -> tasks_v2.CreateTaskRequest:
|
|
82
|
+
request.task.http_request.oauth_token = token
|
|
83
|
+
return request
|
|
84
|
+
|
|
85
|
+
return add_token
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def deadline_scheduled_hook(duration: duration_pb2.Duration) -> ScheduledHook:
|
|
89
|
+
"""
|
|
90
|
+
Returns a hook for ScheduledRouteBuilder to set Deadline for job execution.
|
|
91
|
+
|
|
92
|
+
https://cloud.google.com/scheduler/docs/reference/rpc/google.cloud.scheduler.v1#google.cloud.scheduler.v1.Job
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def deadline(
|
|
96
|
+
request: scheduler_v1.CreateJobRequest,
|
|
97
|
+
) -> scheduler_v1.CreateJobRequest:
|
|
98
|
+
request.job.attempt_deadline = duration
|
|
99
|
+
return request
|
|
100
|
+
|
|
101
|
+
return deadline
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def deadline_delayed_hook(duration: duration_pb2.Duration) -> DelayedTaskHook:
|
|
105
|
+
"""
|
|
106
|
+
Returns a hook for DelayedRouteBuilder to set Deadline for task execution.
|
|
107
|
+
|
|
108
|
+
https://cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2#google.cloud.tasks.v2.Task
|
|
109
|
+
"""
|
|
110
|
+
|
|
111
|
+
def deadline(request: tasks_v2.CreateTaskRequest) -> tasks_v2.CreateTaskRequest:
|
|
112
|
+
request.task.dispatch_deadline = duration
|
|
113
|
+
return request
|
|
114
|
+
|
|
115
|
+
return deadline
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Standard Library Imports
|
|
2
|
+
from typing import Any, Dict, List, Tuple
|
|
3
|
+
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
|
|
4
|
+
|
|
5
|
+
# Third Party Imports
|
|
6
|
+
from fastapi.dependencies.utils import request_params_to_args
|
|
7
|
+
from fastapi.encoders import jsonable_encoder
|
|
8
|
+
from fastapi.routing import APIRoute
|
|
9
|
+
from pydantic.v1.error_wrappers import ErrorWrapper
|
|
10
|
+
|
|
11
|
+
# Imports from this repository
|
|
12
|
+
from fastapi_gcp_tasks.exception import MissingParamError, WrongTypeError
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
# Third Party Imports
|
|
16
|
+
import ujson as json
|
|
17
|
+
except ImportError:
|
|
18
|
+
# Standard Library Imports
|
|
19
|
+
import json # type: ignore[no-redef]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Requester:
|
|
23
|
+
"""
|
|
24
|
+
A class to construct HTTP requests based on FastAPI routes, handling headers, URL construction, and request bodies.
|
|
25
|
+
|
|
26
|
+
Attributes
|
|
27
|
+
----------
|
|
28
|
+
route (APIRoute): The FastAPI route object.
|
|
29
|
+
base_url (str): The base URL for the requests.
|
|
30
|
+
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
*,
|
|
36
|
+
route: APIRoute,
|
|
37
|
+
base_url: str,
|
|
38
|
+
) -> None:
|
|
39
|
+
self.route = route
|
|
40
|
+
self.base_url = base_url.rstrip("/")
|
|
41
|
+
|
|
42
|
+
def _headers(self, *, values: Dict[str, Any]) -> Dict[str, str]:
|
|
43
|
+
headers = _err_val(request_params_to_args(self.route.dependant.header_params, values))
|
|
44
|
+
cookies = _err_val(request_params_to_args(self.route.dependant.cookie_params, values))
|
|
45
|
+
if len(cookies) > 0:
|
|
46
|
+
headers["Cookies"] = "; ".join([f"{k}={v}" for (k, v) in cookies.items()])
|
|
47
|
+
# We use json only.
|
|
48
|
+
headers["Content-Type"] = "application/json"
|
|
49
|
+
# Always send string headers and skip all headers which are supposed to be sent by cloudtasks
|
|
50
|
+
return {str(k): str(v) for (k, v) in headers.items() if not str(k).startswith("x_cloudtasks_")}
|
|
51
|
+
|
|
52
|
+
def _url(self, *, values: Dict[str, Any]) -> str:
|
|
53
|
+
route = self.route
|
|
54
|
+
path_values = _err_val(request_params_to_args(route.dependant.path_params, values))
|
|
55
|
+
for name, converter in route.param_convertors.items():
|
|
56
|
+
if name in path_values:
|
|
57
|
+
continue
|
|
58
|
+
if name not in values:
|
|
59
|
+
raise MissingParamError(param=name)
|
|
60
|
+
|
|
61
|
+
# TODO: should we catch errors here and raise better errors?
|
|
62
|
+
path_values[name] = converter.convert(values[name])
|
|
63
|
+
path = route.path_format.format(**path_values)
|
|
64
|
+
params = _err_val(request_params_to_args(route.dependant.query_params, values))
|
|
65
|
+
|
|
66
|
+
# Make final URL
|
|
67
|
+
|
|
68
|
+
# Split base url into parts
|
|
69
|
+
url_parts = list(urlparse(self.base_url))
|
|
70
|
+
|
|
71
|
+
# Add relative path
|
|
72
|
+
# Note: you might think urljoin is a better solution here, it is not.
|
|
73
|
+
url_parts[2] = url_parts[2].strip("/") + "/" + path.strip("/")
|
|
74
|
+
|
|
75
|
+
# Make query dict and update our with our params
|
|
76
|
+
query = dict(parse_qsl(url_parts[4]))
|
|
77
|
+
query.update(params)
|
|
78
|
+
|
|
79
|
+
# override query params
|
|
80
|
+
url_parts[4] = urlencode(query)
|
|
81
|
+
return urlunparse(url_parts)
|
|
82
|
+
|
|
83
|
+
def _body(self, *, values: Dict[str, Any]) -> bytes | None:
|
|
84
|
+
body = None
|
|
85
|
+
body_field = self.route.body_field
|
|
86
|
+
if body_field and body_field.name:
|
|
87
|
+
got_body = values.get(body_field.name)
|
|
88
|
+
if got_body is None:
|
|
89
|
+
if body_field.required:
|
|
90
|
+
raise MissingParamError(name=body_field.name)
|
|
91
|
+
got_body = body_field.get_default()
|
|
92
|
+
if not isinstance(got_body, body_field.type_):
|
|
93
|
+
raise WrongTypeError(field=body_field.name, type=body_field.type_)
|
|
94
|
+
body = json.dumps(jsonable_encoder(got_body)).encode()
|
|
95
|
+
return body
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _err_val(resp: Tuple[Dict, List[ErrorWrapper]]) -> Dict:
|
|
99
|
+
values, errors = resp
|
|
100
|
+
|
|
101
|
+
if len(errors) != 0:
|
|
102
|
+
# TODO: Log everything but raise first only
|
|
103
|
+
# TODO: find a better way to raise and display these errors
|
|
104
|
+
raise errors[0].exc
|
|
105
|
+
return values
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Standard Library Imports
|
|
2
|
+
from typing import Callable, Type
|
|
3
|
+
|
|
4
|
+
# Third Party Imports
|
|
5
|
+
from fastapi.routing import APIRoute
|
|
6
|
+
from google.cloud import scheduler_v1
|
|
7
|
+
|
|
8
|
+
# Imports from this repository
|
|
9
|
+
from fastapi_gcp_tasks.hooks import ScheduledHook, noop_hook
|
|
10
|
+
from fastapi_gcp_tasks.scheduler import Scheduler
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def ScheduledRouteBuilder( # noqa: N802
|
|
14
|
+
*,
|
|
15
|
+
base_url: str,
|
|
16
|
+
location_path: str,
|
|
17
|
+
job_create_timeout: float = 10.0,
|
|
18
|
+
pre_create_hook: ScheduledHook | None = None,
|
|
19
|
+
client: scheduler_v1.CloudSchedulerClient | None = None,
|
|
20
|
+
) -> Type[APIRoute]:
|
|
21
|
+
"""
|
|
22
|
+
Returns a Mixin that should be used to override route_class.
|
|
23
|
+
|
|
24
|
+
It adds a .scheduler method to the original endpoint.
|
|
25
|
+
|
|
26
|
+
Example:
|
|
27
|
+
-------
|
|
28
|
+
```
|
|
29
|
+
scheduled_router = APIRouter(route_class=ScheduledRouteBuilder(...), prefix="/scheduled")
|
|
30
|
+
|
|
31
|
+
@scheduled_router.get("/simple_scheduled_task")
|
|
32
|
+
def simple_scheduled_task():
|
|
33
|
+
# Do work here
|
|
34
|
+
|
|
35
|
+
simple_scheduled_task.scheduler(name="simple_scheduled_task", schedule="* * * * *").schedule()
|
|
36
|
+
|
|
37
|
+
app.include_router(scheduled_router)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
"""
|
|
41
|
+
if client is None:
|
|
42
|
+
client = scheduler_v1.CloudSchedulerClient()
|
|
43
|
+
|
|
44
|
+
if pre_create_hook is None:
|
|
45
|
+
pre_create_hook = noop_hook
|
|
46
|
+
|
|
47
|
+
class ScheduledRouteMixin(APIRoute):
|
|
48
|
+
def get_route_handler(self) -> Callable:
|
|
49
|
+
original_route_handler = super().get_route_handler()
|
|
50
|
+
self.endpoint.scheduler = self.scheduler_options # type: ignore[attr-defined]
|
|
51
|
+
return original_route_handler
|
|
52
|
+
|
|
53
|
+
def scheduler_options(self, *, name: str, schedule: str, **options: dict) -> Scheduler:
|
|
54
|
+
scheduler_opts = {
|
|
55
|
+
"base_url": base_url,
|
|
56
|
+
"location_path": location_path,
|
|
57
|
+
"client": client,
|
|
58
|
+
"pre_create_hook": pre_create_hook,
|
|
59
|
+
"job_create_timeout": job_create_timeout,
|
|
60
|
+
"name": name,
|
|
61
|
+
"schedule": schedule,
|
|
62
|
+
} | options
|
|
63
|
+
|
|
64
|
+
# ignoring the type here because the dictionary values are unpacked
|
|
65
|
+
return Scheduler(route=self, **scheduler_opts) # type: ignore[arg-type]
|
|
66
|
+
|
|
67
|
+
return ScheduledRouteMixin
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Standard Library Imports
|
|
2
|
+
from typing import Any, Iterable
|
|
3
|
+
|
|
4
|
+
# Third Party Imports
|
|
5
|
+
from fastapi.routing import APIRoute
|
|
6
|
+
from google.cloud import scheduler_v1
|
|
7
|
+
from google.protobuf import duration_pb2
|
|
8
|
+
|
|
9
|
+
# Imports from this repository
|
|
10
|
+
from fastapi_gcp_tasks.exception import BadMethodError
|
|
11
|
+
from fastapi_gcp_tasks.hooks import ScheduledHook
|
|
12
|
+
from fastapi_gcp_tasks.requester import Requester
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Scheduler(Requester):
|
|
16
|
+
"""
|
|
17
|
+
A class to schedule HTTP requests as jobs on Google Cloud Scheduler.
|
|
18
|
+
|
|
19
|
+
Attributes
|
|
20
|
+
----------
|
|
21
|
+
retry_config (scheduler_v1.RetryConfig): Configuration for retrying failed jobs.
|
|
22
|
+
job_id (str): The unique identifier for the job.
|
|
23
|
+
time_zone (str): The time zone for the job schedule.
|
|
24
|
+
location_path (str): The location path for the job.
|
|
25
|
+
cron_schedule (str): The cron schedule for the job.
|
|
26
|
+
job_create_timeout (float): Timeout for creating the job.
|
|
27
|
+
method (scheduler_v1.HttpMethod): The HTTP method for the job.
|
|
28
|
+
client (scheduler_v1.CloudSchedulerClient): The Cloud Scheduler client.
|
|
29
|
+
pre_create_hook (ScheduledHook): Hook to be called before creating the job.
|
|
30
|
+
force (bool): Whether to force create the job if it already exists.
|
|
31
|
+
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
*,
|
|
37
|
+
route: APIRoute,
|
|
38
|
+
base_url: str,
|
|
39
|
+
location_path: str,
|
|
40
|
+
schedule: str,
|
|
41
|
+
client: scheduler_v1.CloudSchedulerClient,
|
|
42
|
+
pre_create_hook: ScheduledHook,
|
|
43
|
+
name: str = "",
|
|
44
|
+
job_create_timeout: float = 10.0,
|
|
45
|
+
retry_config: scheduler_v1.RetryConfig | None = None,
|
|
46
|
+
time_zone: str = "UTC",
|
|
47
|
+
force: bool = False,
|
|
48
|
+
) -> None:
|
|
49
|
+
super().__init__(route=route, base_url=base_url)
|
|
50
|
+
if not name:
|
|
51
|
+
name = route.unique_id
|
|
52
|
+
|
|
53
|
+
if retry_config is None:
|
|
54
|
+
retry_config = scheduler_v1.RetryConfig(
|
|
55
|
+
retry_count=5,
|
|
56
|
+
max_retry_duration=duration_pb2.Duration(seconds=0),
|
|
57
|
+
min_backoff_duration=duration_pb2.Duration(seconds=5),
|
|
58
|
+
max_backoff_duration=duration_pb2.Duration(seconds=120),
|
|
59
|
+
max_doublings=5,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
self.retry_config = retry_config
|
|
63
|
+
location_parts = client.parse_common_location_path(location_path)
|
|
64
|
+
|
|
65
|
+
self.job_id = client.job_path(job=name, **location_parts)
|
|
66
|
+
self.time_zone = time_zone
|
|
67
|
+
|
|
68
|
+
self.location_path = location_path
|
|
69
|
+
self.cron_schedule = schedule
|
|
70
|
+
self.job_create_timeout = job_create_timeout
|
|
71
|
+
|
|
72
|
+
self.method = _scheduler_method(route.methods)
|
|
73
|
+
self.client = client
|
|
74
|
+
self.pre_create_hook = pre_create_hook
|
|
75
|
+
self.force = force
|
|
76
|
+
|
|
77
|
+
def schedule(self, **kwargs: Any) -> None:
|
|
78
|
+
"""Schedule a job on Cloud Scheduler."""
|
|
79
|
+
# Create http request
|
|
80
|
+
request = scheduler_v1.HttpTarget()
|
|
81
|
+
request.http_method = self.method
|
|
82
|
+
request.uri = self._url(values=kwargs)
|
|
83
|
+
request.headers = self._headers(values=kwargs)
|
|
84
|
+
|
|
85
|
+
body = self._body(values=kwargs)
|
|
86
|
+
if body:
|
|
87
|
+
request.body = body
|
|
88
|
+
|
|
89
|
+
# Scheduled the task
|
|
90
|
+
job = scheduler_v1.Job(
|
|
91
|
+
name=self.job_id,
|
|
92
|
+
http_target=request,
|
|
93
|
+
schedule=self.cron_schedule,
|
|
94
|
+
retry_config=self.retry_config,
|
|
95
|
+
time_zone=self.time_zone,
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
request = scheduler_v1.CreateJobRequest(parent=self.location_path, job=job)
|
|
99
|
+
|
|
100
|
+
request = self.pre_create_hook(request)
|
|
101
|
+
|
|
102
|
+
if self.force or self._has_changed(request=request):
|
|
103
|
+
# Delete and create job
|
|
104
|
+
self.delete()
|
|
105
|
+
self.client.create_job(request=request, timeout=self.job_create_timeout)
|
|
106
|
+
|
|
107
|
+
def _has_changed(self, request: scheduler_v1.CreateJobRequest) -> bool:
|
|
108
|
+
try:
|
|
109
|
+
job = self.client.get_job(name=request.job.name)
|
|
110
|
+
# Remove things that are either output only or GCP adds by default
|
|
111
|
+
job.user_update_time = None # type: ignore[assignment]
|
|
112
|
+
job.state = None # type: ignore[assignment]
|
|
113
|
+
job.status = None
|
|
114
|
+
job.last_attempt_time = None # type: ignore[assignment]
|
|
115
|
+
job.schedule_time = None # type: ignore[assignment]
|
|
116
|
+
del job.http_target.headers["User-Agent"]
|
|
117
|
+
# Proto compare works directly with `__eq__`
|
|
118
|
+
return job != request.job
|
|
119
|
+
# TODO: replace this with a more specific exception
|
|
120
|
+
except Exception: # noqa: BLE001
|
|
121
|
+
return True
|
|
122
|
+
return False
|
|
123
|
+
|
|
124
|
+
def delete(self) -> bool | Exception:
|
|
125
|
+
"""Delete the job from the scheduler if it exists."""
|
|
126
|
+
# We return true or exception because you could have the delete code on multiple instances
|
|
127
|
+
try:
|
|
128
|
+
self.client.delete_job(name=self.job_id, timeout=self.job_create_timeout)
|
|
129
|
+
return True
|
|
130
|
+
# TODO: replace this with a more specific exception. we may also just raise the exception here?
|
|
131
|
+
except Exception as ex: # noqa: BLE001
|
|
132
|
+
return ex
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _scheduler_method(methods: Iterable[str]) -> scheduler_v1.HttpMethod:
|
|
136
|
+
method_map = {
|
|
137
|
+
"POST": scheduler_v1.HttpMethod.POST,
|
|
138
|
+
"GET": scheduler_v1.HttpMethod.GET,
|
|
139
|
+
"HEAD": scheduler_v1.HttpMethod.HEAD,
|
|
140
|
+
"PUT": scheduler_v1.HttpMethod.PUT,
|
|
141
|
+
"DELETE": scheduler_v1.HttpMethod.DELETE,
|
|
142
|
+
"PATCH": scheduler_v1.HttpMethod.PATCH,
|
|
143
|
+
"OPTIONS": scheduler_v1.HttpMethod.OPTIONS,
|
|
144
|
+
}
|
|
145
|
+
methods = list(methods)
|
|
146
|
+
# Only crash if we're being bound
|
|
147
|
+
if len(methods) > 1:
|
|
148
|
+
raise BadMethodError("Can't schedule task with multiple methods")
|
|
149
|
+
method = method_map.get(methods[0])
|
|
150
|
+
if method is None:
|
|
151
|
+
raise BadMethodError(f"Unknown method {methods[0]}")
|
|
152
|
+
return scheduler_v1.HttpMethod(method)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Third Party Imports
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
import grpc
|
|
5
|
+
from google.api_core.exceptions import AlreadyExists
|
|
6
|
+
from google.cloud import scheduler_v1, tasks_v2
|
|
7
|
+
from google.cloud.tasks_v2.services.cloud_tasks import transports
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def location_path(*, project: str, location: str) -> str:
|
|
11
|
+
"""Helper function to construct a location path for Cloud Scheduler."""
|
|
12
|
+
return scheduler_v1.CloudSchedulerClient.common_location_path(project=project, location=location)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def queue_path(*, project: str, location: str, queue: str) -> str:
|
|
16
|
+
"""Helper function to construct a queue path for Cloud Tasks."""
|
|
17
|
+
return tasks_v2.CloudTasksClient.queue_path(project=project, location=location, queue=queue)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def ensure_queue(
|
|
21
|
+
*,
|
|
22
|
+
client: tasks_v2.CloudTasksClient,
|
|
23
|
+
path: str,
|
|
24
|
+
**kwargs: Any,
|
|
25
|
+
) -> None:
|
|
26
|
+
"""
|
|
27
|
+
Helper function to ensure a Cloud Tasks queue exists.
|
|
28
|
+
|
|
29
|
+
If the queue already exists, this function will not raise an error.
|
|
30
|
+
If the queue does not exist, it will be created with the provided kwargs.
|
|
31
|
+
"""
|
|
32
|
+
# We extract information from the queue path to make the public api simpler
|
|
33
|
+
parsed_queue_path = client.parse_queue_path(path=path)
|
|
34
|
+
create_req = tasks_v2.CreateQueueRequest(
|
|
35
|
+
parent=location_path(**parsed_queue_path),
|
|
36
|
+
queue=tasks_v2.Queue(name=path, **kwargs),
|
|
37
|
+
)
|
|
38
|
+
try:
|
|
39
|
+
client.create_queue(request=create_req)
|
|
40
|
+
except AlreadyExists:
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def emulator_client(*, host: str = "localhost:8123") -> tasks_v2.CloudTasksClient:
|
|
45
|
+
"""Helper function to create a CloudTasksClient from an emulator host."""
|
|
46
|
+
channel = grpc.insecure_channel(host)
|
|
47
|
+
transport = transports.CloudTasksGrpcTransport(channel=channel)
|
|
48
|
+
return tasks_v2.CloudTasksClient(transport=transport)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Simplify Jobs, Inc
|
|
4
|
+
Copyright (c) 2021 Adori Labs, Inc
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: fastapi-gcp-tasks
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Trigger delayed Cloud Tasks from FastAPI
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Team Simplify
|
|
7
|
+
Author-email: oss@simplify.jobs
|
|
8
|
+
Requires-Python: >=3.11,<3.14
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Requires-Dist: fastapi (>=0.110.0,<0.120.0)
|
|
14
|
+
Requires-Dist: google-cloud-scheduler (>=2.13.3,<2.20.0)
|
|
15
|
+
Requires-Dist: google-cloud-tasks (>=2.16.3,<2.20.0)
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# FastAPI Cloud Tasks
|
|
19
|
+
|
|
20
|
+
Strongly typed background tasks with FastAPI and Google CloudTasks. This is a fork of [fastapi-gcp-tasks](https://github.com/adori/fastapi-gcp-tasks), updated with new features and bug fixes.
|
|
21
|
+
|
|
22
|
+
```mermaid
|
|
23
|
+
sequenceDiagram
|
|
24
|
+
autonumber
|
|
25
|
+
actor User
|
|
26
|
+
participant Service
|
|
27
|
+
participant CloudTasks
|
|
28
|
+
participant Worker
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
User ->>+ Service: /trigger
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
rect rgb(100,130,180)
|
|
35
|
+
note right of Service: hello.delay()
|
|
36
|
+
Service -->>+ CloudTasks: Create task
|
|
37
|
+
CloudTasks -->>- Service: Accepted
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
Service ->>- User: Hello task triggered
|
|
41
|
+
note right of CloudTasks: Async
|
|
42
|
+
CloudTasks -->>+ Worker: /hello
|
|
43
|
+
Worker -->>- CloudTasks: 200
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
```
|
|
50
|
+
pip install fastapi-gcp-tasks
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Key features
|
|
54
|
+
|
|
55
|
+
- Strongly typed tasks.
|
|
56
|
+
- Fail at invocation site to make it easier to develop and debug.
|
|
57
|
+
- Breaking schema changes between versions will fail at task runner with Pydantic.
|
|
58
|
+
- Familiar and simple public API
|
|
59
|
+
- `.delay` method that takes same arguments as the task.
|
|
60
|
+
- `.scheduler` method to create recurring job.
|
|
61
|
+
- Tasks are regular FastAPI endpoints on plain old HTTP.
|
|
62
|
+
- `Depends` just works!
|
|
63
|
+
- All middlewares, telemetry, auth, debugging etc solutions for FastAPI work as is.
|
|
64
|
+
- Host task runners independent of GCP. If CloudTasks can reach the URL, it can invoke the task.
|
|
65
|
+
- Save money.
|
|
66
|
+
- Task invocation with GCP is [free for first million, then costs $0.4/million](https://cloud.google.com/tasks/pricing).
|
|
67
|
+
That's almost always cheaper than running a RabbitMQ/Redis/SQL backend for celery.
|
|
68
|
+
- Jobs cost [$0.1 per job per month irrespective of invocations. 3 jobs are free.](https://cloud.google.com/scheduler#pricing)
|
|
69
|
+
Either free or almost always cheaper than always running beat worker.
|
|
70
|
+
- If somehow, this cost ever becomes a concern, the `client` can be overriden to call any gRPC server with a compatible API.
|
|
71
|
+
[Here's a trivial emulator implementation that we will use locally](https://github.com/aertje/cloud-tasks-emulator)
|
|
72
|
+
- Autoscale.
|
|
73
|
+
- With a FaaS setup, your task workers can autoscale based on load.
|
|
74
|
+
- Most FaaS services have free tiers making it much cheaper than running a celery worker.
|
|
75
|
+
|
|
76
|
+
## How it works
|
|
77
|
+
|
|
78
|
+
### Delayed job
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from fastapi_gcp_tasks import DelayedRouteBuilder
|
|
82
|
+
|
|
83
|
+
delayed_router = APIRouter(route_class=DelayedRouteBuilder(...))
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class Recipe(BaseModel):
|
|
87
|
+
ingredients: List[str]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@delayed_router.post("/{restaurant}/make_dinner")
|
|
91
|
+
async def make_dinner(restaurant: str, recipe: Recipe):
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
# Do a ton of work here.
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
app.include_router(delayed_router)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Now we can trigger the task with
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
make_dinner.delay(restaurant="Taj", recipe=Recipe(ingredients=["Pav","Bhaji"]))
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
If we want to trigger the task 30 minutes later
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
make_dinner.options(countdown=1800).delay(...)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Scheduled Task
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from fastapi_gcp_tasks import ScheduledRouteBuilder
|
|
116
|
+
|
|
117
|
+
scheduled_router = APIRouter(route_class=ScheduledRouteBuilder(...))
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class Recipe(BaseModel):
|
|
121
|
+
ingredients: List[str]
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@scheduled_router.post("/home_cook")
|
|
125
|
+
async def home_cook(recipe: Recipe):
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
# Make my own food
|
|
129
|
+
|
|
130
|
+
app.include_router(scheduled_router)
|
|
131
|
+
|
|
132
|
+
# If you want to make your own breakfast every morning at 7AM IST.
|
|
133
|
+
home_cook.scheduler(name="test-home-cook-at-7AM-IST", schedule="0 7 * * *", time_zone="Asia/Kolkata").schedule(
|
|
134
|
+
recipe=Recipe(ingredients=["Milk", "Cereal"]))
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Concept
|
|
138
|
+
|
|
139
|
+
[`Cloud Tasks`](https://cloud.google.com/tasks) allows us to schedule a HTTP request in the future.
|
|
140
|
+
|
|
141
|
+
[FastAPI](https://fastapi.tiangolo.com/tutorial/body/) makes us define complete schema and params for an HTTP endpoint.
|
|
142
|
+
|
|
143
|
+
[`Cloud Scheduler`](https://cloud.google.com/scheduler) allows us to schedule recurring HTTP requests in the future.
|
|
144
|
+
|
|
145
|
+
FastAPI Cloud Tasks works by putting the three together:
|
|
146
|
+
|
|
147
|
+
- GCP's Cloud Tasks + FastAPI = Partial replacement for celery's async delayed tasks.
|
|
148
|
+
- GCP's Cloud Scheduler + FastAPI = Replacement for celery beat.
|
|
149
|
+
- FastAPI Cloud Tasks + Cloud Run = Autoscaled delayed tasks.
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
## Running
|
|
154
|
+
|
|
155
|
+
### Local
|
|
156
|
+
|
|
157
|
+
Pre-requisites:
|
|
158
|
+
- `pip install fastapi-gcp-tasks`
|
|
159
|
+
- Install [cloud-tasks-emulator](https://github.com/aertje/cloud-tasks-emulator)
|
|
160
|
+
- Alternatively install ngrok and forward the server's port
|
|
161
|
+
|
|
162
|
+
Start running the emulator in a terminal
|
|
163
|
+
```sh
|
|
164
|
+
cloud-tasks-emulator
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Start running the task runner on port 8000 so that it is accessible from cloud tasks.
|
|
168
|
+
|
|
169
|
+
```sh
|
|
170
|
+
uvicorn examples.simple.main:app --reload --port 8000
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
In another terminal, trigger the task with curl
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
curl http://localhost:8000/trigger
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Check the logs on the server, you should see
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
WARNING: Hello task ran with payload: Triggered task
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Important bits of code:
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
# complete file: examples/simple/main.py
|
|
189
|
+
|
|
190
|
+
# For local, we connect to the emulator client
|
|
191
|
+
client = None
|
|
192
|
+
if IS_LOCAL:
|
|
193
|
+
client = emulator_client()
|
|
194
|
+
|
|
195
|
+
# Construct our DelayedRoute class with all relevant settings
|
|
196
|
+
# This can be done once across the entire project
|
|
197
|
+
DelayedRoute = DelayedRouteBuilder(
|
|
198
|
+
client=client,
|
|
199
|
+
base_url="http://localhost:8000"
|
|
200
|
+
queue_path=queue_path(
|
|
201
|
+
project="gcp-project-id",
|
|
202
|
+
location="asia-south1",
|
|
203
|
+
queue="test-queue",
|
|
204
|
+
),
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
# Override the route_class so that we can add .delay method to the endpoints and know their complete URL
|
|
208
|
+
delayed_router = APIRouter(route_class=DelayedRoute, prefix="/delayed")
|
|
209
|
+
|
|
210
|
+
class Payload(BaseModel):
|
|
211
|
+
message: str
|
|
212
|
+
|
|
213
|
+
@delayed_router.post("/hello")
|
|
214
|
+
async def hello(p: Payload = Payload(message="Default")):
|
|
215
|
+
logger.warning(f"Hello task ran with payload: {p.message}")
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
# Define our app and add trigger to it.
|
|
219
|
+
app = FastAPI()
|
|
220
|
+
|
|
221
|
+
@app.get("/trigger")
|
|
222
|
+
async def trigger():
|
|
223
|
+
# Trigger the task
|
|
224
|
+
hello.delay(p=Payload(message="Triggered task"))
|
|
225
|
+
return {"message": "Hello task triggered"}
|
|
226
|
+
|
|
227
|
+
app.include_router(delayed_router)
|
|
228
|
+
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Note: You can read complete working source code of the above example in [`examples/simple/main.py`](examples/simple/main.py)
|
|
232
|
+
|
|
233
|
+
In the real world you'd have a separate process for task runner and actual task.
|
|
234
|
+
|
|
235
|
+
### Deployed environment / Cloud Run
|
|
236
|
+
|
|
237
|
+
Running on Cloud Run with authentication needs us to supply an OIDC token. To do that we can use a `hook`.
|
|
238
|
+
|
|
239
|
+
Pre-requisites:
|
|
240
|
+
|
|
241
|
+
- Create a task queue. Copy the project id, location and queue name.
|
|
242
|
+
- Deploy the worker as a service on Cloud Run and copy it's URL.
|
|
243
|
+
- Create a service account in cloud IAM and add `Cloud Run Invoker` role to it.
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
# URL of the Cloud Run service
|
|
248
|
+
base_url = "https://hello-randomchars-el.a.run.app"
|
|
249
|
+
|
|
250
|
+
DelayedRoute = DelayedRouteBuilder(
|
|
251
|
+
base_url=base_url,
|
|
252
|
+
# Task queue, same as above.
|
|
253
|
+
queue_path=queue_path(...),
|
|
254
|
+
pre_create_hook=oidc_task_hook(
|
|
255
|
+
token=tasks_v2.OidcToken(
|
|
256
|
+
# Service account that you created
|
|
257
|
+
service_account_email="fastapi-gcp-tasks@gcp-project-id.iam.gserviceaccount.com",
|
|
258
|
+
audience=base_url,
|
|
259
|
+
),
|
|
260
|
+
),
|
|
261
|
+
)
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
Check the fleshed out example at [`examples/full/tasks.py`](examples/full/tasks.py)
|
|
265
|
+
|
|
266
|
+
If you're not running on CloudRun and want to an OAuth Token instead, you can use the `oauth_task_hook` instead.
|
|
267
|
+
|
|
268
|
+
Check [fastapi_cloud_tasks/hooks.py](fastapi_gcp_tasks/hooks.py) to get the hang od hooks and how you can use them.
|
|
269
|
+
|
|
270
|
+
## Configuration
|
|
271
|
+
|
|
272
|
+
### DelayedRouteBuilder
|
|
273
|
+
|
|
274
|
+
Usage:
|
|
275
|
+
|
|
276
|
+
```python
|
|
277
|
+
DelayedRoute = DelayedRouteBuilder(...)
|
|
278
|
+
delayed_router = APIRouter(route_class=DelayedRoute)
|
|
279
|
+
|
|
280
|
+
@delayed_router.get("/simple_task")
|
|
281
|
+
def simple_task():
|
|
282
|
+
return {}
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
- `base_url` - The URL of your worker FastAPI service.
|
|
286
|
+
|
|
287
|
+
- `queue_path` - Full path of the Cloud Tasks queue. (Hint: use the util function `queue_path`)
|
|
288
|
+
|
|
289
|
+
- `task_create_timeout` - How long should we wait before giving up on creating cloud task.
|
|
290
|
+
|
|
291
|
+
- `pre_create_hook` - If you need to edit the `CreateTaskRequest` before sending it to Cloud Tasks (eg: Auth for Cloud Run), you can do that with this hook. See hooks section below for more.
|
|
292
|
+
|
|
293
|
+
- `client` - If you need to override the Cloud Tasks client, pass the client here. (eg: changing credentials, transport etc)
|
|
294
|
+
|
|
295
|
+
#### Task level default options
|
|
296
|
+
|
|
297
|
+
Usage:
|
|
298
|
+
|
|
299
|
+
```python
|
|
300
|
+
@delayed_router.get("/simple_task")
|
|
301
|
+
@task_default_options(...)
|
|
302
|
+
def simple_task():
|
|
303
|
+
return {}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
All options from above can be passed as `kwargs` to the decorator.
|
|
307
|
+
|
|
308
|
+
Additional options:
|
|
309
|
+
|
|
310
|
+
- `countdown` - Seconds in the future to schedule the task.
|
|
311
|
+
- `task_id` - named task id for deduplication. (One task id will only be queued once.)
|
|
312
|
+
|
|
313
|
+
Example:
|
|
314
|
+
|
|
315
|
+
```python
|
|
316
|
+
# Trigger after 5 minutes
|
|
317
|
+
@delayed_router.get("/simple_task")
|
|
318
|
+
@task_default_options(countdown=300)
|
|
319
|
+
def simple_task():
|
|
320
|
+
return {}
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
#### Delayer Options
|
|
324
|
+
|
|
325
|
+
Usage:
|
|
326
|
+
|
|
327
|
+
```python
|
|
328
|
+
simple_task.options(...).delay()
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
All options from above can be overwritten per call (including DelayedRouteBuilder options like `base_url`) with kwargs to the `options` function before calling delay.
|
|
332
|
+
|
|
333
|
+
Example:
|
|
334
|
+
|
|
335
|
+
```python
|
|
336
|
+
# Trigger after 2 minutes
|
|
337
|
+
simple_task.options(countdown=120).delay()
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
### ScheduledRouteBuilder
|
|
341
|
+
|
|
342
|
+
Usage:
|
|
343
|
+
|
|
344
|
+
```python
|
|
345
|
+
ScheduledRoute = ScheduledRouteBuilder(...)
|
|
346
|
+
scheduled_router = APIRouter(route_class=ScheduledRoute)
|
|
347
|
+
|
|
348
|
+
@scheduled_router.get("/simple_scheduled_task")
|
|
349
|
+
def simple_scheduled_task():
|
|
350
|
+
return {}
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
simple_scheduled_task.scheduler(name="simple_scheduled_task", schedule="* * * * *").schedule()
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
## Hooks
|
|
358
|
+
|
|
359
|
+
We might need to override things in the task being sent to Cloud Tasks. The `pre_create_hook` allows us to do that.
|
|
360
|
+
|
|
361
|
+
Some hooks are included in the library.
|
|
362
|
+
|
|
363
|
+
- `oidc_delayed_hook` / `oidc_scheduled_hook` - Used to pass OIDC token (for Cloud Run etc).
|
|
364
|
+
- `deadline_delayed_hook` / `deadline_scheduled_hook` - Used to change the timeout for the worker of a task. (PS: this deadline is decided by the sender to the queue and not the worker)
|
|
365
|
+
- `chained_hook` - If you need to chain multiple hooks together, you can do that with `chained_hook(hook1, hook2)`
|
|
366
|
+
|
|
367
|
+
## Helper dependencies
|
|
368
|
+
|
|
369
|
+
### max_retries
|
|
370
|
+
|
|
371
|
+
```python
|
|
372
|
+
@delayed_router.post("/fail_twice", dependencies=[Depends(max_retries(2))])
|
|
373
|
+
async def fail_twice():
|
|
374
|
+
raise Exception("nooo")
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
### CloudTasksHeaders
|
|
378
|
+
|
|
379
|
+
```python
|
|
380
|
+
@delayed_router.get("/my_task")
|
|
381
|
+
async def my_task(ct_headers: CloudTasksHeaders = Depends()):
|
|
382
|
+
print(ct_headers.queue_name)
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
Check the file [fastapi_cloud_tasks/dependencies.py](fastapi_gcp_tasks/dependencies.py) for details.
|
|
386
|
+
|
|
387
|
+
## Contributing
|
|
388
|
+
|
|
389
|
+
- Run the `format.sh` and `lint.sh` scripts before raising a PR.
|
|
390
|
+
- Add examples and/or tests for new features.
|
|
391
|
+
- If the change is massive, open an issue to discuss it before writing code.
|
|
392
|
+
|
|
393
|
+
## License
|
|
394
|
+
|
|
395
|
+
This project is licensed under the terms of the MIT license. This project was forked from [fastapi-gcp-tasks](https://github.com/Adori/fastapi-gcp-tasks) under the MIT license. All changes made to the original project are also licensed under the MIT license.
|
|
396
|
+
|
|
397
|
+
## Disclaimer
|
|
398
|
+
|
|
399
|
+
This project is neither affiliated with, nor sponsored by Google.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
fastapi_gcp_tasks/__init__.py,sha256=PJCDGps4nkDIVqYteR_Spj-vnAdNrulvBs7I-UWBg4I,223
|
|
2
|
+
fastapi_gcp_tasks/decorators.py,sha256=1MVCd8j64WIwfciOUtTk_7bm82Xwoo2CezDzpS0mB3U,341
|
|
3
|
+
fastapi_gcp_tasks/delayed_route.py,sha256=g2S3gFuqOMdIb7WavAxCmH4KRgTXxsBXv0kqUJMY5r8,2695
|
|
4
|
+
fastapi_gcp_tasks/delayer.py,sha256=wlaKYxpW1FHeukU1b1vl5ob7ttYD1FmdEnMcU0v-VzQ,3687
|
|
5
|
+
fastapi_gcp_tasks/dependencies.py,sha256=vU6jIkRL1cHY8scTDTukI8Z6Gj_TGevWe6O7HzyXMCg,1605
|
|
6
|
+
fastapi_gcp_tasks/exception.py,sha256=j73Wnp8hvFt2KcsIa02PLNAI5g19GzmppbfgUhw5S6Y,553
|
|
7
|
+
fastapi_gcp_tasks/hooks.py,sha256=Lycd2W-5GzpUvVdIv52O9fzJ_svyPhM_v0WH_N73w9w,3637
|
|
8
|
+
fastapi_gcp_tasks/requester.py,sha256=hW4MWdTw8FLqTUX7uY7xO2GT5x-kj6u-Vt6p1SkL0Ms,3872
|
|
9
|
+
fastapi_gcp_tasks/scheduled_route.py,sha256=uVXbTmWWB1eYGTzkktuPpW8HmHECG_42E48EOW_EfDU,2175
|
|
10
|
+
fastapi_gcp_tasks/scheduler.py,sha256=jRYxfMDIJi7kIwM97WXLokKu1HgxSKvwutxd0eLkdd0,5768
|
|
11
|
+
fastapi_gcp_tasks/utils.py,sha256=cWmb3Hp_ARC_7jzlWJqmBev5p6_AQ8-3x82y4uQ6Vck,1754
|
|
12
|
+
fastapi_gcp_tasks-0.1.0.dist-info/LICENSE,sha256=MZNVfvWc_pvZ3mRsL-THDNaozD4Xv_QRjpoAe1-ghSY,1110
|
|
13
|
+
fastapi_gcp_tasks-0.1.0.dist-info/METADATA,sha256=Z_scg3XKbr8DgAurYe9zekzGbnDhZyORC5-hn0xASA0,11416
|
|
14
|
+
fastapi_gcp_tasks-0.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
15
|
+
fastapi_gcp_tasks-0.1.0.dist-info/RECORD,,
|