From ee3e94d2e7fb28acb476a5abf164d3ca4db112b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Wed, 7 Feb 2018 10:48:04 +0100 Subject: [PATCH] [backport] Add advisory lock to send_notifications --- .../management/commands/send_notifications.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/taiga/projects/notifications/management/commands/send_notifications.py b/taiga/projects/notifications/management/commands/send_notifications.py index 988454eb..dc08c354 100644 --- a/taiga/projects/notifications/management/commands/send_notifications.py +++ b/taiga/projects/notifications/management/commands/send_notifications.py @@ -23,12 +23,18 @@ from taiga.base.utils.iterators import iter_queryset from taiga.projects.notifications.models import HistoryChangeNotification from taiga.projects.notifications.services import send_sync_notifications +from django_pglocks import advisory_lock + class Command(BaseCommand): def handle(self, *args, **options): - qs = HistoryChangeNotification.objects.all() - for change_notification in iter_queryset(qs, itersize=100): - try: - send_sync_notifications(change_notification.pk) - except HistoryChangeNotification.DoesNotExist: - pass + with advisory_lock("send-notifications-command", wait=False) as acquired: + if acquired: + qs = HistoryChangeNotification.objects.all() + for change_notification in iter_queryset(qs, itersize=100): + try: + send_sync_notifications(change_notification.pk) + except HistoryChangeNotification.DoesNotExist: + pass + else: + print("Other process already running")