Abort run when signaled to by server

This commit is contained in:
Joscha 2024-05-13 17:02:45 +02:00
parent dbb9d487b3
commit 437be12f14
2 changed files with 27 additions and 12 deletions

View file

@ -5,8 +5,8 @@ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use log::error; use log::{error, warn};
use tokio::sync::Notify; use tokio::{select, sync::Notify};
use crate::{ use crate::{
config::WorkerServerConfig, config::WorkerServerConfig,
@ -82,12 +82,19 @@ impl RunInProgress {
pub async fn perform(&self, server: &Server) -> Option<FinishedRun> { pub async fn perform(&self, server: &Server) -> Option<FinishedRun> {
// TODO Log system info // TODO Log system info
// TODO Handle aborts
let result = match &self.run.bench_method { let run_future = match &self.run.bench_method {
BenchMethod::Internal => self.perform_internal(server), BenchMethod::Internal => self.perform_internal(server),
BenchMethod::Repo { hash } => todo!(), BenchMethod::Repo { hash } => todo!(),
} };
.await;
let result = select! {
result = run_future => result,
_ = self.abort.notified() => {
warn!("Run for {} was aborted", server.name);
Ok(None)
},
};
let run = match result { let run = match result {
Ok(outcome) => outcome, Ok(outcome) => outcome,
@ -116,4 +123,8 @@ impl RunInProgress {
measurements: run.measurements, measurements: run.measurements,
}) })
} }
pub fn abort(&self) {
self.abort.notify_one();
}
} }

View file

@ -38,7 +38,7 @@ pub struct Server {
/// 1. The main task requests a run /// 1. The main task requests a run
/// 2. The ping task sends a status update where the worker is idle /// 2. The ping task sends a status update where the worker is idle
/// 3. The server receives 1, reserves a run and replies /// 3. The server receives 1, reserves a run and replies
/// 4. The server receives 2 and clears the reservatio /// 4. The server receives 2 and clears the reservation
/// 5. Another worker requests a run before this worker's next ping /// 5. Another worker requests a run before this worker's next ping
pub status_lock: Arc<AsyncMutex<()>>, pub status_lock: Arc<AsyncMutex<()>>,
} }
@ -80,6 +80,12 @@ impl Server {
.json::<ServerResponse>() .json::<ServerResponse>()
.await?; .await?;
if response.abort_run {
if let Some(current_run) = &*self.current_run.lock().unwrap() {
current_run.abort();
}
}
Ok(response) Ok(response)
} }
@ -123,13 +129,11 @@ impl Server {
async fn ping(&self) -> somehow::Result<()> { async fn ping(&self) -> somehow::Result<()> {
debug!("Pinging {}", self.name); debug!("Pinging {}", self.name);
let guard = self.status_lock.lock().await; let guard = self.status_lock.lock().await;
let _ = self.post_status(false, None).await?;
let response = self.post_status(false, None).await?;
// TODO Signal that run should be aborted
drop(guard); drop(guard);
Ok(()) Ok(())
} }