{"id":19940704,"url":"https://github.com/uhmiller/dj-cloud-task","last_synced_at":"2025-10-14T09:19:33.795Z","repository":{"id":43786954,"uuid":"413829780","full_name":"uhmiller/dj-cloud-task","owner":"uhmiller","description":"Django Cloud Task Queue. Integrate your Django Application with Google Cloud Task from Google Cloud Platform","archived":false,"fork":false,"pushed_at":"2024-05-04T21:22:08.000Z","size":60,"stargazers_count":12,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-30T14:12:23.373Z","etag":null,"topics":["asynchronous-tasks","cloudtasks","django","google-cloud","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/uhmiller.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-05T13:29:01.000Z","updated_at":"2025-09-03T17:01:41.000Z","dependencies_parsed_at":"2024-01-07T18:08:19.140Z","dependency_job_id":"e9dd7535-9da7-490c-bdb9-551050ed6211","html_url":"https://github.com/uhmiller/dj-cloud-task","commit_stats":{"total_commits":21,"total_committers":1,"mean_commits":21.0,"dds":0.0,"last_synced_commit":"b5d22528408d0152046093de6dcd0a9a6e66fbd4"},"previous_names":["txiocoder/dj-cloud-task","uhmiller/dj-cloud-task","uhttred/dj-cloud-task"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/uhmiller/dj-cloud-task","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uhmiller%2Fdj-cloud-task","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uhmiller%2Fdj-cloud-task/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uhmiller%2Fdj-cloud-task/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uhmiller%2Fdj-cloud-task/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/uhmiller","download_url":"https://codeload.github.com/uhmiller/dj-cloud-task/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/uhmiller%2Fdj-cloud-task/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279018568,"owners_count":26086394,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-14T02:00:06.444Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asynchronous-tasks","cloudtasks","django","google-cloud","python"],"created_at":"2024-11-13T00:06:32.140Z","updated_at":"2025-10-14T09:19:33.762Z","avatar_url":"https://github.com/uhmiller.png","language":"Python","readme":"# Django Cloud Task Queue\n\nIntegrate your [Django](https://www.djangoproject.com/) application with Google [Cloud Task](https://cloud.google.com/tasks) from [Google Cloud Platform](https://cloud.google.com/). This package provides a simple and easy to use decorator to push tasks to Cloud Task Queue and automatically handle all income task execution requests from Cloud Task. In a single entry point.\n\nAt the moment dj-cloud-task only works with HTTP targets such as Cloud Run, Cloud Functions, Compute Engine, ... (in case you are using Google Cloud Platform infrastructure). See the Cloud Task documentation about targets for more [here](https://cloud.google.com/tasks/docs/creating-http-target-tasks)!\n\n### Some Features\n\n* Easily push tasks to Cloud Task using a decorator\n* Automatically route all tasks from a single endpoint\n* Ease scheduling with native python datetime\n* Named task to avoid duplicated\n* Local development support with Redis Queue\n\n### Installation\n\nSimple **dj-cloud-task** can be installed with ``pip``:\n\n    pip install dj-cloud-task\n\nOr from this repository:\n\n    pip install -e git+https://github.com/txiocoder/dj-cloud-task.git@main#egg=cloudtask\n\n\n#### Requirements\n\n* [Python](https://www.python.org/) \u003e= 3.9\n* [django](https://www.djangoproject.com/) \u003e= 3.2.*\n* [google-cloud-tasks](https://pypi.org/project/google-cloud-tasks/) \u003e= 2.5.*\n\nThese are the officially supported python and packages versions. Other versions will probably work\n\n## Quickstart\n\n### Configure\n\nAs stated above, Django Cloud Task is a Django Application. To configure your project you simply need to add ``cloudtask`` to your ``INSTALLED_APPS`` and configure the ``CLOUDTASK`` variable in the ``settings.py`` file. More details about how to configure the CLOUDTASK variable below.\n\nIn file ``settings.py``:\n\n```python\nINSTALLED_APPS: list = [\n    'cloudtask',\n]\n\nCLOUDTASK: dict = {\n    'PROJECT': 'project',\n    'LOCATION': 'europe-west6',\n    'SAE': 'user@project.iam.gserviceaccount.com',\n    'URL': 'https://handler.com/_tasks/',\n    'QUEUE': 'default',\n    'SECRET': 'my-very-secrete-key'\n}\n```\n\nThen, add ``cloudtask.urls`` to your URL configuration to route and handle all task execution requests coming from Cloud Task.\n\nIn your ``project.urls``:\n\n```python\nfrom django.urls import path, include\n\nurlpatterns: list = [\n    path('_tasks/', include('cloudtask.urls'))\n]\n```\n\n### Create Tasks\n\nThe tasks are simple python functions that could be defined anywhere. But, I suggest you create a file with the name ``tasks.py`` in your django module/app and declare there. To create a task, simply decorate a function with ``cloudtask.decorators.task``. See the example below: \n\n```python\nfrom cloudtask import (\n    CloudTaskRequest,\n    task)\n\n@task(queue='default')\ndef add(request: CloudTaskRequest, a: int = 5, b: int = 4) -\u003e None:\n    print(f'Running task with args {a=} and {b=}')\n    print(a + b)\n```\n\n### Run Tasks\n\nTo send a task to Cloud Task call the task function and then call the delay method inside the returned task instance. This will send a request to Cloud Task to enqueue the task and Cloud Task will request to run it as fast as possible.\n\n```python\nfrom .tasks import add\n\nadd(a=10, b=30).delay()\n# or use the alias push\nadd(a=30, b=10).push()\n```\n\n## GCP Authentication\n\nThis module requires to be authenticated with Google Cloud Platform as a service. The GC Platform provides various ways to authenticate with it. See the GC Platform page about authentication strategies [here](https://cloud.google.com/docs/authentication/production).\n\n\n## Configurations\n\nIn this session you will see how to configure cloudtask. We have required attributes, optional but required in task declaration and  only optional attributes.\nThe required attributes are **PROJECT**, **LOCATION** and **SAE**.\n\nHere the details about all attributes accpeted in ``CLOUDTASK``\n\n| Attribute    | Type    | Required  | Description                            |\n|--------------|---------|-----------|----------------------------------------|\n| PROJECT      | ``str`` | ``True``  | Project ID from Google Cloud Platform  |\n| LOCATION     | ``str`` | ``True``  | Cloud Task Queue Location              |\n| SAE          | ``str`` | ``True``  | Service Account Email                  |\n| URL          | ``str`` | ``False`` | Default URL                            |\n| QUEUE        | ``srt`` | ``False`` | Default Queue Name                     |\n| SECRET       | ``str`` | ``False`` | Secret key to authorize task execution |\n| LOCAL_RQ     | ``bool``| ``False`` | Use Redis Queue to handle tasks locally|\n| LOCAL_RQ_URL | ``str`` | ``False`` | Optional Redis connection URL          |\n| TESTING      | ``bool``| ``False`` | Testing Mode                           |\n\n#### URL attribute\n\nThe URL attribute is optional, but if you don't set you will need to explicitly pass as  a task decorator argument\n\n```python\nfrom cloudtask import task\n\n@task(queue='emails', url='https://mysite.com/emails-tasks')\ndef send_email(request, to: str):\n    pass\n```\n\nYou can change the ``url`` of task instance in runtime if you need things to be done more automated.\n\n```python\nfrom .tasks import send_email\n\ntask = send_email(to='a@a.com')\ntask.url = 'https://mysite.com/tasks'\ntask.push()\n```\n\n#### QUEUE attribute\n\nThe same as the URL attribute, the QUEUE attribute is optional, and if you don't set it you will need to explicitly pass as a task decorator argument. You can not change the QUEUE of task instance at runtime.\n\n```python\nfrom cloudtask import task\n\n@task(queue='default')\ndef send_email(request, to: str):\n    pass\n```\n\n#### TESTING attribute\n\nUseful when testing your django application. If True will run all tasks immediately without push to Cloud Task Queue\n\n## Working with Tasks\n\nIn this session you will see how to play with tasks. Django Cloud Task provides not just one way to create, push and handle tasks. It's flexible! All defined tasks receive the ``request`` keyword argument which is an instance of ``cloudtask.tasks.CloudTaskRequest`` containing all request information from Cloud Task\n\n### Creating and Push\n\n```python\nfrom cloudtask import (\n    CloudTaskRequest,\n    task)\n\n@task()\ndef say_yes(rquest) -\u003e None:\n    print('Yes')\n\n@task(queue='default', named=True)\ndef add(request: CloudTaskRequest, a: int = 5, b: int = 4) -\u003e None:\n    print(f'Running task with args {a=} and {b=}')\n    print(a + b)\n\n# Pushing to Cloud Task\n\nsay_yes().delay() # or with push\nadd().push()\n```\n\n\n#### Immediately Execute a Task\n\nSometimes you will need to execute the task function immediately (without pushing to Cloud Task), to do that, just call the ``__cal__`` or ``execute`` method from the returned task instance. The request will have limited information.\n\n```python\n\nfrom .tasks import add\nadd(a=5, b=3).__call__()\n# or using the alias\nadd(a=5, b=3)()\n# or using the execute method\nadd(a=5, b=3).execute()\n```\n#### Scheduling\n\nYou can schedule a task to be delivered later using native python datetime.\n\n```python\nfrom datetime import timedelta\nfrom django.utils.timezone import now\nfrom .tasks import add\n\nat = now() + timedelta(days=2)\nadd(a=3, b=6).schedule(at=at)\n```\n\n#### Named Tasks\n\n```python\nfrom .tasks import add\n\nadd(a=3, b=6).delay()\nadd(a=3, b=6).delay()\nadd(a=3, b=6).delay()\n```\n\nBy default, the above will run normally. Cloud Task by default adds a unique name for each new task. That makes it possible to have duplicated tasks in the queue, even with the same arguments. If you want a task to only be enqueued once at time, you have to set the task as a named task.\n\nDjango Cloud Task will give a task name based on the task function name.\n\n```python\nfrom cloudtask import (\n    CloudTaskRequest,\n    task)\n\n@task(named=True)\ndef clean_expired(request: CloudTaskRequest):\n    pass\n\nclean_expired().delay()\nclean_expired().delay() # this line will raise an entity error by Cloud Task\n```\n\nYou can also set the name in ``task`` decorator or dynamically. This feature is very useful when you want to do some recursive tasks.\n\n```python\nfrom cloudtask.tasks import Task\nfrom cloudtask import (\n    CloudTaskRequest,\n    task)\n\n\n@task(named='SOME_UNIQUE_NAME')\ndef task_do_some(request: CloudTaskRequest):\n    pass\n\n# or dynamically\n\n@task()\ndef delete_article(request: CloudTaskRequest, article_id: int):\n    pass\n\narticle_id: int = 34\ntask: Task = delete_article(article_id=article_id)\ntask.named = f'DELETE_ARTICLE_{article_id}'\ntask.push()\n```\n\n## Local development support\n\n### Setup\n\nUse Redis Queue for local development support. To start, first install [rq](https://python-rq.org/), [rq-scheduler](https://github.com/rq/rq-scheduler) and [requests](https://docs.python-requests.org/en/latest/) with pip. You will need a Redis connection too. Then configure on your ``CLOUDTASK`` settings.\n\n```sh\npip install rq requests rq-scheduler\n```\n\n### Configurations\n\nOn ``CLOUDTASK`` settings set ``LOCAL_RQ`` as ``True`` to start handle the tasks locally.\n\n```python\nCLOUDTASK: dict = {\n    'LOCAL_RQ': True\n}\n```\n\nYou can use ``LOCAL_RQ_URL`` to change the default redis connection string\n\n```python\nCLOUDTASK: dict = {\n    'LOCAL_RQ': True,\n    'LOCAL_RQ_URL': 'redis://localhost:6379' # default by redis\n}\n```\n\nThat is all, but do not forget to set the right local ``URL`` on your ``CLOUDTASK`` settings to  handle the tasks.\n\n### Running Tasks Locally\n\nTo start running task locally just start the worker process with the available management command\n\n```sh\npython manage.py cloudtask-worker\n```\n\nIf you have task that are scheduled, start the ``rqscheduler`` worker process to support scheduling tasks. In your project root dir:\n\n```sh\nrqscheduler\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuhmiller%2Fdj-cloud-task","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fuhmiller%2Fdj-cloud-task","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fuhmiller%2Fdj-cloud-task/lists"}