Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
Revert "Refactor poll_state"
Browse files Browse the repository at this point in the history
This reverts commit e7287fd.
  • Loading branch information
aaron-lane committed Dec 30, 2023
1 parent 0f5a586 commit 2cebfb8
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions app/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from celery.exceptions import TimeoutError
from celery.result import AsyncResult
from celery.states import FAILURE, PENDING, REVOKED, SUCCESS
from celery.states import FAILURE, PENDING, SUCCESS
from django.contrib.auth.decorators import login_required
from django.core import serializers
from django.http import (
Expand All @@ -22,7 +22,7 @@

from app.constants.str import PERMISSION_DENIED
from app.models import Item
from app.worker.app_celery import ATTEMPT_LIMIT
from app.worker.app_celery import ATTEMPT_LIMIT, PROGRESS
from app.worker.tasks import receiptor
from app.worker.tasks.exporter import exporter
from app.worker.tasks.importers import historical_data_importer
Expand Down Expand Up @@ -120,21 +120,15 @@ def poll_state(request: HttpRequest):
request=request,
err_msg="The task_id query parameter of the request was omitted.")

# A request for an invalid task ID will receive a task that is not ready but otherwise valid :(.
# SEE: https://github.com/celery/celery/issues/3596
task = AsyncResult(task_id)

# Ready states are success, failure, and revoked.
# SEE: https://docs.celeryq.dev/en/4.4.3/_modules/celery/states.html
if not task.ready():
res = JsonResponse(_poll_state(PENDING, 0, 200))
elif task.successful():
res = HttpResponse(SUCCESS)
elif task.failed():
res = JsonResponse(_poll_state(PENDING, 0, 200))
if task.state == FAILURE or task.failed():
res = JsonResponse(_poll_state(FAILURE, 0, 400))
else:
res = JsonResponse(_poll_state(REVOKED, 0, 500))

elif task.state == PROGRESS:
res = JsonResponse(task.result) if isinstance(
task.result, dict) else HttpResponse(task.result)
elif task.state == SUCCESS or task.successful() or task.ready():
res = HttpResponse(SUCCESS)
return res


Expand Down

0 comments on commit 2cebfb8

Please sign in to comment.