Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions JobRunner/callback_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def _handle_submit(app, module, method, data, token):
_check_rpc_token(app, token)
job_id = str(uuid.uuid4())
data["method"] = "%s.%s" % (module, method)
outputs[job_id] = {"finished": 0}
app.config["out_q"].put(["submit", job_id, data])
app.config["jobcount"] += 1
return {"result": [job_id]}
Expand All @@ -196,15 +197,33 @@ def _error(errstr, trace=None, code=-32000):
return {"error": err}


def _is_uuid(putative: str) -> bool:
"""
Return True if `putative` is a valid UUID string, False otherwise.
"""
try:
uuid.UUID(putative)
except ValueError:
return False
return True


def _handle_checkjob(app, data):
if "params" not in data:
raise SanicException(status_code=404)
if (not data.get("params")
or not isinstance(data["params"], list)
or len(data["params"]) != 1
or not isinstance(data["params"][0], str)
):
return _error("method params must be a list containing exactly one job ID string")
job_id = data["params"][0]
if not _is_uuid(job_id):
return _error(f"Invalid job ID: {data['params'][0]}")
_check_finished(app, f"Checkjob for {job_id}")
resp = {"finished": 0}
if job_id not in outputs:
return _error(f"No such job ID: {job_id}")

if job_id in outputs:
resp = outputs[job_id]
resp = outputs[job_id]
if resp.get("finished") != 0:
resp["finished"] = 1
if "error" in resp:
return resp
Expand Down Expand Up @@ -246,7 +265,7 @@ async def _process_rpc(app, data, token):
try:
while True:
_check_finished(app, f'synk check for {data["method"]} for {job_id}')
if job_id in outputs:
if outputs[job_id].get("finished") != 0:
resp = outputs[job_id]
resp["finished"] = 1
return resp
Expand Down
46 changes: 45 additions & 1 deletion test/test_callback_server_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import os
import pytest
import requests
import socket
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The socket import is moved but appears to be duplicated. There's already an import socket statement after line 16 that should be removed to avoid redundancy.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That "duplicate line" is a deletion

import time
from typing import Any
import uuid

from JobRunner.Callback import Callback
import socket

# NOTE - the CBS is started once for this module. Don't assume it has any particular provenance
# stored.
Expand Down Expand Up @@ -509,3 +510,46 @@ def test_submit_fail_max_jobs_limit(callback_ports):
"id": "callback",
"version": "1.1"
}


def test_check_job_fail_no_params(callback_ports):
port = callback_ports[0]
err = "method params must be a list containing exactly one job ID string"

resp = _post(port, {"method": "foo._check_job"})
j = resp.json()
assert j == {
"error": {
"code": -32000,
"name": "CallbackServerError",
"message": err,
},
}


def test_check_job_fail_bad_params(callback_ports):
port = callback_ports[0]
err = "method params must be a list containing exactly one job ID string"
randomuuid = str(uuid.uuid4())

testset = [
(None, err),
({}, err),
("foo", err),
([], err),
(["foo", "bar"], err),
([[]], err),
([{}], err),
([randomuuid], f"No such job ID: {randomuuid}"),
([randomuuid + "g"], f"Invalid job ID: {randomuuid}g"),
]
for t, e in testset:
resp = _post(port, {"method": "foo._check_job", "params": t})
j = resp.json()
assert j == {
"error": {
"code": -32000,
"name": "CallbackServerError",
"message": e,
},
}
Loading