From a1d31a4f5e089a69614877ce4a59f6a2bf74c388 Mon Sep 17 00:00:00 2001 From: Everton Castro Date: Fri, 5 Aug 2022 19:53:15 -0300 Subject: [PATCH] version 0.0.2 --- .gitignore | 10 ++- LICENSE | 2 +- README.md | 126 +++++++++++++++++++++++++++ README.rst | 159 ---------------------------------- async_cloud_tasks/__init__.py | 2 +- pyproject.toml | 35 ++++++++ setup.py | 60 +------------ test_app/test_app/settings.py | 2 +- 8 files changed, 175 insertions(+), 221 deletions(-) create mode 100644 README.md delete mode 100644 README.rst create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore index f6ad23d..faeb574 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,13 @@ env cloudbuild.yaml .dockerignore -Makefile +test_app/Makefile test_app/async_cloud_tasks -.env \ No newline at end of file +.env + +*.egg-info +build/ +dist/ + +Makefile \ No newline at end of file diff --git a/LICENSE b/LICENSE index 2d12a29..06b175e 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..d9a25dd --- /dev/null +++ b/README.md @@ -0,0 +1,126 @@ +# Async Cloud Tasks + +### 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/) + +## Documentation + +- IMPORTANT: Now it is only giving support do **Django** framework. + + +## Installation + +(1) Install the package from PyPi +``` +pip install async-cloud-tasks +``` + +(2) Add ``async_cloud_tasks`` to ``INSTALLED_APPS``: + +```python +INSTALLED_APPS = ( + # ... + 'async_cloud_tasks', + # ... +) +``` + + + +(3) Add configuration to your settings + +```python +ASYNC_CLOUD_TASKS={ + 'gcp_location': '', + 'gcp_project_name': '', + 'task_handler_root_url': '_tasks/', + 'service_url': '', + 'handler_secret': '' +} + +# 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 +``` + +(4) 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 now) + +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').execute() +``` + +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.execute() +``` + + +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) \ No newline at end of file diff --git a/README.rst b/README.rst deleted file mode 100644 index 5f2281e..0000000 --- a/README.rst +++ /dev/null @@ -1,159 +0,0 @@ -============================ -Django Cloud Tasks -============================ -Integrate `Google Cloud Tasks `_ with Django. - -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. - -Package is in early alpha and it does not have any real security at the moment. You need to authorize requests coming -to your instances yourself. - -Prerequisites -============= - -- Django 1.8+ -- Python 3.4, 3.5, 3.6 - -Dependencies -============ - -- `google-api-python-client `_ - -Documentation -============= - -TODO - -Installation -============ - -(1) Install latest version from Github (PyPy package will be available as soon as stable version is released): - - .. code-block:: sh - - pip install -e git+git@github.com:GeorgeLubaretsi/django-cloud-tasks.git#egg=django_cloud_tasks - - -(2) Add ``django_cloud_tasks`` to ``INSTALLED_APPS``: - - .. code-block:: python - - INSTALLED_APPS = ( - # ... - 'django_cloud_tasks', - # ... - ) - - -(3) Add configuration to your settings - - .. code-block:: python - - DJANGO_CLOUD_TASKS={ - 'project_location_name': 'projects/{project_name}/locations/us-central1', - 'task_handler_root_url': '/_tasks/', - }, - - # 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 - DJANGO_CLOUD_TASKS_EXECUTE_LOCALLY = False - - # If False, running `.execute()` on remote task will simply log the task data instead of adding it to - # the queue. Useful for debugging. Default: True - DJANGO_CLOUD_TASKS_BLOCK_REMOTE_TASKS = False - - -(4) Add cloud task views to your urls.py (must resolve to the same url as ``task_handler_root_url``) - - .. code-block:: python - - # urls.py - # ... - from django.urls import path, include - from django_cloud_tasks import urls as dct_urls - - urlpatterns = [ - # ... - path('_tasks/', include(dct_urls)), - ] - - - -Quick start -=========== - -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``` - -.. code-block:: python - - # cloud_tasks.py - # ... - from django_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: - -.. code-block:: python - - from my_app.cloud_tasks import example_task - - example_task(p1='1', p2='2').execute() - - -Pushing remote task to the queue (when task handler is defined elsewhere): - -.. code-block:: python - - from django_cloud_tasks import remote_task - from django_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_2 = example_task(payload={'p1': 2, 'p2': '3'}) - - # Execute in batch: - batch_execute([payload_1, payload_2]) - - # Or one by one: - payload_1.execute() - payload_2.execute() - - -You can also send tasks in batch if latency is an issue and you have to send many small tasks to the queue -(limited to 1000 at a time): - -.. code-block:: python - - from my_app.cloud_tasks import example_task - from django_cloud_tasks import batch_execute - - tasks = [] - for i in range(0, 420): - task = example_task(p1=i, p2=i) - tasks.append(task) - - batch_execute(tasks) - - - -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): - -.. code-block:: python - - task = example_task(p1=i, p2=i) - task.run() - - - -- https://cloud.google.com/python/docs/reference/cloudtasks/latest/google.cloud.tasks_v2.services.cloud_tasks.CloudTasksClient \ No newline at end of file diff --git a/async_cloud_tasks/__init__.py b/async_cloud_tasks/__init__.py index 1365809..b0815bf 100644 --- a/async_cloud_tasks/__init__.py +++ b/async_cloud_tasks/__init__.py @@ -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(): diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..fd32dfb --- /dev/null +++ b/pyproject.toml @@ -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 = "Google Cloud Tasks to run functions as tasks (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" diff --git a/setup.py b/setup.py index 47fdbba..e546293 100644 --- a/setup.py +++ b/setup.py @@ -1,59 +1,5 @@ -import os -import sys +# setup.py -try: - from setuptools import setup -except ImportError: - from distutils.core import setup +from setuptools import setup -version = '0.0.4' - -if sys.argv[-1] == 'publish': - try: - import wheel - print("Wheel version: ", wheel.__version__) - except ImportError: - print('Wheel library missing. Please run "pip install wheel"') - sys.exit() - os.system('python setup.py sdist upload') - os.system('python setup.py bdist_wheel upload') - sys.exit() - -if sys.argv[-1] == 'tag': - print("Tagging the version on git:") - os.system("git tag -a %s -m 'version %s'" % (version, version)) - os.system("git push --tags") - sys.exit() - -readme = open('README.rst').read() - -setup( - name='django-cloud-tasks', - version=version, - description="""Google Cloud Tasks integration for Django""", - long_description=readme, - author='GeorgeLubaretsi', - url='https://github.com/GeorgeLubaretsi/django-cloud-tasks', - packages=[ - 'async_cloud_tasks', - ], - include_package_data=True, - install_requires=[ - 'google-api-python-client>=1.6.4', - ], - license="MIT", - zip_safe=False, - keywords='django cloudtasks cloud tasks', - classifiers=[ - 'Framework :: Django', - 'Framework :: Django :: 1.11', - 'Framework :: Django :: 2.0', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: BSD License', - 'Natural Language :: English', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.4', - 'Programming Language :: Python :: 3.5', - 'Programming Language :: Python :: 3.6', - ], -) +setup() \ No newline at end of file diff --git a/test_app/test_app/settings.py b/test_app/test_app/settings.py index 0c6f22c..fd8f853 100644 --- a/test_app/test_app/settings.py +++ b/test_app/test_app/settings.py @@ -133,7 +133,7 @@ 'gcp_project_name': project, 'task_handler_root_url': '_tasks/', 'service_url': 'https://dct-test-app-3evazfo34q-uc.a.run.app', - # 'handler_secret': 'aba' + 'handler_secret': None } # This setting allows you to debug your cloud tasks by running actual task handler function locally