fastapi-gcp-tasks 0.1.0__tar.gz

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.
@@ -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.