From 6c71080e56c22350e76cad092761ebffc936c62b Mon Sep 17 00:00:00 2001 From: bloodearnest Date: Wed, 6 Dec 2023 13:43:41 +0000 Subject: [PATCH] Expose metrics in sync protocol --- jobrunner/executors/local.py | 4 ++-- jobrunner/record_stats.py | 7 +++++++ jobrunner/sync.py | 5 +++-- tests/factories.py | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/jobrunner/executors/local.py b/jobrunner/executors/local.py index 21507a23..b5ccfdb5 100644 --- a/jobrunner/executors/local.py +++ b/jobrunner/executors/local.py @@ -11,7 +11,7 @@ from pipeline.legacy import get_all_output_patterns_from_project_file -from jobrunner import config, models +from jobrunner import config, record_stats from jobrunner.executors import volumes from jobrunner.job_executor import ( ExecutorAPI, @@ -22,7 +22,7 @@ JobStatus, Privacy, ) -from jobrunner.lib import database, datestr_to_ns_timestamp, docker +from jobrunner.lib import datestr_to_ns_timestamp, docker from jobrunner.lib.git import checkout_commit from jobrunner.lib.path_utils import list_dir_with_ignore_patterns from jobrunner.lib.string_utils import tabulate diff --git a/jobrunner/record_stats.py b/jobrunner/record_stats.py index 01cdef75..c6129df7 100644 --- a/jobrunner/record_stats.py +++ b/jobrunner/record_stats.py @@ -83,6 +83,13 @@ def main(): time.sleep(max(2, config.STATS_POLL_INTERVAL - elapsed)) +def get_metrics_for_job(job_id): + try: + return database.find_one(models.ExecutorJob, id=job_id).metrics + except ValueError: + return {} + + def record_tick_trace(last_run, active_jobs): """Record a period tick trace of current jobs. diff --git a/jobrunner/sync.py b/jobrunner/sync.py index ddf28431..d0086c38 100644 --- a/jobrunner/sync.py +++ b/jobrunner/sync.py @@ -9,7 +9,7 @@ import requests -from jobrunner import config, queries +from jobrunner import config, queries, record_stats from jobrunner.create_or_update_jobs import create_or_update_jobs from jobrunner.lib.database import find_where, select_values from jobrunner.lib.log_utils import configure_logging, set_log_context @@ -143,13 +143,14 @@ def job_to_remote_format(job): Convert our internal representation of a Job into whatever format the job-server expects """ + return { "identifier": job.id, "job_request_id": job.job_request_id, "action": job.action, "run_command": job.run_command, "status": job.state.value, - "status_code": job.status_code.value if job.status_code else "", + "status_code": job.status_code.value, "status_message": job.status_message or "", "created_at": job.created_at_isoformat, "updated_at": job.updated_at_isoformat, diff --git a/tests/factories.py b/tests/factories.py index 6d7c538b..968eb440 100644 --- a/tests/factories.py +++ b/tests/factories.py @@ -81,7 +81,7 @@ def job_factory(job_request=None, **kwargs): if "started_at" not in kwargs: status_code = kwargs.get("status_code", values["status_code"]) - if status_code >= StatusCode.EXECUTING: + if status_code and status_code >= StatusCode.EXECUTING: values["started_at"] = int(timestamp) if "status_code_updated_at" not in kwargs: