From aa0c15bd7a8a0d9baba7c711b78f79e3d7005f5e Mon Sep 17 00:00:00 2001 From: rmanach Date: Fri, 22 Sep 2023 17:57:38 +0200 Subject: [PATCH] abort running task --- deployment/tasks.py | 5 +++-- deployment/urls.py | 3 ++- deployment/views.py | 25 ++++++++++++++++++++++- templates/deployment/board.html | 2 +- templates/deployment/details.html | 26 ++++++++++++++---------- templates/header.html | 33 +------------------------------ templates/home.html | 2 +- 7 files changed, 48 insertions(+), 48 deletions(-) diff --git a/deployment/tasks.py b/deployment/tasks.py index 47b193d..c230c26 100644 --- a/deployment/tasks.py +++ b/deployment/tasks.py @@ -2,13 +2,14 @@ import time import random from uuid import UUID -from celery import Task, shared_task +from celery import shared_task +from celery.contrib.abortable import AbortableTask from django_eventstream import send_event from deployment.models import Deployment, Type, Status -class DeploymentTask(Task): +class DeploymentTask(AbortableTask): def try_exec(self, stop: int): if stop == 0: stop = 1 diff --git a/deployment/urls.py b/deployment/urls.py index 882cf5f..574bfd2 100644 --- a/deployment/urls.py +++ b/deployment/urls.py @@ -1,13 +1,14 @@ import django_eventstream from django.urls import path, include -from deployment.views import index, create, details, deploy +from deployment.views import index, create, details, deploy, abort urlpatterns = [ path("", index, name="deployment"), path("create", create, name="deployment-create"), path("", details, name="deployment-details"), path("/deploy", deploy, name="deployment-launch"), + path("/abort", abort, name="deployment-abort"), path( "events/", include(django_eventstream.urls), diff --git a/deployment/views.py b/deployment/views.py index b0722bc..c21f0eb 100644 --- a/deployment/views.py +++ b/deployment/views.py @@ -1,6 +1,7 @@ from uuid import uuid4 from celery.result import AsyncResult +from celery.contrib.abortable import AbortableAsyncResult from django.core.paginator import Paginator from django.http import ( HttpResponseRedirect, @@ -38,6 +39,9 @@ def deploy(request, deployment_id): deployment = get_object_or_404(Deployment, id=deployment_id) if request.method == "POST": + if deployment.status not in (Status.READY.name, Status.FAILED.name): + return HttpResponseBadRequest("deployment is undeployable") + # override previous errors if deployment.error: deployment.error = None @@ -52,10 +56,29 @@ def deploy(request, deployment_id): return HttpResponseRedirect("/deployment") +def abort(request, deployment_id): + deployment = get_object_or_404(Deployment, id=deployment_id) + + if request.method == "POST": + if deployment.status not in (Status.RUNNING.name, Status.PENDING.name): + return HttpResponseBadRequest("deployment is unabortable") + + res = AbortableAsyncResult(str(deployment.task_id)) + res.abort() + res.revoke(terminate=True) + + deployment.status = Status.FAILED.name + deployment.error = f"aborted by {request.user}" + deployment.task_id = None + deployment.save() + + return HttpResponseRedirect(f"/deployment/{deployment.id}") + + def details(request, deployment_id): deployment = get_object_or_404(Deployment, id=deployment_id) if deployment.status == Status.RUNNING.name: - # retrieve the progression in the backend task + # retrieve the progression task in Redis res = AsyncResult(str(deployment.task_id)) deployment.progress = res.info.get("progress", 0) diff --git a/templates/deployment/board.html b/templates/deployment/board.html index 5b02029..61c1c3a 100644 --- a/templates/deployment/board.html +++ b/templates/deployment/board.html @@ -48,7 +48,7 @@ {% else %} diff --git a/templates/deployment/details.html b/templates/deployment/details.html index 8822197..5dba50c 100644 --- a/templates/deployment/details.html +++ b/templates/deployment/details.html @@ -5,7 +5,6 @@ {% block title %} Deployment details: {{ deployment.name }} {% endblock %} {% if deployment.status == "RUNNING" %} - {% block bodyattr %} {% if deployment.status == "RUNNING" %} onload="start('{{ url|safe }}');" @@ -19,7 +18,6 @@ {% endif %} {% endblock %} - {% endif %} {% block content %} @@ -66,13 +64,21 @@ {% endif %} -
- {% csrf_token %} - - {% if deployment.status != "RUNNING" %} - - {% endif %} -
+ {% if deployment.status != "RUNNING" and deployment.status != "PENDING" %} +
+ {% csrf_token %} +
+ {% else %} +
+ {% csrf_token %} +
+ {% endif %} + + {% if deployment.status != "RUNNING" and deployment.status != "PENDING" %} + + {% else %} + + {% endif %} {% else %}

Please log in !

{% endif %} @@ -88,7 +94,7 @@ {% endif %} {% endblock %} \ No newline at end of file diff --git a/templates/header.html b/templates/header.html index aa616c3..4df8829 100644 --- a/templates/header.html +++ b/templates/header.html @@ -26,35 +26,4 @@ - - - - \ No newline at end of file + \ No newline at end of file diff --git a/templates/home.html b/templates/home.html index 754cfe7..7b22bc9 100644 --- a/templates/home.html +++ b/templates/home.html @@ -13,7 +13,7 @@
Forget, i don't care... Please go on - Deployments + Deployments and enjoy the life !