From 8961542d1434a875817f91ab6355f80d64181cdc Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Thu, 1 Feb 2024 11:10:22 -0500 Subject: [PATCH] Use decorator to keep track of group names for django-q2 tasks --- cmsmanage/django_q2_helper.py | 10 ++++++++++ doorcontrol/apps.py | 2 +- doorcontrol/tasks/scrapehidevents.py | 2 ++ membershipworks/admin.py | 4 ++-- membershipworks/apps.py | 6 +++--- membershipworks/tasks/scrape.py | 3 +++ membershipworks/tasks/ucsAccounts.py | 2 ++ 7 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cmsmanage/django_q2_helper.py b/cmsmanage/django_q2_helper.py index 533d49a..2f41faf 100644 --- a/cmsmanage/django_q2_helper.py +++ b/cmsmanage/django_q2_helper.py @@ -9,3 +9,13 @@ def ensure_scheduled(name: str, func, **kwargs): **kwargs, }, ) + + +def q_task_group(task_group: str): + """Decorator to more cleanly add `q_task_group` to a function""" + + def inner(func): + func.q_task_group = task_group + return func + + return inner diff --git a/doorcontrol/apps.py b/doorcontrol/apps.py index 340959a..4fb6d02 100644 --- a/doorcontrol/apps.py +++ b/doorcontrol/apps.py @@ -10,7 +10,7 @@ def post_migrate_callback(sender, **kwargs): from .tasks.scrapehidevents import q_getMessagesAllDoors ensure_scheduled( - "Fetch HID Events", + q_getMessagesAllDoors.q_task_group, q_getMessagesAllDoors, schedule_type=Schedule.MINUTES, minutes=15, diff --git a/doorcontrol/tasks/scrapehidevents.py b/doorcontrol/tasks/scrapehidevents.py index 9d93f4b..04a66d3 100644 --- a/doorcontrol/tasks/scrapehidevents.py +++ b/doorcontrol/tasks/scrapehidevents.py @@ -5,6 +5,7 @@ from django.utils import timezone from django_q.tasks import async_task +from cmsmanage.django_q2_helper import q_task_group from doorcontrol.models import Door, HIDEvent @@ -34,6 +35,7 @@ def getMessages(door: Door): } +@q_task_group("Fetch HID Events") def q_getMessagesAllDoors(): # TODO: this should probably use async_iter for door in Door.objects.all(): diff --git a/membershipworks/admin.py b/membershipworks/admin.py index 34affd1..2663be0 100644 --- a/membershipworks/admin.py +++ b/membershipworks/admin.py @@ -45,7 +45,7 @@ class BaseMembershipWorksAdmin(DjangoObjectActions, ReadOnlyAdmin): if tool_name == "refresh_membershipworks_data": try: last_run_time = naturaltime( - Task.objects.filter(group="Scrape Data from MembershipWorks") + Task.objects.filter(group=scrape_membershipworks.q_task_group) .values_list("started", flat=True) .latest("started") ) @@ -56,7 +56,7 @@ class BaseMembershipWorksAdmin(DjangoObjectActions, ReadOnlyAdmin): @action def refresh_membershipworks_data(self, request, obj): - async_task(scrape_membershipworks, group="Scrape Data from MembershipWorks") + async_task(scrape_membershipworks, group=scrape_membershipworks.q_task_group) self.message_user( request, "Queued refresh, please wait a few seconds/minutes then refresh the page", diff --git a/membershipworks/apps.py b/membershipworks/apps.py index 167b4e2..7f40f1c 100644 --- a/membershipworks/apps.py +++ b/membershipworks/apps.py @@ -11,19 +11,19 @@ def post_migrate_callback(sender, **kwargs): from .tasks.ucsAccounts import sync_accounts ensure_scheduled( - "Scrape MembershipWorks Data", + scrape_membershipworks.q_task_group, scrape_membershipworks, schedule_type=Schedule.HOURLY, ) ensure_scheduled( - "Scrape MembershipWorks Events", + scrape_events.q_task_group, scrape_events, schedule_type=Schedule.HOURLY, ) ensure_scheduled( - "Sync UCS Accounts", + sync_accounts.q_task_group, sync_accounts, schedule_type=Schedule.MINUTES, minutes=15, diff --git a/membershipworks/tasks/scrape.py b/membershipworks/tasks/scrape.py index 1f343ea..38b0ed1 100644 --- a/membershipworks/tasks/scrape.py +++ b/membershipworks/tasks/scrape.py @@ -5,6 +5,7 @@ from django.conf import settings from django.db import transaction from django.db.models import QuerySet +from cmsmanage.django_q2_helper import q_task_group from membershipworks.membershipworks_api import FieldType, MembershipWorks from membershipworks.models import ( Event, @@ -89,6 +90,7 @@ def scrape_transactions(membershipworks: MembershipWorks): Transaction.from_api_dict(csv_transaction).save() +@q_task_group("Scrape MembershipWorks Data") @transaction.atomic def scrape_membershipworks(*args, **options): membershipworks = MembershipWorks() @@ -111,6 +113,7 @@ def scrape_event_details(queryset: QuerySet[EventExt]): event.save() +@q_task_group("Scrape MembershipWorks Events") def scrape_events(): membershipworks = MembershipWorks() membershipworks.login( diff --git a/membershipworks/tasks/ucsAccounts.py b/membershipworks/tasks/ucsAccounts.py index 83689b1..318ffa2 100644 --- a/membershipworks/tasks/ucsAccounts.py +++ b/membershipworks/tasks/ucsAccounts.py @@ -8,6 +8,7 @@ from django.conf import settings from udm_rest_client.exceptions import NoObject, UdmError from udm_rest_client.udm import UDM +from cmsmanage.django_q2_helper import q_task_group from membershipworks.models import Flag, Member USER_BASE = "cn=users,dc=sawtooth,dc=claremontmakerspace,dc=org" @@ -132,5 +133,6 @@ async def async_accounts(): await sync_member(user_mod, member) +@q_task_group("Sync UCS Accounts") def sync_accounts(): asyncio.run(async_accounts())