From 8e8218e4737f251adaf6c2977d0e1e1775dfed93 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 8 Jan 2026 14:17:41 -0600 Subject: Stop a background job manually. --- uploader/background_jobs.py | 30 ++++++++++++ uploader/templates/background-jobs/stop-job.html | 61 ++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 uploader/templates/background-jobs/stop-job.html diff --git a/uploader/background_jobs.py b/uploader/background_jobs.py index 16d372c..ac6c033 100644 --- a/uploader/background_jobs.py +++ b/uploader/background_jobs.py @@ -194,3 +194,33 @@ def delete_single(job_id: uuid.UUID): "background-jobs.job_summary", job_id=job_id)) except JobNotFound as _jnf: return render_template("jobs/job-not-found.html", job_id=job_id) + + +@background_jobs_bp.route("/stop/", methods=["GET", "POST"]) +@require_login +def stop_job(job_id: uuid.UUID): + """Stop a running job.""" + with sqlite3.connection(app.config["ASYNCHRONOUS_JOBS_SQLITE_DB"]) as conn: + try: + job = jobs.job(conn, job_id, fulldetails=True) + status = job["metadata"]["status"] + if status != "running": + flash("Cannot stop a job that is not running.", "alert alert-danger") + return redirect(url_for( + "background-jobs.job_summary", job_id=job_id)) + + if request.method == "GET": + return render_template("background-jobs/stop-job.html", + job=job, + display_datetime=make_datetime_formatter()) + + if request.form["btn-confirm-stop"] == "stop": + jobs.kill_job(conn, job_id) + flash("Job was stopped successfully.", "alert alert-success") + return redirect(url_for( + "background-jobs.job_summary", job_id=job_id)) + flash("Stop cancelled.", "alert alert-info") + return redirect(url_for( + "background-jobs.job_summary", job_id=job_id)) + except JobNotFound as _jnf: + return render_template("jobs/job-not-found.html", job_id=job_id) diff --git a/uploader/templates/background-jobs/stop-job.html b/uploader/templates/background-jobs/stop-job.html new file mode 100644 index 0000000..fc190ac --- /dev/null +++ b/uploader/templates/background-jobs/stop-job.html @@ -0,0 +1,61 @@ +{%extends "background-jobs/base.html"%} +{%from "flash_messages.html" import flash_all_messages%} +{%from "background-jobs/macro-display-job-details.html" import display_job_details%} + +{%block title%}Background Jobs{%endblock%} + +{%block pagetitle%}Background Jobs{%endblock%} + +{%block breadcrumbs%} +{{super()}} + +{%endblock%} + +{%block contents%} +{{flash_all_messages()}} + +
+

background jobs: stop?

+ +

Are you sure you want to stop the job below?

+ + {{display_job_details(job, display_datetime)}} +
+ +
+
+
+
+ +
+
+ +
+
+
+
+{%endblock%} + + +{%block sidebarcontents%} +
+
What is this?
+
+
+

Confirm whether or not you want to stop job + {{job.job_id}}.

+
+{{super()}} +{%endblock%} -- cgit 1.4.1