Skip to content

Commit eb6740c

Browse files
🎉 Migrate from fastapi-cloud-tasks to fastapi-gcp-tasks (#4)
1 parent 42c04e8 commit eb6740c

35 files changed

+2042
-333
lines changed

.flake8

Lines changed: 0 additions & 5 deletions
This file was deleted.

.github/workflows/lint.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: lint
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
types:
9+
- opened
10+
- synchronize
11+
12+
jobs:
13+
lint:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Check out repository
17+
uses: actions/checkout@v4
18+
- name: Set up python
19+
id: setup-python
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: '3.11'
23+
24+
- name: Install Poetry
25+
uses: snok/install-poetry@v1
26+
with:
27+
virtualenvs-create: true
28+
virtualenvs-in-project: true
29+
installer-parallel: true
30+
31+
- name: Load cached venv
32+
id: cached-poetry-dependencies
33+
uses: actions/cache@v3
34+
with:
35+
path: .venv
36+
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
37+
38+
- name: Install dependencies
39+
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
40+
run: poetry install --no-interaction --no-root
41+
42+
- name: Install project
43+
run: poetry install --no-interaction
44+
45+
- name: Run tests
46+
run: |
47+
source .venv/bin/activate
48+
sh scripts/lint.sh

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,8 @@ dmypy.json
132132
pyvenv.cfg
133133
bin/
134134

135-
version.txt
135+
# repository metadata
136+
version.txt
137+
138+
# JetBrains IDE
139+
.idea/

.isort.cfg

Lines changed: 0 additions & 7 deletions
This file was deleted.

.pre-commit-config.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
MIT License
22

3+
Copyright (c) 2024 Simplify Jobs, Inc
34
Copyright (c) 2021 Adori Labs, Inc
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy

README.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,20 @@ pip install fastapi-cloud-tasks
6161
### Delayed job
6262

6363
```python
64-
from fastapi_cloud_tasks import DelayedRouteBuilder
64+
from fastapi_gcp_tasks import DelayedRouteBuilder
6565

6666
delayed_router = APIRouter(route_class=DelayedRouteBuilder(...))
6767

68+
6869
class Recipe(BaseModel):
69-
ingredients: List[str]
70+
ingredients: List[str]
71+
7072

7173
@delayed_router.post("/{restaurant}/make_dinner")
7274
async def make_dinner(restaurant: str, recipe: Recipe):
73-
# Do a ton of work here.
75+
76+
77+
# Do a ton of work here.
7478

7579

7680
app.include_router(delayed_router)
@@ -89,22 +93,28 @@ make_dinner.options(countdown=1800).delay(...)
8993
```
9094

9195
### Scheduled Task
96+
9297
```python
93-
from fastapi_cloud_tasks import ScheduledRouteBuilder
98+
from fastapi_gcp_tasks import ScheduledRouteBuilder
9499

95100
scheduled_router = APIRouter(route_class=ScheduledRouteBuilder(...))
96101

102+
97103
class Recipe(BaseModel):
98-
ingredients: List[str]
104+
ingredients: List[str]
105+
99106

100107
@scheduled_router.post("/home_cook")
101108
async def home_cook(recipe: Recipe):
102-
# Make my own food
109+
110+
111+
# Make my own food
103112

104113
app.include_router(scheduled_router)
105114

106115
# If you want to make your own breakfast every morning at 7AM IST.
107-
home_cook.scheduler(name="test-home-cook-at-7AM-IST", schedule="0 7 * * *", time_zone="Asia/Kolkata").schedule(recipe=Recipe(ingredients=["Milk","Cereal"]))
116+
home_cook.scheduler(name="test-home-cook-at-7AM-IST", schedule="0 7 * * *", time_zone="Asia/Kolkata").schedule(
117+
recipe=Recipe(ingredients=["Milk", "Cereal"]))
108118
```
109119

110120
## Concept
@@ -238,7 +248,7 @@ Check the fleshed out example at [`examples/full/tasks.py`](examples/full/tasks.
238248

239249
If you're not running on CloudRun and want to an OAuth Token instead, you can use the `oauth_task_hook` instead.
240250

241-
Check [fastapi_cloud_tasks/hooks.py](fastapi_cloud_tasks/hooks.py) to get the hang od hooks and how you can use them.
251+
Check [fastapi_cloud_tasks/hooks.py](fastapi_gcp_tasks/hooks.py) to get the hang od hooks and how you can use them.
242252

243253
## Configuration
244254

@@ -301,7 +311,7 @@ Usage:
301311
simple_task.options(...).delay()
302312
```
303313

304-
All options from above can be overriden per call (including DelayedRouteBuilder options like `base_url`) with kwargs to the `options` function before calling delay.
314+
All options from above can be overwritten per call (including DelayedRouteBuilder options like `base_url`) with kwargs to the `options` function before calling delay.
305315

306316
Example:
307317

@@ -355,12 +365,18 @@ async def my_task(ct_headers: CloudTasksHeaders = Depends()):
355365
print(ct_headers.queue_name)
356366
```
357367

358-
Check the file [fastapi_cloud_tasks/dependencies.py](fastapi_cloud_tasks/dependencies.py) for details.
368+
Check the file [fastapi_cloud_tasks/dependencies.py](fastapi_gcp_tasks/dependencies.py) for details.
359369

360370
## Contributing
361371

362372
- Run `pre-commit install` on your local to get pre-commit hook.
363373
- Make changes and raise a PR!
364374
- If the change is massive, open an issue to discuss it before writing code.
365375

366-
Note: This project is neither affiliated with, nor sponsored by Google.
376+
## License
377+
378+
This project is licensed under the terms of the MIT license. This project was forked from [fastapi-cloud-tasks](https://github.com/Adori/fastapi-cloud-tasks) under the MIT license. All changes made to the original project are also licensed under the MIT license.
379+
380+
## Disclaimer
381+
382+
This project is neither affiliated with, nor sponsored by Google.

examples/full/main.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22
from uuid import uuid4
33

44
# Third Party Imports
5-
from fastapi import FastAPI
6-
from fastapi import Response
7-
from fastapi import status
5+
from fastapi import FastAPI, Response, status
86
from google.api_core.exceptions import AlreadyExists
97

108
# Imports from this repository
119
from examples.full.serializer import Payload
1210
from examples.full.settings import IS_LOCAL
13-
from examples.full.tasks import fail_twice
14-
from examples.full.tasks import hello
11+
from examples.full.tasks import fail_twice, hello
1512

1613
app = FastAPI()
1714

@@ -44,9 +41,7 @@ async def deduped(response: Response):
4441
@app.get("/fail")
4542
async def fail():
4643
fail_twice.delay()
47-
return {
48-
"message": "The triggered task will fail twice and then be marked done automatically"
49-
}
44+
return {"message": "The triggered task will fail twice and then be marked done automatically"}
5045

5146

5247
# We can use a trick on local to get all tasks on the same process as the main server.

examples/full/serializer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33

44

55
class Payload(BaseModel):
6+
"""Basic payload from the api."""
7+
68
message: str

examples/full/settings.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@
22
import os
33

44
# Third Party Imports
5-
from google.cloud import scheduler_v1
6-
from google.cloud import tasks_v2
5+
from google.cloud import scheduler_v1, tasks_v2
76

87
# Imports from this repository
9-
from fastapi_cloud_tasks.utils import location_path
10-
from fastapi_cloud_tasks.utils import queue_path
8+
from fastapi_gcp_tasks.utils import location_path, queue_path
119

1210
# set env var IS_LOCAL=false for your deployment environment
1311
IS_LOCAL = os.getenv("IS_LOCAL", "true").lower() == "true"
1412

1513
# The suffix _fastapi_cloud_tasks is a trick for running both main and task server in the same process for local
1614
# In a deployed environment, you'd most likely want them to be separate
1715
# Check main.py for how this is used.
18-
TASK_LISTENER_BASE_URL = os.getenv(
19-
"TASK_LISTENER_BASE_URL", default="http://localhost:8000/_fastapi_cloud_tasks"
20-
)
16+
TASK_LISTENER_BASE_URL = os.getenv("TASK_LISTENER_BASE_URL", default="http://localhost:8000/_fastapi_cloud_tasks")
2117
TASK_PROJECT_ID = os.getenv("TASK_PROJECT_ID", default="sample-project")
2218
TASK_LOCATION = os.getenv("TASK_LOCATION", default="asia-south1")
2319
SCHEDULED_LOCATION = os.getenv("SCHEDULED_LOCATION", default="us-central1")
@@ -41,9 +37,7 @@
4137
location=SCHEDULED_LOCATION,
4238
)
4339

44-
TASK_OIDC_TOKEN = tasks_v2.OidcToken(
45-
service_account_email=TASK_SERVICE_ACCOUNT, audience=TASK_LISTENER_BASE_URL
46-
)
40+
TASK_OIDC_TOKEN = tasks_v2.OidcToken(service_account_email=TASK_SERVICE_ACCOUNT, audience=TASK_LISTENER_BASE_URL)
4741
SCHEDULED_OIDC_TOKEN = scheduler_v1.OidcToken(
4842
service_account_email=TASK_SERVICE_ACCOUNT, audience=TASK_LISTENER_BASE_URL
4943
)

examples/full/tasks.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,32 @@
22
import logging
33

44
# Third Party Imports
5-
from fastapi import Depends
6-
from fastapi import FastAPI
5+
from fastapi import Depends, FastAPI
76
from fastapi.routing import APIRouter
87
from google.protobuf import duration_pb2
98

109
# Imports from this repository
1110
from examples.full.serializer import Payload
12-
from examples.full.settings import CLOUD_TASKS_EMULATOR_URL
13-
from examples.full.settings import IS_LOCAL
14-
from examples.full.settings import SCHEDULED_LOCATION_PATH
15-
from examples.full.settings import SCHEDULED_OIDC_TOKEN
16-
from examples.full.settings import TASK_LISTENER_BASE_URL
17-
from examples.full.settings import TASK_OIDC_TOKEN
18-
from examples.full.settings import TASK_QUEUE_PATH
19-
from fastapi_cloud_tasks import DelayedRouteBuilder
20-
from fastapi_cloud_tasks.dependencies import CloudTasksHeaders
21-
from fastapi_cloud_tasks.dependencies import max_retries
22-
from fastapi_cloud_tasks.hooks import chained_hook
23-
from fastapi_cloud_tasks.hooks import deadline_delayed_hook
24-
from fastapi_cloud_tasks.hooks import deadline_scheduled_hook
25-
from fastapi_cloud_tasks.hooks import oidc_delayed_hook
26-
from fastapi_cloud_tasks.hooks import oidc_scheduled_hook
27-
from fastapi_cloud_tasks.scheduled_route import ScheduledRouteBuilder
28-
from fastapi_cloud_tasks.utils import emulator_client
11+
from examples.full.settings import (
12+
CLOUD_TASKS_EMULATOR_URL,
13+
IS_LOCAL,
14+
SCHEDULED_LOCATION_PATH,
15+
SCHEDULED_OIDC_TOKEN,
16+
TASK_LISTENER_BASE_URL,
17+
TASK_OIDC_TOKEN,
18+
TASK_QUEUE_PATH,
19+
)
20+
from fastapi_gcp_tasks import DelayedRouteBuilder
21+
from fastapi_gcp_tasks.dependencies import max_retries
22+
from fastapi_gcp_tasks.hooks import (
23+
chained_hook,
24+
deadline_delayed_hook,
25+
deadline_scheduled_hook,
26+
oidc_delayed_hook,
27+
oidc_scheduled_hook,
28+
)
29+
from fastapi_gcp_tasks.scheduled_route import ScheduledRouteBuilder
30+
from fastapi_gcp_tasks.utils import emulator_client
2931

3032
app = FastAPI()
3133

examples/simple/main.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
from pydantic.v1 import BaseModel
99

1010
# Imports from this repository
11-
from fastapi_cloud_tasks import DelayedRouteBuilder
12-
from fastapi_cloud_tasks.utils import emulator_client
13-
from fastapi_cloud_tasks.utils import queue_path
11+
from fastapi_gcp_tasks import DelayedRouteBuilder
12+
from fastapi_gcp_tasks.utils import emulator_client, queue_path
1413

1514
# set env var IS_LOCAL=false for your deployment environment
1615
IS_LOCAL = os.getenv("IS_LOCAL", "true").lower() == "true"
@@ -39,6 +38,8 @@
3938

4039

4140
class Payload(BaseModel):
41+
"""Basic payload from the api."""
42+
4243
message: str
4344

4445

fastapi_cloud_tasks/__init__.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

fastapi_cloud_tasks/decorators.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

fastapi_cloud_tasks/exception.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)