Use decorator to keep track of group names for django-q2 tasks

This commit is contained in:
Adam Goldsmith 2024-02-01 11:10:22 -05:00
parent 3d7d428914
commit 8961542d14
7 changed files with 23 additions and 6 deletions

View File

@ -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

View File

@ -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,

View File

@ -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():

View File

@ -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",

View File

@ -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,

View File

@ -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(

View File

@ -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())