Skip to content

Commit

Permalink
Expose metrics in job sync protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
bloodearnest committed Dec 13, 2023
1 parent f7cda63 commit c70cbc6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
6 changes: 4 additions & 2 deletions jobrunner/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -143,19 +143,21 @@ 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,
"started_at": job.started_at_isoformat,
"completed_at": job.completed_at_isoformat,
"trace_context": job.trace_context,
"metrics": record_stats.read_job_metrics(job.id),
}


Expand Down
28 changes: 28 additions & 0 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from jobrunner import config, queries, sync
from jobrunner.models import JobRequest
from tests.factories import job_factory, metrics_factory


def test_job_request_from_remote_format():
Expand Down Expand Up @@ -76,6 +77,33 @@ def test_job_request_from_remote_format_database_name_fallback():
assert job_request == expected


def test_job_to_remote_format_default(db):
job = job_factory()

json = sync.job_to_remote_format(job)

assert json["action"] == "action_name"
assert json["run_command"] == "python myscript.py"
assert json["status"] == "pending"
assert json["status_code"] == "created"
assert json["metrics"] == {}


def test_job_to_remote_format_null_status_message(db):
job = job_factory(status_message=None)
json = sync.job_to_remote_format(job)
assert json["status_message"] == ""


def test_job_to_remote_format_metrics(db):
job = job_factory()
metrics_factory(job, metrics={"test": 0.0})

json = sync.job_to_remote_format(job)

assert json["metrics"] == {"test": 0.0}


def test_session_request_no_flags(db, responses):
responses.add(
method="GET",
Expand Down

0 comments on commit c70cbc6

Please sign in to comment.