Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stopping_task_pr_1 #1065

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/tasks/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
AnnotationViewSet,
PredictionViewSet,
get_celery_tasks,
stopping_celery_tasks,
resume_celery_task,
delete_celery_task,
)

router = routers.DefaultRouter()
Expand All @@ -15,4 +18,7 @@

urlpatterns = [
path("get_celery_tasks/", get_celery_tasks),
path("stopping_celery_tasks/", stopping_celery_tasks),
path("resume_celery_task/", resume_celery_task),
path("delete_celery_task/", delete_celery_task),
] + router.urls
63 changes: 63 additions & 0 deletions backend/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
from django.utils import timezone
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import json
from celery import Celery

# from flower.api import Flower
# flower_app = Flower()
celery_app = Celery()

from django.core.exceptions import ObjectDoesNotExist
from django.http import StreamingHttpResponse, FileResponse
Expand Down Expand Up @@ -2589,3 +2594,61 @@ def get_celery_tasks(request):
page_size = int(request.GET.get("page_size", 10))
data = paginate_queryset(filtered_tasks, page_number, page_size)
return JsonResponse(data["results"], safe=False)


@api_view(["GET"])
def stopping_celery_tasks(req):
task_id = req.GET.get("task_id")

if task_id is None:
return JsonResponse({"message": "Task ID is required"}, status=400)

task = celery_app.AsyncResult(task_id)

if task is None or task.state == "PENDING":
return JsonResponse({"message": "Task not found or not running"}, status=404)

if task.state in ["SUCCESS", "FAILURE", "REVOKED"]:
return JsonResponse(
{"message": "Task already completed or revoked"}, status=400
)

task.revoke(terminate=True)

return JsonResponse({"message": "Task stopped successfully"}, status=200)


@api_view(["GET"])
def resume_celery_task(req):
task_id = req.GET.get("task_id")

if task_id is None:
return JsonResponse({"message": "Task ID is required"}, status=400)

task = celery_app.AsyncResult(task_id)

if task is None or task.state not in ["REVOKED", "FAILURE"]:
return JsonResponse(
{"message": "Task not found or cannot be resumed"}, status=400
)

task.revive()

return JsonResponse({"message": "Task resumed successfully"}, status=200)


@api_view(["GET"])
def delete_celery_task(req):
task_id = req.GET.get("task_id")

if task_id is None:
return JsonResponse({"message": "Task ID is required"}, status=400)

task = celery_app.AsyncResult(task_id)

if task is None:
return JsonResponse({"message": "Task not found"}, status=404)

task.forget()

return JsonResponse({"message": "Task deleted successfully"}, status=200)
Loading