Skip to content

Commit

Permalink
Make pick_task not returning error when queue is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy authored Apr 9, 2024
1 parent cccdba6 commit 91b9e09
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 8 additions & 4 deletions judger/src/agent/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl PlatformClient {
}
}

pub async fn pick_task(&self) -> Result<JudgeTask, anyhow::Error> {
pub async fn pick_task(&self) -> Result<Option<JudgeTask>, anyhow::Error> {
pick_task(&self.client).await
}

Expand Down Expand Up @@ -45,18 +45,22 @@ struct PickTaskResponse {
task: JudgeTask,
}

async fn pick_task(client: &HttpClient) -> Result<JudgeTask, anyhow::Error> {
async fn pick_task(client: &HttpClient) -> Result<Option<JudgeTask>, anyhow::Error> {
let pick_url = "api/v1/judge/task/pick";
let body = PickTaskBody {
consumer: "".to_string(),
};
let response = client.post(pick_url.to_string()).json(&body).send().await?;

match response.status() {
reqwest::StatusCode::OK => Ok(response.json::<PickTaskResponse>().await?.task),
reqwest::StatusCode::OK => Ok(Some(response.json::<PickTaskResponse>().await?.task)),
reqwest::StatusCode::NO_CONTENT => Ok(None),
_ => {
log::error!("Failed to pick task: {:?}", response);
Err(anyhow::anyhow!("Queue is empty"))
Err(anyhow::anyhow!(format!(
"Failed to pick task: {:?}",
response
)))
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion judger/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ impl JudgeWorker {
loop {
interval.tick().await;
match platform_client.pick_task().await {
Ok(task) => {
Ok(maybe_task) => {
if maybe_task.is_none() {
continue;
}
let task = maybe_task.unwrap();
log::info!("Received task: {:?}", task);
match self.run_judge(
task.problem_slug.clone(),
Expand Down

0 comments on commit 91b9e09

Please sign in to comment.