import os import shlex import subprocess from uuid import uuid4 from redis import Redis def error_filename(job_id, error_dir): return f"{error_dir}/job_{job_id}.error" def launch_job(redis_conn: Redis, filepath, filetype, redisurl, error_dir): """Launch a job in the background""" job_id = str(uuid4()) command = [ "python3", "-m" "scripts.worker", filetype, filepath, redisurl, job_id] job = { "job_id": job_id, "command": shlex.join(command), "status": "pending", "filename": os.path.basename(filepath), "percent": 0 } redis_conn.hset(name=job["job_id"], mapping=job) if not os.path.exists(error_dir): os.mkdir(error_dir) with open(error_filename(job_id, error_dir), "w") as errorfile: subprocess.Popen(command, stderr=errorfile) return job def job(redis_conn, job_id: str): "Retrieve the job" return redis_conn.hgetall(job_id)