abort running task
This commit is contained in:
		
							parent
							
								
									1d9c4e0164
								
							
						
					
					
						commit
						aa0c15bd7a
					
				| @ -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 | ||||
|  | ||||
| @ -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("<uuid:deployment_id>", details, name="deployment-details"), | ||||
|     path("<uuid:deployment_id>/deploy", deploy, name="deployment-launch"), | ||||
|     path("<uuid:deployment_id>/abort", abort, name="deployment-abort"), | ||||
|     path( | ||||
|         "events/", | ||||
|         include(django_eventstream.urls), | ||||
|  | ||||
| @ -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) | ||||
| 
 | ||||
|  | ||||
| @ -48,7 +48,7 @@ | ||||
|                                     <th name="deploy"> | ||||
|                                         <button class="btn btn-primary" type="button" disabled> | ||||
|                                             <span class="spinner-border spinner-border-sm" aria-hidden="true"></span> | ||||
|                                             <span role="status">Deploy</span> | ||||
|                                             <span role="status">Deploying...</span> | ||||
|                                         </button> | ||||
|                                     </th> | ||||
|                                 {% else %} | ||||
|  | ||||
| @ -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 @@ | ||||
|             <script src="{% static 'django_eventstream/reconnecting-eventsource.js' %}"></script> | ||||
|         {% endif %} | ||||
|     {% endblock %} | ||||
| 
 | ||||
| {% endif %} | ||||
| 
 | ||||
| {% block content %} | ||||
| @ -66,13 +64,21 @@ | ||||
|                             <label for="updated-at">Error</label> | ||||
|                         </div> | ||||
|                     {% endif %} | ||||
|                     <form action="" method="post"> | ||||
|                     {% if deployment.status != "RUNNING" and deployment.status != "PENDING" %} | ||||
|                         <form id="delete-deployment" action="" method="post"> | ||||
|                             {% csrf_token %} | ||||
|                         <button type="button" onclick="goBack()" class="btn btn-secondary">Back</button> | ||||
|                         {% if deployment.status != "RUNNING" %} | ||||
|                             <button type="submit" class="btn btn-danger">Delete</button> | ||||
|                         {% endif %} | ||||
|                         </form> | ||||
|                     {% else %} | ||||
|                         <form id="abort-deployment" action="{% url 'deployment-abort' deployment.id %}" method="post"> | ||||
|                             {% csrf_token %} | ||||
|                         </form> | ||||
|                     {% endif %} | ||||
|                     <button type="button" onclick="goBack()" class="btn btn-secondary">Back</button> | ||||
|                     {% if deployment.status != "RUNNING" and deployment.status != "PENDING" %} | ||||
|                         <button form="delete-deployment" type="submit" class="btn btn-danger">Delete</button> | ||||
|                     {% else %} | ||||
|                         <button form="abort-deployment" type="submit" class="btn btn-danger">Abort</button> | ||||
|                     {% endif %} | ||||
|                 {% else %} | ||||
|                     <h4 style="text-align: center;">Please log in !</h4> | ||||
|                 {% endif %} | ||||
| @ -88,7 +94,7 @@ | ||||
|     {% endif %} | ||||
|     <script> | ||||
|         function goBack() { | ||||
|             window.location=document.referrer; | ||||
|             window.location={% url 'deployment' %}; | ||||
|         } | ||||
|     </script> | ||||
| {% endblock %} | ||||
| @ -27,34 +27,3 @@ | ||||
|         </div> | ||||
|     </nav> | ||||
| </header> | ||||
| 
 | ||||
| 
 | ||||
| <!-- <nav class="navbar navbar-expand-lg bg-dark border-bottom border-body" data-bs-theme="dark"> | ||||
|     <div class="container-fluid"> | ||||
|         <a class="navbar-brand" href="#">Navbar</a> | ||||
|         <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarColor01" | ||||
|             aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation"> | ||||
|             <span class="navbar-toggler-icon"></span> | ||||
|         </button> | ||||
|         <div class="collapse navbar-collapse" id="navbarColor01"> | ||||
|             <ul class="navbar-nav me-auto mb-2 mb-lg-0"> | ||||
|                 <li class="nav-item"> | ||||
|                     <a class="nav-link active" aria-current="page" href="#">Home</a> | ||||
|                 </li> | ||||
|                 <li class="nav-item"> | ||||
|                     <a class="nav-link" href="#">Features</a> | ||||
|                 </li> | ||||
|                 <li class="nav-item"> | ||||
|                     <a class="nav-link" href="#">Pricing</a> | ||||
|                 </li> | ||||
|                 <li class="nav-item"> | ||||
|                     <a class="nav-link" href="#">About</a> | ||||
|                 </li> | ||||
|             </ul> | ||||
|             <form class="d-flex" role="search"> | ||||
|                 <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search"> | ||||
|                 <button class="btn btn-outline-light" type="submit">Search</button> | ||||
|             </form> | ||||
|         </div> | ||||
|     </div> | ||||
| </nav> --> | ||||
| @ -13,7 +13,7 @@ | ||||
|             <div class="row justify-content-md-center"> | ||||
|                 <div class="col-lg-6 col-md-8 col-sm-8"> | ||||
|                     <div style="text-align: center;">Forget, i don't care... Please go on  | ||||
|                         <a href="/deployment/">Deployments</a>  | ||||
|                         <a href="{% url 'deployment' %}">Deployments</a>  | ||||
|                         and enjoy the life ! | ||||
|                     </div> | ||||
|                 </div> | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 rmanach
						rmanach