1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
"""Generic views and utilities to handle background jobs."""
import uuid
import datetime
import importlib
from typing import Callable
from functools import partial
from werkzeug.wrappers.response import Response
from flask import (
flash,
request,
redirect,
Blueprint,
current_app as app)
from gn_libs import jobs
from gn_libs import sqlite3
from gn_libs.jobs.jobs import JobNotFound
from uploader import session
from uploader.authorisation import require_login
from uploader.flask_extensions import url_for, render_template
background_jobs_bp = Blueprint("background-jobs", __name__)
HandlerType = Callable[[dict], Response]
def make_datetime_formatter(dtformat: str = "%A, %d %B %Y at %H:%M %Z") -> Callable[[str], str]:
"""Make a datetime formatter with the provided `dtformat`"""
def __formatter__(val: str) -> str:
dt = datetime.datetime.fromisoformat(val)
return dt.strftime(dtformat.strip())
return __formatter__
__default_datetime_formatter__ = make_datetime_formatter()
def __default_handler__(_job):
return render_template("background-jobs/job-summary.html",
job=_job,
display_datetime=__default_datetime_formatter__)
def register_handlers(
job_type: str,
success_handler: HandlerType,
# pylint: disable=[redefined-outer-name]
error_handler: HandlerType = __default_handler__
# pylint: disable=[redefined-outer-name]
) -> str:
"""Register success and error handlers for each job type."""
if not bool(app.config.get("background-jobs")):
app.config["background-jobs"] = {}
if not bool(app.config["background-jobs"].get(job_type)):
app.config["background-jobs"][job_type] = {
"success": success_handler,
"error": error_handler
}
return job_type
def register_job_handlers(job: dict):
"""Related to register handlers above."""
def __load_handler__(absolute_function_path):
_parts = absolute_function_path.split(".")
app.logger.debug("THE PARTS ARE: %s", _parts)
assert len(_parts) > 1, f"Invalid path: {absolute_function_path}"
module = importlib.import_module(f".{_parts[-2]}",
package=".".join(_parts[0:-2]))
return getattr(module, _parts[-1])
metadata = job["metadata"]
if metadata.get("success_handler"):
_success_handler = __load_handler__(metadata["success_handler"])
try:
_error_handler = __load_handler__(metadata["error_handler"])
except Exception as _exc:# pylint: disable=[broad-exception-caught]
_error_handler = __default_handler__
register_handlers(
metadata["job-type"], _success_handler, _error_handler)
def handler(job: dict, handler_type: str) -> HandlerType:
"""Fetch a handler for the job."""
_job_type = job["metadata"]["job-type"]
_handler = app.config.get(
"background-jobs", {}
).get(
_job_type, {}
).get(handler_type)
if bool(_handler):
return _handler(job)
return __default_handler__(job)
error_handler = partial(handler, handler_type="error")
success_handler = partial(handler, handler_type="success")
@background_jobs_bp.route("/status/<uuid:job_id>")
@require_login
def job_status(job_id: uuid.UUID):
"""View the job status."""
with sqlite3.connection(app.config["ASYNCHRONOUS_JOBS_SQLITE_DB"]) as conn:
try:
job = jobs.job(conn, job_id, fulldetails=True)
status = job["metadata"]["status"]
register_job_handlers(job)
if status in ("error", "stopped"):
return error_handler(job)
if status == "completed":
return success_handler(job)
return render_template("background-jobs/job-status.html",
job=job,
display_datetime=__default_datetime_formatter__)
except JobNotFound as _jnf:
return render_template("jobs/job-not-found.html", job_id=job_id)
@background_jobs_bp.route("/error/<uuid:job_id>")
@require_login
def job_error(job_id: uuid.UUID):
"""Handle job errors in a generic manner."""
with sqlite3.connection(app.config["ASYNCHRONOUS_JOBS_SQLITE_DB"]) as conn:
try:
job = jobs.job(conn, job_id, fulldetails=True)
return render_template("jobs/job-error.html", job=job)
except JobNotFound as _jnf:
return render_template("jobs/job-not-found.html", job_id=job_id)
@background_jobs_bp.route("/list")
@require_login
def list_jobs():
"""List background jobs."""
with sqlite3.connection(app.config["ASYNCHRONOUS_JOBS_SQLITE_DB"]) as conn:
return render_template(
"background-jobs/list-jobs.html",
jobs=jobs.jobs_by_external_id(
conn, session.user_details()["user_id"]),
display_datetime=__default_datetime_formatter__)
@background_jobs_bp.route("/summary/<uuid:job_id>")
@require_login
def job_summary(job_id: uuid.UUID):
"""Provide a summary for completed jobs."""
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 in ("completed", "error", "stopped"):
return render_template("background-jobs/job-summary.html",
job=job,
display_datetime=__default_datetime_formatter__)
return redirect(url_for(
"background-jobs.job_status", job_id=job["job_id"]))
except JobNotFound as _jnf:
return render_template("jobs/job-not-found.html", job_id=job_id)
@background_jobs_bp.route("/delete/<uuid:job_id>", methods=["GET", "POST"])
@require_login
def delete_single(job_id: uuid.UUID):
"""Delete a single 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 not in ("completed", "error", "stopped"):
flash("We cannot delete a running job.", "alert alert-danger")
return redirect(url_for(
"background-jobs.job_summary", job_id=job_id))
if request.method == "GET":
return render_template("background-jobs/delete-job.html",
job=job,
display_datetime=__default_datetime_formatter__)
if request.form["btn-confirm-delete"] == "delete":
jobs.delete_job(conn, job_id)
flash("Job was deleted successfully.", "alert alert-success")
return redirect(url_for("background-jobs.list_jobs"))
flash("Delete 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)
@background_jobs_bp.route("/stop/<uuid:job_id>", 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=__default_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)
|