Skip to content

Commit

Permalink
Set correct task status
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-misuk-valor committed Jan 24, 2025
1 parent b4e5ed9 commit 048f420
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.4 on 2025-01-23 16:03

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("api", "0015_deduplicationset_encodings_finding_delete_duplicate"),
]

operations = [
migrations.AddField(
model_name="deduplicationset",
name="encoding_errors",
field=models.JSONField(blank=True, default=dict, null=True),
),
]
4 changes: 2 additions & 2 deletions src/hope_dedup_engine/apps/faces/celery/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
def image_pipeline(
deduplication_set: DeduplicationSet, config: dict[str, Any]
) -> Signature:
encode_images_pipeline = parallelize.s(
encode_images_pipeline = parallelize.si(
deduplication_set_image_files.s(deduplication_set.id),
encode_images.s(config),
100,
)
find_duplicates_pipeline = parallelize.s(
find_duplicates_pipeline = parallelize.si(
deduplication_set_embedding_pairs.s(deduplication_set.id),
filter_ignored_pairs.s(deduplication_set.id) | find_duplicates.s(config),
100,
Expand Down
8 changes: 4 additions & 4 deletions src/hope_dedup_engine/apps/faces/services/facial.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ def encode_faces(

for filename in filenames:
if filename not in images:
errors.append((filename, FacialError.NO_FILE_FOUND.name))
errors.append((filename, FacialError.NO_FILE_FOUND))
continue

try:
result = DeepFace.represent(storage.load_image(filename), **(options or {}))
if len(result) > 1:
errors.append((filename, FacialError.MULTIPLE_FACES_DETECTED.name))
errors.append((filename, FacialError.MULTIPLE_FACES_DETECTED))
else:
embeddings.append((filename, cast(list[float], result[0]["embedding"])))
except TypeError as e:
logger.exception(e)
errors.append((filename, FacialError.GENERIC_ERROR.name))
errors.append((filename, FacialError.GENERIC_ERROR))
except ValueError:
errors.append((filename, FacialError.NO_FACE_DETECTED.name))
errors.append((filename, FacialError.NO_FACE_DETECTED))

return embeddings, errors

Expand Down
16 changes: 16 additions & 0 deletions src/hope_dedup_engine/utils/celery/task_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import celery
from celery import exceptions as celery_exceptions
from celery import signals as celery_signals
from celery.states import FAILURE
from django_celery_results.models import TaskResult

# Because of few bugs in Celery it cannot handle exceptions in chains,
# groups, and chords if those structures are nested. An exception can make
Expand Down Expand Up @@ -125,3 +128,16 @@ def inner(*args: Any, **kwargs: Any) -> Result:
return make_error(e)

return inner


@celery_signals.task_postrun.connect
def unwrap_results(sender=None, headers=None, body=None, **kwargs) -> None:
if (task_id := kwargs.get("task_id")) and (result := kwargs.get("retval")):
if is_result(result):
result_model = TaskResult.objects.get(task_id=task_id)
if is_value(result):
result_model.result = result[DATA]
elif is_error(result):
result_model.result = result[MESSAGE]
result_model.status = FAILURE
result_model.save()

0 comments on commit 048f420

Please sign in to comment.