diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-11-27 17:40:33 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-11-27 17:40:33 +0100 |
commit | 0f4989c03f931297a488c75fc6f268e1113bae42 (patch) | |
tree | c6e07b9206418b6a05e5e69e2a4022657feb1db3 /ishtar_common/utils.py | |
parent | 362d46c4b8d4cde14ed90163c367b6a008b452e7 (diff) | |
download | Ishtar-0f4989c03f931297a488c75fc6f268e1113bae42.tar.bz2 Ishtar-0f4989c03f931297a488c75fc6f268e1113bae42.zip |
Background post save: wait for not commited object
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r-- | ishtar_common/utils.py | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 641fbd156..d22a89916 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -36,6 +36,7 @@ import six import subprocess import sys import tempfile +import time import xmltodict import zipfile @@ -418,9 +419,18 @@ def deserialize_args_for_tasks(sender, kwargs, extra_kwargs=None): if not isinstance(sender, (tuple, list)): # not task return sender, kwargs["instance"] sender = apps.get_model(*sender) - try: - instance = sender.objects.get(pk=kwargs['instance']) - except sender.DoesNotExist: + instance = None + retried = 0 + # object can be in cache of another thread but not yet commited + # waiting for it + while not instance and retried < 6: + if retried: + time.sleep(.5) + if sender.objects.filter(pk=kwargs['instance']).count(): + instance = sender.objects.get(pk=kwargs['instance']) + else: + retried += 1 + if not instance: return sender, None # object is not here anymore if extra_kwargs: for kw in extra_kwargs: @@ -456,7 +466,9 @@ def cached_label_changed(sender, **kwargs): # return # cache.set(cache_key, True, settings.CACHE_TASK_TIMEOUT) - if not settings.USE_BACKGROUND_TASK: + if not settings.USE_BACKGROUND_TASK or not instance.pk \ + or not sender.objects.filter(pk=instance.pk).count(): + # no background task or not yet fully saved return _cached_label_changed(sender, **kwargs) if getattr(instance, "_cascade_change", False): @@ -474,7 +486,6 @@ def _cached_label_changed(sender, **kwargs): if not instance: return force_update = kwargs.get('force_update', False) - if hasattr(instance, "need_update") and instance.need_update: force_update = True instance.skip_history_when_saving = True |