From a7e9e94cfafcdee11a68d440a71fb4936c9af82b Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 8 Sep 2022 09:37:22 +0300 Subject: Use sys.executable and setup default environment Use the sys.executable to get the correct python binary to run the external processes, and setup the environment to point to the correct PYTHONPATH. --- qc_app/jobs.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/qc_app/jobs.py b/qc_app/jobs.py index 9b350b7..dc9ba92 100644 --- a/qc_app/jobs.py +++ b/qc_app/jobs.py @@ -1,11 +1,13 @@ """Handle jobs""" import os +import sys import shlex import subprocess from uuid import uuid4 from datetime import timedelta from redis import Redis +from flask import current_app as app def error_filename(job_id, error_dir): "Compute the path of the file where errors will be dumped." @@ -29,7 +31,7 @@ def build_file_verification_job( "Build a file verification job" job_id = str(uuid4()) command = [ - "python3", "-m", "scripts.validate_file", filetype, filepath, redisurl, + sys.executable, "-m", "scripts.validate_file", filetype, filepath, redisurl, job_id ] return __init_job__( @@ -44,8 +46,8 @@ def data_insertion_job(# pylint: disable=[too-many-arguments] ttl_seconds: int) -> dict: "Build a data insertion job" command = [ - "python3", "-m", "scripts.insert_data", filetype, filepath, speciesid, - platformid, datasetid, databaseuri, redisuri + sys.executable, "-m", "scripts.insert_data", filetype, filepath, + speciesid, platformid, datasetid, databaseuri, redisuri ] return __init_job__( redis_conn, str(uuid4()), command, "data-insertion", ttl_seconds, { @@ -62,8 +64,9 @@ def launch_job(the_job: dict, redisurl: str, error_dir): "w", encoding="utf-8") as errorfile: subprocess.Popen( # pylint: disable=[consider-using-with] - ["python3", "-m", "scripts.worker", redisurl, job_id], - stderr=errorfile) + [sys.executable, "-m", "scripts.worker", redisurl, job_id], + stderr=errorfile, + env={"PYTHONPATH": ":".join(sys.path)}) return the_job -- cgit v1.2.3