Package: django-cloud-tasks-withcredentials
Forked fromhttps://github.com/GeorgeLubaretsi/django-cloud-tasksIntegrateGoogle Cloud Taskswith Django.Package provides easy to use decorator to create task handlers.App looks for tasks incloud_tasks.pyfiles 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.PrerequisitesDjango 1.8+Python 3.4, 3.5, 3.6Dependenciesgoogle-api-python-clientDocumentationTODOInstallationInstall latest version from Github (PyPy package will be available as soon as stable version is released):pipinstall-egit+git@github.com:rgutierrez-cotech/django-cloud-tasks.git#egg=django_cloud_tasksAdddjango_cloud_taskstoINSTALLED_APPS:INSTALLED_APPS=(# ...'django_cloud_tasks',# ...)Optionally, set up Google Cloud credentials in your settings.py for use with Django Cloud Tasks. If you are using a service account, make sure the account has the “Cloud Tasks Admin” permission.# install these if they are not already installedimportgoogle.authfromgoogle.oauth2importservice_account# ...# To use application default credentials:google_cloud_credentials,project=google.auth.default()# To use a service account JSON file:google_cloud_credentials=service_account.Credentials.from_service_account_file('/path/to/key.json')Add configuration to your settingsDJANGO_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: FalseDJANGO_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: TrueDJANGO_CLOUD_TASKS_BLOCK_REMOTE_TASKS=False# Optional argument. Specify a google.auth.credentials.Credentials object to use with the API# Discovery service. Can be application default credentials or credentials generated from a# service account JSON file. Default: NoneDJANGO_CLOUD_TASKS_CREDENTIALS=None# or google_cloud_credentials if you defined this aboveAdd cloud task views to your urls.py (must resolve to the same url astask_handler_root_url)# urls.py# ...fromdjango.urlsimportpath,includefromdjango_cloud_tasksimporturlsasdct_urlsurlpatterns=[# ...path('_tasks/',include(dct_urls)),]Quick startSimply import the task decorator and define the task insidecloud_tasks.pyin your app.
First parameter should always berequestwhich is populated after task is executed by Cloud Task service.You can get actual request coming from Cloud Task service by accessingrequest.requestin your task body and
additional attributes such as:request.task_id`,`request.request_headers`# cloud_tasks.py# ...fromdjango_cloud_tasks.decoratorsimporttask@task(queue='default')defexample_task(request,p1,p2):print(p1,p2)print(request.task_id)Pushing the task to the queue:frommy_app.cloud_tasksimportexample_taskexample_task(p1='1',p2='2').execute()Pushing remote task to the queue (when task handler is defined elsewhere):fromdjango_cloud_tasksimportremote_taskfromdjango_cloud_tasksimportbatch_executeexample_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):frommy_app.cloud_tasksimportexample_taskfromdjango_cloud_tasksimportbatch_executetasks=[]foriinrange(0,420):task=example_task(p1=i,p2=i)tasks.append(task)batch_execute(tasks)It is also possible to run an actual function usingrunmethod ofCloudTaskWrapperobject instance that is returned after task is called (this can be useful for debugging):task=example_task(p1=i,p2=i)task.run()