From eeb83388e171e58be36b9b7820a968d76092d8a9 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 23 May 2024 23:23:35 -0400 Subject: [PATCH] Directly use `q_task_group` name in django-q2 `ensure_scheduled` helper --- cmsmanage/django_q2_helper.py | 6 ++++-- doorcontrol/apps.py | 2 -- doorcontrol/tasks/update_doors.py | 2 ++ membershipworks/apps.py | 4 ---- membershipworks/tasks/event_survey_emails.py | 2 ++ 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmsmanage/django_q2_helper.py b/cmsmanage/django_q2_helper.py index 2f41faf..5ebddd4 100644 --- a/cmsmanage/django_q2_helper.py +++ b/cmsmanage/django_q2_helper.py @@ -1,9 +1,11 @@ from django_q.models import Schedule -def ensure_scheduled(name: str, func, **kwargs): +def ensure_scheduled(func, **kwargs): + if not hasattr(func, "q_task_group"): + raise ValueError("task func not decorated with @q_task_group") Schedule.objects.update_or_create( - name=name, + name=func.q_task_group, defaults={ "func": f"{func.__module__}.{func.__qualname__}", **kwargs, diff --git a/doorcontrol/apps.py b/doorcontrol/apps.py index c528677..5ca5095 100644 --- a/doorcontrol/apps.py +++ b/doorcontrol/apps.py @@ -11,14 +11,12 @@ def post_migrate_callback(sender, **kwargs): from .tasks.update_doors import q_update_all_doors ensure_scheduled( - q_getMessagesAllDoors.q_task_group, q_getMessagesAllDoors, schedule_type=Schedule.MINUTES, minutes=15, ) ensure_scheduled( - "Update Door Controller Members and Cards", q_update_all_doors, schedule_type=Schedule.MINUTES, minutes=15, diff --git a/doorcontrol/tasks/update_doors.py b/doorcontrol/tasks/update_doors.py index da59ad4..077e485 100644 --- a/doorcontrol/tasks/update_doors.py +++ b/doorcontrol/tasks/update_doors.py @@ -4,6 +4,7 @@ from typing import TypedDict from django_q.tasks import async_task +from cmsmanage.django_q2_helper import q_task_group from doorcontrol.hid.Credential import Credential from doorcontrol.hid.DoorController import ROOT, E from doorcontrol.models import AttributeScheduleRule, Door, FlagScheduleRule @@ -266,6 +267,7 @@ def update_door(door: Door, dry_run: bool = False): # TODO: delete cardholders that are no longer members? +@q_task_group("Update Door Controller Members and Cards") def q_update_all_doors(): for door in Door.objects.all(): async_task( diff --git a/membershipworks/apps.py b/membershipworks/apps.py index a1a1f4d..ad1b52e 100644 --- a/membershipworks/apps.py +++ b/membershipworks/apps.py @@ -12,26 +12,22 @@ def post_migrate_callback(sender, **kwargs): from .tasks.ucsAccounts import sync_accounts ensure_scheduled( - scrape_membershipworks.q_task_group, scrape_membershipworks, schedule_type=Schedule.HOURLY, ) ensure_scheduled( - scrape_events.q_task_group, scrape_events, schedule_type=Schedule.HOURLY, ) ensure_scheduled( - sync_accounts.q_task_group, sync_accounts, schedule_type=Schedule.MINUTES, minutes=15, ) ensure_scheduled( - sync_accounts.q_task_group, send_survey_emails, schedule_type=Schedule.HOURLY, ) diff --git a/membershipworks/tasks/event_survey_emails.py b/membershipworks/tasks/event_survey_emails.py index 79dd254..f13d486 100644 --- a/membershipworks/tasks/event_survey_emails.py +++ b/membershipworks/tasks/event_survey_emails.py @@ -6,6 +6,7 @@ from django.conf import settings from django.core import mail from django.db.models.functions import Now +from cmsmanage.django_q2_helper import q_task_group from cmsmanage.email import TemplatedMultipartEmail from membershipworks.models import EventExt @@ -58,6 +59,7 @@ class EventSurveyEmail(TemplatedMultipartEmail): ) +@q_task_group("Send Event Survey Emails") def send_survey_emails(): with mail.get_connection() as conn: for event in EventExt.objects.filter(