diff options
Diffstat (limited to 'uploader/background_jobs.py')
-rw-r--r-- | uploader/background_jobs.py | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/uploader/background_jobs.py b/uploader/background_jobs.py index 09ea0c0..dc9f837 100644 --- a/uploader/background_jobs.py +++ b/uploader/background_jobs.py @@ -1,11 +1,10 @@ +"""Generic views and utilities to handle background jobs.""" import uuid -import logging import importlib from typing import Callable from functools import partial from flask import ( - request, url_for, redirect, Response, @@ -29,8 +28,11 @@ def __default_error_handler__(job: dict) -> Response: def register_handlers( job_type: str, success_handler: HandlerType, + # pylint: disable=[redefined-outer-name] error_handler: HandlerType = __default_error_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"] = {} @@ -49,19 +51,19 @@ def register_job_handlers(job: str): _parts = absolute_function_path.split(".") app.logger.debug("THE PARTS ARE: %s", _parts) assert len(_parts) > 1, f"Invalid path: {absolute_function_path}" - function = _parts[-1] module = importlib.import_module(f".{_parts[-2]}", package=".".join(_parts[0:-2])) return getattr(module, _parts[-1]) metadata = job["metadata"] if metadata["success_handler"]: - success_handler = __load_handler__(metadata["success_handler"]) + _success_handler = __load_handler__(metadata["success_handler"]) try: - error_handler = __load_handler__(metadata["error_handler"]) - except Exception as _exc: - error_handler = __default_error_handler__ - register_handlers(metadata["job-type"], success_handler, error_handler) + _error_handler = __load_handler__(metadata["error_handler"]) + except Exception as _exc:# pylint: disable=[broad-exception-caught] + _error_handler = __default_error_handler__ + register_handlers( + metadata["job-type"], _success_handler, _error_handler) def handler(job: dict, handler_type: str) -> HandlerType: @@ -74,7 +76,7 @@ def handler(job: dict, handler_type: str) -> HandlerType: ).get(handler_type) if bool(_handler): return _handler(job) - raise Exception( + raise Exception(# pylint: disable=[broad-exception-raised] f"No '{handler_type}' handler registered for job type: {_job_type}") @@ -98,8 +100,8 @@ def job_status(job_id: uuid.UUID): if status == "completed": return success_handler(job) - return render_template(f"jobs/job-status.html", job=job) - except JobNotFound as jnf: + return render_template("jobs/job-status.html", job=job) + except JobNotFound as _jnf: return render_template( "jobs/job-not-found.html", job_id=job_id) @@ -108,9 +110,10 @@ def job_status(job_id: uuid.UUID): @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: + except JobNotFound as _jnf: return render_template("jobs/job-not-found.html", job_id=job_id) |