from datetime import datetime from django.conf import settings from django.db import transaction from django.utils import timezone from django_q.tasks import async_task from django_q.models import Schedule from cmsmanage.django_q2_helper import ensure_scheduled from doorcontrol.models import Door, HIDEvent from doorcontrol.hid.DoorController import DoorController @transaction.atomic() def getMessages(door: Door): last_event = door.hidevent_set.order_by("timestamp").last() if last_event is not None: last_ts = timezone.make_naive(last_event.timestamp) else: last_ts = datetime(2010, 1, 1) door_controller = DoorController( door.ip, settings.HID_DOOR_USERNAME, settings.HID_DOOR_PASSWORD, ) for events_page in door_controller.get_events(last_ts): print(f"Importing {len(events_page)} events for {door.name}") HIDEvent.objects.bulk_create( (HIDEvent.from_xml_attributes(door, event.attrib) for event in events_page), ignore_conflicts=True, ) def q_getMessagesAllDoors(): # TODO: this should probably use async_iter for door in Door.objects.all(): async_task( getMessages, door, cluster="internal", group=f"Fetch HID Events - {door.name}", ) def schedule_tasks(): ensure_scheduled( "Fetch HID Events", q_getMessagesAllDoors, schedule_type=Schedule.MINUTES, minutes=15, )