Poke server tasks in coordinator

This commit is contained in:
Joscha 2023-08-11 01:11:53 +02:00
parent 7911a67906
commit 33607779b0
3 changed files with 82 additions and 41 deletions

View file

@ -1,53 +1,44 @@
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use std::sync::{Arc, Mutex};
use time::OffsetDateTime;
use tokio::sync::mpsc;
use tracing::{debug, info_span, warn, Instrument};
use crate::{config::RunnerServerConfig, somehow};
use crate::{
config::{Config, RunnerServerConfig},
somehow,
};
use super::coordinator::Coordinator;
enum RunState {
Preparing,
Running,
Finished, // TODO Include run results here
}
struct Run {
id: String,
hash: String,
start: OffsetDateTime,
state: RunState,
}
pub struct Server {
name: String,
config: &'static RunnerServerConfig,
ping_delay: Duration,
config: &'static Config,
server_config: &'static RunnerServerConfig,
coordinator: Arc<Mutex<Coordinator>>,
run: Option<Arc<Mutex<Run>>>,
}
impl Server {
pub fn new(
name: String,
config: &'static RunnerServerConfig,
ping_delay: Duration,
config: &'static Config,
server_config: &'static RunnerServerConfig,
coordinator: Arc<Mutex<Coordinator>>,
) -> Self {
Self {
name,
config,
ping_delay,
server_config,
coordinator,
run: None,
}
}
pub async fn run(&mut self) {
let (poke_tx, mut poke_rx) = mpsc::unbounded_channel();
self.coordinator
.lock()
.unwrap()
.register(self.name.clone(), poke_tx.clone());
let name = self.name.clone();
async {
loop {
@ -55,7 +46,16 @@ impl Server {
Ok(()) => {}
Err(e) => warn!("Error talking to server:\n{e:?}"),
}
tokio::time::sleep(self.ping_delay).await;
// Wait for poke or until the ping delay elapses. If we get
// poked while pinging the server, this will not wait and we'll
// immediately do another ping.
let _ = tokio::time::timeout(self.config.runner_ping_delay, poke_rx.recv()).await;
// Empty queue in case we were poked more than once. This can
// happen for example if we get poked multiple times while
// pinging the server.
while poke_rx.try_recv().is_ok() {}
}
}
.instrument(info_span!("runner", name))