From e24a6d273ce5b90872fa7e49e39bcbb04006418b Mon Sep 17 00:00:00 2001 From: JacobLinCool Date: Fri, 2 Jun 2023 18:32:38 +0800 Subject: [PATCH] feat: early return if callback provided --- src/server/judge.rs | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/server/judge.rs b/src/server/judge.rs index 8910d11..fafab23 100644 --- a/src/server/judge.rs +++ b/src/server/judge.rs @@ -77,9 +77,34 @@ pub async fn judge( } }; + if let Some(callback) = submission.callback { + task::spawn(async move { + let result = run_specs(wasm, submission.specs).await; + let client = Client::new(); + match client.post(&callback).json(&result).send().await { + Ok(_) => { + println!("Callback sent successfully. ({})", &callback); + } + Err(e) => { + println!("Error sending callback. {} ({})", e, &callback); + } + } + }); + + Json(JudgeResults { + results: vec![], + error: None, + }) + } else { + let result = run_specs(wasm, submission.specs).await; + Json(result) + } +} + +pub async fn run_specs(wasm: Box<[u8]>, specs: Vec) -> JudgeResults { let mut tasks = Vec::new(); - for spec in submission.specs { + for spec in specs { let check = spec.check_spec().await; if let Err(e) = check { let task = task::spawn_local(async move { (Err(e), Err("".to_string()), None) }); @@ -188,22 +213,8 @@ pub async fn judge( } } - let result = JudgeResults { + JudgeResults { results, error: None, - }; - - if let Some(callback) = submission.callback { - let client = Client::new(); - match client.post(&callback).json(&result).send().await { - Ok(_) => { - println!("Callback sent successfully. ({})", &callback); - } - Err(e) => { - println!("Error sending callback. {} ({})", e, &callback); - } - } } - - Json(result) }