diff options
| author | Frederick Muriuki Muriithi | 2026-01-07 15:05:34 -0600 |
|---|---|---|
| committer | Frederick Muriuki Muriithi | 2026-01-07 15:05:34 -0600 |
| commit | 5eaafdabf0e22b04907d6639600d2270211c4da5 (patch) | |
| tree | 65e891a009e2a6cb797269a7770f2e4faaf1492b | |
| parent | 63ad52c70bf32ca46ccd4ca6010e2aaa434c0344 (diff) | |
| download | gn-libs-5eaafdabf0e22b04907d6639600d2270211c4da5.tar.gz | |
Fetch jobs by their external IDs.
| -rw-r--r-- | gn_libs/jobs/__init__.py | 3 | ||||
| -rw-r--r-- | gn_libs/jobs/jobs.py | 21 |
2 files changed, 23 insertions, 1 deletions
diff --git a/gn_libs/jobs/__init__.py b/gn_libs/jobs/__init__.py index d6e4ce3..4960349 100644 --- a/gn_libs/jobs/__init__.py +++ b/gn_libs/jobs/__init__.py @@ -4,7 +4,8 @@ from .jobs import (job, launch_job, initialise_job, push_to_stream, - update_metadata) + update_metadata, + jobs_by_external_id) def init_app(flask_app): """Initialise the migrations for flask""" diff --git a/gn_libs/jobs/jobs.py b/gn_libs/jobs/jobs.py index 8453697..f5b7a5e 100644 --- a/gn_libs/jobs/jobs.py +++ b/gn_libs/jobs/jobs.py @@ -60,6 +60,27 @@ def job(conn: DbConnection, job_id: Union[str, uuid.UUID], fulldetails: bool = F return _job +def jobs_by_external_id(conn: DbConnection, external_id: Union[str, uuid.UUID]) -> tuple[dict, ...]: + """Fetch jobs by their external IDs.""" + with _cursor(conn) as cursor: + cursor.execute( + "SELECT jeids.external_id, jobs.* FROM jobs_external_ids AS jeids " + "INNER JOIN jobs ON jeids.job_id=jobs.job_id " + "WHERE jeids.external_id=?", + (str(external_id),)) + _jobs = {row["job_id"]: {**dict(row), "metadata": {}} for row in cursor.fetchall()} + _jobs_ids = tuple(_job["job_id"] for _job in _jobs.values()) + + _paramstr = ", ".join(["?"] * len(_jobs_ids)) + cursor.execute( + f"SELECT * FROM jobs_metadata WHERE job_id IN ({_paramstr})", + _jobs_ids) + for row in cursor.fetchall(): + _jobs[row["job_id"]]["metadata"][row["metadata_key"]] = row["metadata_value"] + + return tuple(_jobs.values()) + + def __save_job__( conn: DbConnection, the_job: dict, |
