abort running task
This commit is contained in:
		
							parent
							
								
									1d9c4e0164
								
							
						
					
					
						commit
						aa0c15bd7a
					
				| @ -2,13 +2,14 @@ import time | |||||||
| import random | import random | ||||||
| from uuid import UUID | 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 django_eventstream import send_event | ||||||
| 
 | 
 | ||||||
| from deployment.models import Deployment, Type, Status | from deployment.models import Deployment, Type, Status | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class DeploymentTask(Task): | class DeploymentTask(AbortableTask): | ||||||
|     def try_exec(self, stop: int): |     def try_exec(self, stop: int): | ||||||
|         if stop == 0: |         if stop == 0: | ||||||
|             stop = 1 |             stop = 1 | ||||||
|  | |||||||
| @ -1,13 +1,14 @@ | |||||||
| import django_eventstream | import django_eventstream | ||||||
| 
 | 
 | ||||||
| from django.urls import path, include | from django.urls import path, include | ||||||
| from deployment.views import index, create, details, deploy | from deployment.views import index, create, details, deploy, abort | ||||||
| 
 | 
 | ||||||
| urlpatterns = [ | urlpatterns = [ | ||||||
|     path("", index, name="deployment"), |     path("", index, name="deployment"), | ||||||
|     path("create", create, name="deployment-create"), |     path("create", create, name="deployment-create"), | ||||||
|     path("<uuid:deployment_id>", details, name="deployment-details"), |     path("<uuid:deployment_id>", details, name="deployment-details"), | ||||||
|     path("<uuid:deployment_id>/deploy", deploy, name="deployment-launch"), |     path("<uuid:deployment_id>/deploy", deploy, name="deployment-launch"), | ||||||
|  |     path("<uuid:deployment_id>/abort", abort, name="deployment-abort"), | ||||||
|     path( |     path( | ||||||
|         "events/", |         "events/", | ||||||
|         include(django_eventstream.urls), |         include(django_eventstream.urls), | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| from uuid import uuid4 | from uuid import uuid4 | ||||||
| 
 | 
 | ||||||
| from celery.result import AsyncResult | from celery.result import AsyncResult | ||||||
|  | from celery.contrib.abortable import AbortableAsyncResult | ||||||
| from django.core.paginator import Paginator | from django.core.paginator import Paginator | ||||||
| from django.http import ( | from django.http import ( | ||||||
|     HttpResponseRedirect, |     HttpResponseRedirect, | ||||||
| @ -38,6 +39,9 @@ def deploy(request, deployment_id): | |||||||
|     deployment = get_object_or_404(Deployment, id=deployment_id) |     deployment = get_object_or_404(Deployment, id=deployment_id) | ||||||
| 
 | 
 | ||||||
|     if request.method == "POST": |     if request.method == "POST": | ||||||
|  |         if deployment.status not in (Status.READY.name, Status.FAILED.name): | ||||||
|  |             return HttpResponseBadRequest("deployment is undeployable") | ||||||
|  | 
 | ||||||
|         # override previous errors |         # override previous errors | ||||||
|         if deployment.error: |         if deployment.error: | ||||||
|             deployment.error = None |             deployment.error = None | ||||||
| @ -52,10 +56,29 @@ def deploy(request, deployment_id): | |||||||
|     return HttpResponseRedirect("/deployment") |     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): | def details(request, deployment_id): | ||||||
|     deployment = get_object_or_404(Deployment, id=deployment_id) |     deployment = get_object_or_404(Deployment, id=deployment_id) | ||||||
|     if deployment.status == Status.RUNNING.name: |     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)) |         res = AsyncResult(str(deployment.task_id)) | ||||||
|         deployment.progress = res.info.get("progress", 0) |         deployment.progress = res.info.get("progress", 0) | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -48,7 +48,7 @@ | |||||||
|                                     <th name="deploy"> |                                     <th name="deploy"> | ||||||
|                                         <button class="btn btn-primary" type="button" disabled> |                                         <button class="btn btn-primary" type="button" disabled> | ||||||
|                                             <span class="spinner-border spinner-border-sm" aria-hidden="true"></span> |                                             <span class="spinner-border spinner-border-sm" aria-hidden="true"></span> | ||||||
|                                             <span role="status">Deploy</span> |                                             <span role="status">Deploying...</span> | ||||||
|                                         </button> |                                         </button> | ||||||
|                                     </th> |                                     </th> | ||||||
|                                 {% else %} |                                 {% else %} | ||||||
|  | |||||||
| @ -5,7 +5,6 @@ | |||||||
| {% block title %} Deployment details: {{ deployment.name }} {% endblock %} | {% block title %} Deployment details: {{ deployment.name }} {% endblock %} | ||||||
| 
 | 
 | ||||||
| {% if deployment.status == "RUNNING" %} | {% if deployment.status == "RUNNING" %} | ||||||
| 
 |  | ||||||
|     {% block bodyattr %} |     {% block bodyattr %} | ||||||
|         {% if deployment.status == "RUNNING" %} |         {% if deployment.status == "RUNNING" %} | ||||||
|             onload="start('{{ url|safe }}');" |             onload="start('{{ url|safe }}');" | ||||||
| @ -19,7 +18,6 @@ | |||||||
|             <script src="{% static 'django_eventstream/reconnecting-eventsource.js' %}"></script> |             <script src="{% static 'django_eventstream/reconnecting-eventsource.js' %}"></script> | ||||||
|         {% endif %} |         {% endif %} | ||||||
|     {% endblock %} |     {% endblock %} | ||||||
| 
 |  | ||||||
| {% endif %} | {% endif %} | ||||||
| 
 | 
 | ||||||
| {% block content %} | {% block content %} | ||||||
| @ -66,13 +64,21 @@ | |||||||
|                             <label for="updated-at">Error</label> |                             <label for="updated-at">Error</label> | ||||||
|                         </div> |                         </div> | ||||||
|                     {% endif %} |                     {% endif %} | ||||||
|                     <form action="" method="post"> |                     {% if deployment.status != "RUNNING" and deployment.status != "PENDING" %} | ||||||
|                         {% csrf_token %} |                         <form id="delete-deployment" action="" method="post"> | ||||||
|                         <button type="button" onclick="goBack()" class="btn btn-secondary">Back</button> |                             {% csrf_token %} | ||||||
|                         {% if deployment.status != "RUNNING" %} |                         </form> | ||||||
|                             <button type="submit" class="btn btn-danger">Delete</button> |                     {% else %} | ||||||
|                         {% endif %} |                         <form id="abort-deployment" action="{% url 'deployment-abort' deployment.id %}" method="post"> | ||||||
|                     </form> |                             {% 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 %} |                 {% else %} | ||||||
|                     <h4 style="text-align: center;">Please log in !</h4> |                     <h4 style="text-align: center;">Please log in !</h4> | ||||||
|                 {% endif %} |                 {% endif %} | ||||||
| @ -88,7 +94,7 @@ | |||||||
|     {% endif %} |     {% endif %} | ||||||
|     <script> |     <script> | ||||||
|         function goBack() { |         function goBack() { | ||||||
|             window.location=document.referrer; |             window.location={% url 'deployment' %}; | ||||||
|         } |         } | ||||||
|     </script> |     </script> | ||||||
| {% endblock %} | {% endblock %} | ||||||
| @ -27,34 +27,3 @@ | |||||||
|         </div> |         </div> | ||||||
|     </nav> |     </nav> | ||||||
| </header> | </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="row justify-content-md-center"> | ||||||
|                 <div class="col-lg-6 col-md-8 col-sm-8"> |                 <div class="col-lg-6 col-md-8 col-sm-8"> | ||||||
|                     <div style="text-align: center;">Forget, i don't care... Please go on  |                     <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 ! |                         and enjoy the life ! | ||||||
|                     </div> |                     </div> | ||||||
|                 </div> |                 </div> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 rmanach
						rmanach