2
0
Fork 0

add celery task to send shift reminders

This commit is contained in:
Andreas (@xAndy) Zimmermann 2022-04-27 21:55:39 +02:00
parent 0da9d981c8
commit 3b0c0f7c9c
3 changed files with 20 additions and 8 deletions

View File

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

View File

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

View File

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