Home > Software design >  Celery 'update_status' function is not working
Celery 'update_status' function is not working

Time:06-24

I am using celery and django-celery-results in my Django application. My celery results are being stored in the backend and this is what celery.py looks like.

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shoonya_backend.settings")

# Define celery app and settings
celery_app = Celery(
    "shoonya_backend",
    result_backend="django-db",
    accept_content=["application/json"],
    result_serializer="json",
    task_serializer="json",
)

celery_app.config_from_object("django.conf:settings", namespace="CELERY")
celery_app.autodiscover_tasks()

I am trying to update the state of a task from within the celery function if I get an exception as follows.

try: 
    result = resource.import_data(imported_data, raise_errors=True)



    # If validation checks fail, raise the Exception
except Exception as e:

    self.update_state(
        state=states.FAILURE,
        meta={
            "exc_type": type(e).__name__,
            "exc_message": traceback.format_exc().split("\n"),
        },
    )

The following command doesn't result in any change in the TaskResult table even when the exception is raised and the Task is always a success.

CodePudding user response:

In order to have a FAILURE state, you need to raise an exception, else you will always have a SUCCESS result state. Here you don't raise one, you just update your task status, which is not the same.

Even though you update the state of your task to FAILURE, because you then finish the execution of your task without raising any error, it instantly changes to SUCCESS.

But raising an exception will finish the execution on the FAILURE state you need.

You should have something like that :

try: 
    result = resource.import_data(imported_data, raise_errors=True)

except Exception as e:
    raise e # In your backend you'll get a FAILURE state and the traceback
  • Related