From c6abe5911ebcdc5b65a1b02025ce92517a8b2ef8 Mon Sep 17 00:00:00 2001 From: "Andreas (@xAndy) Zimmermann" Date: Wed, 27 Apr 2022 19:10:23 +0200 Subject: [PATCH] add message send tasks and respect dynamic send setting --- .../app/migrations/0002_message_sent_at.py | 18 ++++++++++ shiftregister/app/models.py | 5 +++ shiftregister/app/tasks.py | 36 +++++++++++++++++-- 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 shiftregister/app/migrations/0002_message_sent_at.py diff --git a/shiftregister/app/migrations/0002_message_sent_at.py b/shiftregister/app/migrations/0002_message_sent_at.py new file mode 100644 index 0000000..8aaea36 --- /dev/null +++ b/shiftregister/app/migrations/0002_message_sent_at.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.4 on 2022-04-27 16:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("app", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="message", + name="sent_at", + field=models.DateTimeField(null=True), + ), + ] diff --git a/shiftregister/app/models.py b/shiftregister/app/models.py index f321714..73e4eef 100644 --- a/shiftregister/app/models.py +++ b/shiftregister/app/models.py @@ -103,6 +103,7 @@ class Message(models.Model): text = models.CharField(max_length=160) to = models.ForeignKey(Helper, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) + sent_at = models.DateTimeField(null=True) def __str__(self): return f"{self.to.name}({self.created_at}): {self.text}" @@ -127,6 +128,10 @@ class LoginToken(models.Model): msg.save() self.sent_at = timezone.now() self.save() + # import here to break import cycle + from .tasks import send_message + + send_message.apply_async((msg.pk,)) def get_absolute_url(self): return reverse("token_login", kwargs={"token": self.id}) diff --git a/shiftregister/app/tasks.py b/shiftregister/app/tasks.py index 2f3de06..98a244d 100644 --- a/shiftregister/app/tasks.py +++ b/shiftregister/app/tasks.py @@ -1,6 +1,38 @@ from celery import shared_task +from .models import Message +from django.db import transaction +from django.utils import timezone +from dynamic_preferences.registries import global_preferences_registry +global_preferences = global_preferences_registry.manager() +# cron task to send normal messages(reminders,changes) in batches @shared_task -def test(): - return "Hello, Celery!" +def send_messages(): + if not global_preferences["helper__send_sms"]: + print("sms disabled, not sending") + return + with transaction.atomic(): + for msg in Message.objects.select_for_update().filter(sent_at__isnull=True): + if msg.sent_at: + continue + print(f"TODO: send message @{msg.to.phone} {msg.text}") + msg.sent_at = timezone.now() + msg.save() + + +# singlemessage so registration links arrive faster. +# will silently fail when messages are disabled. +# those messages will be picked up by the cron send later +# once we have decided to continue sending messages +@shared_task +def send_message(msgid): + if not global_preferences["helper__send_sms"]: + return + with transaction.atomic(): + msg = Message.objects.select_for_update().get(pk=msgid) + if msg.sent_at: + return + print(f"TODO: send message @{msg.to.phone} {msg.text}") + msg.sent_at = timezone.now() + msg.save()