diff --git a/shiftregister/app/models.py b/shiftregister/app/models.py index 2e80da6..76146d9 100644 --- a/shiftregister/app/models.py +++ b/shiftregister/app/models.py @@ -88,13 +88,6 @@ class ShiftRegistration(models.Model): self.reminder_sent = True self.save() - def get_unnotified_registrations(): - return ShiftRegistration.objects.filter( - reminder_sent=False, - shift__start_at__lte=timezone.now() - + global_preferences["helper__reminder_time"], - ) - def __str__(self): return f"{self.helper.name}: {self.shift}" diff --git a/shiftregister/app/tasks.py b/shiftregister/app/tasks.py index 98a244d..02dce9e 100644 --- a/shiftregister/app/tasks.py +++ b/shiftregister/app/tasks.py @@ -1,5 +1,5 @@ from celery import shared_task -from .models import Message +from .models import Message, ShiftRegistration from django.db import transaction from django.utils import timezone from dynamic_preferences.registries import global_preferences_registry @@ -36,3 +36,14 @@ def send_message(msgid): print(f"TODO: send message @{msg.to.phone} {msg.text}") msg.sent_at = timezone.now() msg.save() + + +@shared_task +def send_reminders(): + with transaction.atomic(): + for reg in ShiftRegistration.objects.select_for_update().filter( + reminder_sent=False, + shift__start_at__lte=timezone.now() + + global_preferences["helper__reminder_time"], + ): + reg.send_reminder() diff --git a/shiftregister/settings.py b/shiftregister/settings.py index e56c8a1..b30b841 100644 --- a/shiftregister/settings.py +++ b/shiftregister/settings.py @@ -154,6 +154,14 @@ CELERY_BEAT_SCHEDULE = { "task": "shiftregister.importer.tasks.import_shifts", "schedule": float(getenv("SHIFT_IMPORT_INTERVAL", 60.0)), # seconds }, + "send-messages-every-120-seconds": { + "task": "shiftregister.app.tasks.send_messages", + "schedule": float(getenv("SHIFT_IMPORT_INTERVAL", 120.0)), # seconds + }, + "send-reminders-every-300-seconds": { + "task": "shiftregister.app.tasks.send_reminders", + "schedule": float(getenv("SHIFT_IMPORT_INTERVAL", 300.0)), # seconds + }, } CELERY_BEAT_SCHEDULE_FILENAME = str(BASE_DIR / "storage" / "celerybeat-schedule")