From 7b05c4d3a8b691ed1acb315d680b1f8f38823c18 Mon Sep 17 00:00:00 2001 From: ch20b063 Date: Thu, 25 Apr 2024 09:41:01 +0530 Subject: [PATCH 1/2] stopping_task_pr_1 --- backend/tasks/urls.py | 2 ++ backend/tasks/views.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/backend/tasks/urls.py b/backend/tasks/urls.py index d4f229e6f..f74f31963 100644 --- a/backend/tasks/urls.py +++ b/backend/tasks/urls.py @@ -6,6 +6,7 @@ AnnotationViewSet, PredictionViewSet, get_celery_tasks, + stopping_celery_tasks, ) router = routers.DefaultRouter() @@ -15,4 +16,5 @@ urlpatterns = [ path("get_celery_tasks/", get_celery_tasks), + path("stopping_celery_tasks/", stopping_celery_tasks), ] + router.urls diff --git a/backend/tasks/views.py b/backend/tasks/views.py index 43517ac06..20774687c 100644 --- a/backend/tasks/views.py +++ b/backend/tasks/views.py @@ -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 @@ -2598,3 +2603,24 @@ 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) + + +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) From 2b6d8197b9e13e42c21952d382d0a39d771bfe8a Mon Sep 17 00:00:00 2001 From: ch20b063 Date: Fri, 3 May 2024 08:28:51 +0530 Subject: [PATCH 2/2] deleting and resuming task --- backend/tasks/urls.py | 4 ++++ backend/tasks/views.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/backend/tasks/urls.py b/backend/tasks/urls.py index f74f31963..ceabbf4c7 100644 --- a/backend/tasks/urls.py +++ b/backend/tasks/urls.py @@ -7,6 +7,8 @@ PredictionViewSet, get_celery_tasks, stopping_celery_tasks, + resume_celery_task, + delete_celery_task, ) router = routers.DefaultRouter() @@ -17,4 +19,6 @@ 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 diff --git a/backend/tasks/views.py b/backend/tasks/views.py index 20774687c..e5430aadc 100644 --- a/backend/tasks/views.py +++ b/backend/tasks/views.py @@ -2605,6 +2605,7 @@ def get_celery_tasks(request): return JsonResponse(data["results"], safe=False) +@api_view(["GET"]) def stopping_celery_tasks(req): task_id = req.GET.get("task_id") @@ -2624,3 +2625,39 @@ def stopping_celery_tasks(req): 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)