Skip to content

Commit

Permalink
Merge pull request #2 from evertoncastro/version002
Browse files Browse the repository at this point in the history
version 0.0.2
  • Loading branch information
evertoncastro committed Aug 5, 2022
2 parents 5568e81 + a1d31a4 commit 6c5c38d
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 221 deletions.
10 changes: 8 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ env

cloudbuild.yaml
.dockerignore
Makefile
test_app/Makefile
test_app/async_cloud_tasks

.env
.env

*.egg-info
build/
dist/

Makefile
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) George Lubaretsi
Copyright (c) Everton Castro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Async Cloud Tasks
## Use Cloud Tasks to defer function execution in your web applications :honey_pot:

### Inspired by [django-cloud-tasks](https://github.com/GeorgeLubaretsi/django-cloud-tasks), thanks to GeorgeLubaretsi 👏🏻👏🏻👏🏻


Integrate [Google Cloud Tasks](https://cloud.google.com/tasks/docs/add-task-queue) with web applications without the need to create an exclusive endpoint to handle tasks.
The package provides easy to use decorator to create task handlers.
App looks for tasks in ``cloud_tasks.py`` files in your installed applications and auto-registers them.


## Prerequisites
- Python 3.7+

## Dependencies
- [google-cloud-tasks](https://pypi.org/project/google-cloud-tasks/)

## Support

- IMPORTANT: Now it is only giving support for **Django** framework.

## Installation

Install the package from PyPi

```sh
pip install async-cloud-tasks
```

Add ``async_cloud_tasks`` to ``INSTALLED_APPS``:

```python
INSTALLED_APPS = (
# ...
'async_cloud_tasks',
# ...
)
```

Add configuration to your settings

```python
ASYNC_CLOUD_TASKS={
'gcp_location': '<GCP TASK QUEUE LOCATION>',
'gcp_project_name': '<GCP TASK QUEUE PROJECT NAME>',
'task_handler_root_url': '_tasks/',
'service_url': '<BASE SERVICE URL>',
'handler_secret': '<SECRET KEY TO BE USED FOR TASK HANDLER AUTHENTICATION>'
}

# This setting allows you to debug your cloud tasks by running actual task handler function locally
# instead of sending them to the task queue. Default: False
ASYNC_CLOUD_TASKS_EXECUTE_LOCALLY = False
```

Add cloud task views to your urls.py (must resolve to the same url as ``task_handler_root_url``)

```python
# urls.py
# ...
from django.urls import path, include
from async_cloud_tasks import urls as dct_urls

urlpatterns = [
# ...
path('_tasks/', include(dct_urls)),
]
```


## Quick start (only for Django)

Simply import the task decorator and define the task inside ``cloud_tasks.py`` in your app.
First parameter should always be ``request`` which is populated after task is executed by Cloud Task service.

You can get actual request coming from Cloud Task service by accessing ``request.request`` in your task body and
additional attributes such as: ``request.task_id```, ```request.request_headers```

```python
# cloud_tasks.py
# ...
from async_cloud_tasks.decorators import task

@task(queue='default')
def example_task(request, p1, p2):
print(p1, p2)
print(request.task_id)
```

Pushing the task to the queue:

```python
from my_app.cloud_tasks import example_task

example_task(p1='1', p2='2').defer()
```

Pushing remote task to the queue (when task handler is defined elsewhere):

```python
from async_cloud_tasks import remote_task
from async_cloud_tasks import batch_execute

example_task = remote_task(queue='my-queue', handler='remote_app.cloud_tasks.example_task'):
payload_1 = example_task(payload={'p1': 1, 'p2': '2'})
payload_1.defer()
```


It is also possible to run an actual function using ``run`` method of ``CloudTaskWrapper`` object instance that is returned after task is called (this can be useful for debugging):

```python
task = example_task(p1=i, p2=i)
task.run()
```


## References
- [CloudTasksClient](https://cloud.google.com/python/docs/reference/cloudtasks/latest/google.cloud.tasks_v2.services.cloud_tasks.CloudTasksClient)
159 changes: 0 additions & 159 deletions README.rst

This file was deleted.

2 changes: 1 addition & 1 deletion async_cloud_tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.utils.module_loading import autodiscover_modules
from .base import remote_task

__version__ = '0.0.1'
__version__ = '0.0.2'


def autodiscover():
Expand Down
35 changes: 35 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# pyproject.toml

[build-system]
requires = ["setuptools>=61.0.0", "wheel"]
build-backend = "setuptools.build_meta"


[tool.setuptools]
packages = ["async_cloud_tasks"]


[project]
name = "async-cloud-tasks"
version = "0.0.2"
description = "Use Cloud Tasks to defer function execution (forked from github.com/GeorgeLubaretsi/django-cloud-tasks)"
readme = "README.md"
authors = [{ name = "Everton Castro", email = "evertoncastro.sp@gmail.com" }]
license = { file = "LICENSE" }
classifiers = [
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
]
keywords = ["cloudtasks", "cloud", "tasks", "async", "defer"]
dependencies = [
"google-cloud-tasks>=2.9.0"
]
requires-python = ">=3.7"

[project.optional-dependencies]
dev = ["pip-tools", "pytest"]

[project.urls]
Homepage = "https://github.com/evertoncastro/async-cloud-tasks"
Loading

0 comments on commit 6c5c38d

Please sign in to comment.