From dc4b4bbdf514e222551de2262d6bf2b982441641 Mon Sep 17 00:00:00 2001 From: Basraah Date: Thu, 3 Nov 2016 10:28:20 +1000 Subject: [PATCH] Wait until transaction completes to call group syncs (#571) Prevents syncing of old groups when celery workers execute faster than db transactions can be completed. --- services/signals.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/services/signals.py b/services/signals.py index 41ad0ddc..8766458e 100644 --- a/services/signals.py +++ b/services/signals.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals +from django.db import transaction from django.db.models.signals import m2m_changed from django.db.models.signals import post_save from django.db.models.signals import pre_save @@ -26,7 +27,8 @@ logger = logging.getLogger(__name__) @receiver(m2m_changed, sender=User.groups.through) def m2m_changed_user_groups(sender, instance, action, *args, **kwargs): logger.debug("Received m2m_changed from %s groups with action %s" % (instance, action)) - if action == "post_add" or action == "post_remove" or action == "post_clear": + + def trigger_service_group_update(): logger.debug("Triggering service group update for %s" % instance) auth, c = AuthServicesInfo.objects.get_or_create(user=instance) if auth.jabber_username: @@ -48,6 +50,10 @@ def m2m_changed_user_groups(sender, instance, action, *args, **kwargs): if auth.smf_username: update_smf_groups.delay(instance.pk) + if action == "post_add" or action == "post_remove" or action == "post_clear": + logger.debug("Waiting for commit to trigger service group update for %s" % instance) + transaction.on_commit(trigger_service_group_update) + def trigger_all_ts_update(): for auth in AuthServicesInfo.objects.filter(teamspeak3_uid__isnull=False):