diff options
Diffstat (limited to 'scripts/worker.py')
-rw-r--r-- | scripts/worker.py | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/scripts/worker.py b/scripts/worker.py index ecdfaa2..c6e989f 100644 --- a/scripts/worker.py +++ b/scripts/worker.py @@ -1,20 +1,23 @@ +"""External worker script""" import os import sys +import traceback from typing import Callable -from zipfile import Path, ZipFile, is_zipfile +from zipfile import ZipFile, is_zipfile import jsonpickle from redis import Redis -from redis.exceptions import ConnectionError +from redis.exceptions import ConnectionError # pylint: disable=[redefined-builtin] -from .qc import cli_argument_parser from quality_control.utils import make_progress_calculator -from quality_control.parsing import ( - take, FileType, strain_names, collect_errors) +from quality_control.parsing import FileType, strain_names, collect_errors +from .qc import cli_argument_parser def make_progress_indicator( - redis_connection: Redis, job_id: str, progress_calc_fn: Callable) -> Callable: + redis_connection: Redis, job_id: str, + progress_calc_fn: Callable) -> Callable: + """Make function that will compute the progress and update redis""" def __indicator__(linenumber, linetext): progress = progress_calc_fn(linenumber, linetext) redis_connection.hset(name=job_id, mapping=progress._asdict()) @@ -24,6 +27,7 @@ def make_progress_indicator( return __indicator__ def cli_args_valid(args): + "Check that the command-line arguments are provided and correct" if not os.path.exists(args.filepath): print(f"The file '{args.filepath}' does not exist.", file=sys.stderr) return None @@ -33,14 +37,15 @@ def cli_args_valid(args): return None try: - conn = Redis.from_url(args.redisurl) - except ConnectionError as ce: + conn = Redis.from_url(args.redisurl) # pylint: disable=[unused-variable] + except ConnectionError as conn_err: # pylint: disable=[unused-variable] print(traceback.format_exc(), file=sys.stderr) return None return args def process_cli_arguments(): + """Setup command-line parser""" parser = cli_argument_parser() parser.prog = "worker" parser.add_argument( @@ -50,12 +55,14 @@ def process_cli_arguments(): return cli_args_valid(parser.parse_args()) def stream_error(redis_conn, job_id, error): + """Update redis with the most current error(s) found""" errors = jsonpickle.decode( redis_conn.hget(job_id, key="errors") or jsonpickle.encode(tuple())) redis_conn.hset( job_id, key="errors", value=jsonpickle.encode(errors + (error,))) def make_user_aborted(redis_conn, job_id): + """Mkae function that checks whether the user aborted the process""" def __aborted__(): user_aborted = bool(int( redis_conn.hget(name=job_id, key="user_aborted") or "0")) @@ -66,10 +73,12 @@ def make_user_aborted(redis_conn, job_id): return __aborted__ def get_zipfile_size(filepath): + "Compute size of given zipfile" with ZipFile(filepath, "r") as zfile: return zfile.infolist()[0].file_size def main(): + "entry point to the script" args = process_cli_arguments() if args is None: print("Quiting due to errors!", file=sys.stderr) |