abort running task
This commit is contained in:
+3
-2
@@ -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
|
||||
|
||||
+2
-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),
|
||||
|
||||
+24
-1
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user