Skip to content

Commit

Permalink
Merge pull request #375 from canonical/CERTTF-373
Browse files Browse the repository at this point in the history
Update server logic to select jobs with high priority
  • Loading branch information
val500 authored Oct 24, 2024
2 parents 610c6ef + 11e8f2c commit 0043f95
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions server/src/api/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ def job_position_get(job_id):
jobs = database.mongo.db.jobs.find(
{"job_data.job_queue": queue, "result_data.job_state": "waiting"},
{"job_id": 1},
sort=[("job_priority", -1)],
)
# Create a dict mapping job_id (as a string) to the position in the queue
jobs_id_position = {job.get("job_id"): pos for pos, job in enumerate(jobs)}
Expand Down
3 changes: 2 additions & 1 deletion server/src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ def pop_job(queue_list):
"job_id": True,
"created_at": True,
"job_data": True,
"_id": False,
"_id": True,
},
sort=[("job_priority", -1)],
)
except TypeError:
return None
Expand Down
60 changes: 60 additions & 0 deletions server/tests/test_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,63 @@ def test_missing_fields_in_token(mongo_app_with_permissions):
)
assert 403 == job_response.status_code
assert "Invalid Token" in job_response.text


def test_job_get_with_priority(mongo_app_with_permissions):
"""Tests job get returns job with highest job priority"""
app, _, client_id, client_key, _ = mongo_app_with_permissions
authenticate_output = app.post(
"/v1/oauth2/token",
headers=create_auth_header(client_id, client_key),
)
token = authenticate_output.data.decode("utf-8")
jobs = [
{"job_queue": "myqueue2"},
{"job_queue": "myqueue2", "job_priority": 200},
{"job_queue": "myqueue2", "job_priority": 100},
]
job_ids = []
for job in jobs:
job_response = app.post(
"/v1/job", json=job, headers={"Authorization": token}
)
job_id = job_response.json.get("job_id")
job_ids.append(job_id)
returned_job_ids = []
for _ in range(len(jobs)):
job_get_response = app.get("/v1/job?queue=myqueue2")
job_id = job_get_response.json.get("job_id")
returned_job_ids.append(job_id)
assert returned_job_ids[0] == job_ids[1]
assert returned_job_ids[1] == job_ids[2]
assert returned_job_ids[2] == job_ids[0]


def test_job_position_get_with_priority(mongo_app_with_permissions):
"""Tests job position get returns correct position with priority"""
app, _, client_id, client_key, _ = mongo_app_with_permissions
authenticate_output = app.post(
"/v1/oauth2/token",
headers=create_auth_header(client_id, client_key),
)
token = authenticate_output.data.decode("utf-8")
jobs = [
{"job_queue": "myqueue2"},
{"job_queue": "myqueue2", "job_priority": 200},
{"job_queue": "myqueue2", "job_priority": 100},
]
job_ids = []
for job in jobs:
job_response = app.post(
"/v1/job", json=job, headers={"Authorization": token}
)
job_id = job_response.json.get("job_id")
job_ids.append(job_id)

job_positions = []
for job_id in job_ids:
job_positions.append(app.get(f"/v1/job/{job_id}/position").text)

assert job_positions[0] == "2"
assert job_positions[1] == "0"
assert job_positions[2] == "1"

0 comments on commit 0043f95

Please sign in to comment.