Skip to content

Commit 9a2125a

Browse files
authored
Merge pull request #68 from octue/tasks-domain-flag
Allow tasks domain flag to be set dynamically in management command
2 parents 21b4d7e + 1f889a5 commit 9a2125a

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

django_gcp/management/commands/task_manager.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from django.conf import settings
2+
from django.test import override_settings
13
from django_gcp.exceptions import UnknownActionError
24

35
from ._base import BaseCommand
@@ -23,22 +25,31 @@ def add_arguments(self, parser):
2325
help="Clean up unused resources whose name is affixed with GCP_TASKS_RESOURCE_AFFIX",
2426
)
2527

28+
parser.add_argument(
29+
"--tasks-domain",
30+
type=str,
31+
help="Optionally specify a domain to which the tasks will be sent. Overrides the default GCP_TASKS_DOMAIN value.",
32+
)
33+
2634
def handle(self, actions, **options):
2735

2836
cleanup = options["cleanup"]
37+
tasks_domain = options["tasks_domain"] or settings["GCP_TASKS_DOMAIN"]
38+
39+
with override_settings(GCP_TASKS_DOMAIN=tasks_domain):
40+
41+
for action in actions:
42+
if action == "create_scheduler_jobs":
43+
updated, deleted = self.task_manager.create_scheduler_jobs(cleanup=cleanup)
44+
report = [f"[+] {name}" for name in updated] + [f"[-] {name}" for name in deleted]
45+
self.display_task_report(report, "create", "scheduler jobs")
46+
47+
elif action == "create_pubsub_subscriptions":
48+
updated, deleted = self.task_manager.create_pubsub_subscriptions(cleanup=cleanup)
49+
report = [f"[+] {name}" for name in updated] + [f"[-] {name}" for name in deleted]
50+
self.display_task_report(report, "create", "pubsub subscriptions")
2951

30-
for action in actions:
31-
if action == "create_scheduler_jobs":
32-
updated, deleted = self.task_manager.create_scheduler_jobs(cleanup=cleanup)
33-
report = [f"[+] {name}" for name in updated] + [f"[-] {name}" for name in deleted]
34-
self.display_task_report(report, "create", "scheduler jobs")
35-
36-
elif action == "create_pubsub_subscriptions":
37-
updated, deleted = self.task_manager.create_pubsub_subscriptions(cleanup=cleanup)
38-
report = [f"[+] {name}" for name in updated] + [f"[-] {name}" for name in deleted]
39-
self.display_task_report(report, "create", "pubsub subscriptions")
40-
41-
else:
42-
raise UnknownActionError(
43-
f"Unknown action {action}. Use `python manage.py task_manager --help` to see all options"
44-
)
52+
else:
53+
raise UnknownActionError(
54+
f"Unknown action {action}. Use `python manage.py task_manager --help` to see all options"
55+
)

docs/source/tasks_usage.rst

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,30 @@ Note that this requires the task classes to be imported in ``tasks/__init__.py``
2929

3030
Scheduling periodic tasks
3131
-------------------------
32-
Periodic tasks are triggered by cronjobs in Google Cloud Scheduler. To create these jobs, the ``create_scheduler_jobs``
33-
management command must be run.
32+
Periodic tasks are triggered by cronjobs in Google Cloud Scheduler.
33+
To create these resources, you may wish to manage them directly with
34+
terraform but it's possible to create the resources using the ``create_scheduler_jobs``
35+
action from the ``task_manager``` management command:
36+
37+
.. code-block::
38+
39+
# Note: use the --task-domain flag to override the domain where tasks will get sent
40+
python manage.py task manager create_scheduler_jobs
41+
42+
.. attention::
43+
44+
To register these resources, your service account will need to have ``cloudscheduler.update`` permission. Here's how to apply that to a service account using terraform:
45+
46+
.. code-block::
47+
48+
# Allow django-gcp tasks to create periodic tasks in google cloud scheduler
49+
resource "google_project_iam_binding" "cloudscheduler_admin" {
50+
project = var.project
51+
role = "roles/cloudscheduler.admin"
52+
members = [
53+
"serviceAccount:[email protected]",
54+
]
55+
}
3456
3557
Setting up subscriber tasks
3658
---------------------------

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-gcp"
3-
version = "0.11.5"
3+
version = "0.12.0"
44
description = "Utilities to run Django on Google Cloud Platform"
55
authors = ["Tom Clark"]
66
license = "MIT"

0 commit comments

Comments
 (0)