Skip to content

Commit 8880bb6

Browse files
authored
Merge pull request #1253 from tcely/patch-2
Rework `SIGNAL_ENQUEUED` exception handling
2 parents 4fd7cbb + 2910fe8 commit 8880bb6

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

tubesync/common/huey.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import os
3+
import uuid
34
from functools import wraps
45
from huey import (
56
CancelExecution, Huey as huey_Huey,
@@ -403,19 +404,35 @@ def historical_task(signal_name, task_obj, exception_obj=None, /, *, huey=None):
403404
scheduled_at = huey.scheduled_at_from_task(task_obj)
404405
if scheduled_at:
405406
th.scheduled_at = scheduled_at
406-
from sync.models import Media, Source
407-
if not th.verbose_name and task_obj.args:
408-
key = task_obj.args[0]
409-
for model in (Media, Source,):
407+
if not th.verbose_name:
408+
try:
409+
from django.core.exceptions import ValidationError
410+
from sync.models import Media, Source
411+
except:
412+
pass
413+
else:
410414
try:
411-
model_instance = model.objects.get(pk=key)
412-
except (model.DoesNotExist, ValueError,):
413-
pass
415+
key = task_obj.args[0]
416+
instance_uuid_str = str(key)
417+
instance_uuid = uuid.UUID(instance_uuid_str)
418+
except IndexError:
419+
key = None
420+
except RuntimeError:
421+
instance_uuid_str = None
422+
except ValueError:
423+
instance_uuid = None
414424
else:
415-
if hasattr(model_instance, 'key'):
416-
th.verbose_name = f'{th.name} with: {model_instance.key}'
417-
if hasattr(model_instance, 'name'):
418-
th.verbose_name += f' / {model_instance.name}'
425+
for model in (Source, Media,):
426+
try:
427+
model_instance = model.objects.get(pk=instance_uuid)
428+
except (model.DoesNotExist, ValidationError,):
429+
pass
430+
else:
431+
if hasattr(model_instance, 'key'):
432+
th.verbose_name = f'{th.name} with: {model_instance.key}'
433+
if hasattr(model_instance, 'name'):
434+
th.verbose_name += f' / {model_instance.name}'
435+
break
419436
elif signal_name == signals.SIGNAL_SCHEDULED:
420437
scheduled_at = huey.scheduled_at_from_task(task_obj)
421438
if scheduled_at:

0 commit comments

Comments
 (0)