Skip to content

Commit

Permalink
Update job_position_get to use job_priority in ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
val500 committed Oct 22, 2024
1 parent 3cf6743 commit 11e8f2c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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
30 changes: 30 additions & 0 deletions server/tests/test_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,3 +907,33 @@ def test_job_get_with_priority(mongo_app_with_permissions):
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 11e8f2c

Please sign in to comment.