Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update server logic to select jobs with high priority #375

Merged
merged 2 commits into from
Oct 24, 2024
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
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"