Skip to content

Commit

Permalink
Merge pull request #68 from octue/tasks-domain-flag
Browse files Browse the repository at this point in the history
Allow tasks domain flag to be set dynamically in management command
  • Loading branch information
thclark authored May 23, 2024
2 parents 21b4d7e + 1f889a5 commit 9a2125a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
41 changes: 26 additions & 15 deletions django_gcp/management/commands/task_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.conf import settings
from django.test import override_settings
from django_gcp.exceptions import UnknownActionError

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

parser.add_argument(
"--tasks-domain",
type=str,
help="Optionally specify a domain to which the tasks will be sent. Overrides the default GCP_TASKS_DOMAIN value.",
)

def handle(self, actions, **options):

cleanup = options["cleanup"]
tasks_domain = options["tasks_domain"] or settings["GCP_TASKS_DOMAIN"]

with override_settings(GCP_TASKS_DOMAIN=tasks_domain):

for action in actions:
if action == "create_scheduler_jobs":
updated, deleted = self.task_manager.create_scheduler_jobs(cleanup=cleanup)
report = [f"[+] {name}" for name in updated] + [f"[-] {name}" for name in deleted]
self.display_task_report(report, "create", "scheduler jobs")

elif action == "create_pubsub_subscriptions":
updated, deleted = self.task_manager.create_pubsub_subscriptions(cleanup=cleanup)
report = [f"[+] {name}" for name in updated] + [f"[-] {name}" for name in deleted]
self.display_task_report(report, "create", "pubsub subscriptions")

for action in actions:
if action == "create_scheduler_jobs":
updated, deleted = self.task_manager.create_scheduler_jobs(cleanup=cleanup)
report = [f"[+] {name}" for name in updated] + [f"[-] {name}" for name in deleted]
self.display_task_report(report, "create", "scheduler jobs")

elif action == "create_pubsub_subscriptions":
updated, deleted = self.task_manager.create_pubsub_subscriptions(cleanup=cleanup)
report = [f"[+] {name}" for name in updated] + [f"[-] {name}" for name in deleted]
self.display_task_report(report, "create", "pubsub subscriptions")

else:
raise UnknownActionError(
f"Unknown action {action}. Use `python manage.py task_manager --help` to see all options"
)
else:
raise UnknownActionError(
f"Unknown action {action}. Use `python manage.py task_manager --help` to see all options"
)
26 changes: 24 additions & 2 deletions docs/source/tasks_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,30 @@ Note that this requires the task classes to be imported in ``tasks/__init__.py``

Scheduling periodic tasks
-------------------------
Periodic tasks are triggered by cronjobs in Google Cloud Scheduler. To create these jobs, the ``create_scheduler_jobs``
management command must be run.
Periodic tasks are triggered by cronjobs in Google Cloud Scheduler.
To create these resources, you may wish to manage them directly with
terraform but it's possible to create the resources using the ``create_scheduler_jobs``
action from the ``task_manager``` management command:

.. code-block::
# Note: use the --task-domain flag to override the domain where tasks will get sent
python manage.py task manager create_scheduler_jobs
.. attention::

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:

.. code-block::
# Allow django-gcp tasks to create periodic tasks in google cloud scheduler
resource "google_project_iam_binding" "cloudscheduler_admin" {
project = var.project
role = "roles/cloudscheduler.admin"
members = [
"serviceAccount:your-service-account@your-project.iam.gserviceaccount.com",
]
}
Setting up subscriber tasks
---------------------------
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-gcp"
version = "0.11.5"
version = "0.12.0"
description = "Utilities to run Django on Google Cloud Platform"
authors = ["Tom Clark"]
license = "MIT"
Expand Down

0 comments on commit 9a2125a

Please sign in to comment.