Ask servers for runs and perform them
This commit is contained in:
parent
eaca373a6a
commit
b7c0443005
3 changed files with 164 additions and 32 deletions
|
|
@ -5,10 +5,18 @@ mod tree;
|
|||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use reqwest::Client;
|
||||
use time::OffsetDateTime;
|
||||
use tokio::sync::Mutex as AsyncMutex;
|
||||
use tracing::{error, info};
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
use crate::{config::Config, id, worker::server::Server};
|
||||
use crate::{
|
||||
config::Config,
|
||||
id,
|
||||
shared::{FinishedRun, Run},
|
||||
worker::server::Server,
|
||||
};
|
||||
|
||||
use self::run::RunInProgress;
|
||||
|
||||
pub struct Worker {
|
||||
config: &'static Config,
|
||||
|
|
@ -51,10 +59,69 @@ impl Worker {
|
|||
}
|
||||
|
||||
async fn single_server_mode(&self, server: Server) {
|
||||
// TODO Implement
|
||||
loop {
|
||||
while self.perform_run(&server).await {}
|
||||
tokio::time::sleep(self.config.worker_ping_delay).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn many_server_mode(&self, servers: Vec<Server>) {
|
||||
// TODO Implement
|
||||
loop {
|
||||
for server in &servers {
|
||||
let batch_start = OffsetDateTime::now_utc();
|
||||
let batch_end = batch_start + self.config.worker_batch_duration;
|
||||
while OffsetDateTime::now_utc() <= batch_end {
|
||||
if !self.perform_run(server).await {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(self.config.worker_ping_delay).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Ask a server for a run, do the run, send results to the server.
|
||||
///
|
||||
/// Returns whether a run was performed.
|
||||
async fn perform_run(&self, server: &Server) -> bool {
|
||||
// Request run
|
||||
let guard = server.status_lock.lock().await;
|
||||
let Some(run) = self.request_run(server).await else { return false; };
|
||||
let run = RunInProgress::new(server.name.clone(), run);
|
||||
*server.current_run.lock().unwrap() = Some(run.clone());
|
||||
drop(guard);
|
||||
|
||||
// Perform run
|
||||
let Some(run) = run.perform().await else { return false; };
|
||||
|
||||
// Submit run
|
||||
let guard = server.status_lock.lock().await;
|
||||
*server.current_run.lock().unwrap() = None;
|
||||
while !self.submit_run(server, run.clone()).await {
|
||||
tokio::time::sleep(self.config.worker_ping_delay).await;
|
||||
}
|
||||
drop(guard);
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
async fn request_run(&self, server: &Server) -> Option<Run> {
|
||||
match server.post_status(true, None).await {
|
||||
Ok(response) => response.run,
|
||||
Err(e) => {
|
||||
warn!("Error requesting run:\n{e:?}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn submit_run(&self, server: &Server, run: FinishedRun) -> bool {
|
||||
match server.post_status(false, Some(run)).await {
|
||||
Ok(_) => true,
|
||||
Err(e) => {
|
||||
warn!("Error submitting run:\n{e:?}");
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue