Ping server regularly

This commit is contained in:
Joscha 2023-08-11 01:29:05 +02:00
parent 33607779b0
commit 0ae43c608f
5 changed files with 380 additions and 8 deletions

View file

@ -187,11 +187,10 @@ impl ConfigFile {
.servers
.iter()
.map(|(name, server)| {
let url = server
.url
.strip_suffix('/')
.unwrap_or(&server.url)
.to_string();
let mut url = server.url.clone();
if !url.ends_with('/') {
url.push('/');
}
let token = server.token.to_string();
(name.to_string(), RunnerServerConfig { url, token })
})
@ -200,6 +199,7 @@ impl ConfigFile {
}
pub struct RunnerServerConfig {
/// Always ends with a `/`.
pub url: String,
pub token: String,
}

View file

@ -12,3 +12,7 @@ fn random_id(prefix: &str, length: usize) -> String {
pub fn random_runner_token() -> String {
random_id("t", 30)
}
pub fn random_runner_secret() -> String {
random_id("s", 30)
}

View file

@ -1,10 +1,13 @@
use std::sync::{Arc, Mutex};
use reqwest::Client;
use tokio::sync::mpsc;
use tracing::{debug, info_span, warn, Instrument};
use crate::{
config::{Config, RunnerServerConfig},
id,
shared::{RunnerRequest, RunnerStatus},
somehow,
};
@ -15,6 +18,8 @@ pub struct Server {
config: &'static Config,
server_config: &'static RunnerServerConfig,
coordinator: Arc<Mutex<Coordinator>>,
client: Client,
secret: String,
}
impl Server {
@ -29,6 +34,8 @@ impl Server {
config,
server_config,
coordinator,
client: Client::new(),
secret: id::random_runner_secret(),
}
}
@ -62,8 +69,22 @@ impl Server {
.await;
}
async fn ping(&mut self) -> somehow::Result<()> {
async fn ping(&self) -> somehow::Result<()> {
debug!("Pinging");
let request = RunnerRequest {
info: None,
secret: self.secret.clone(),
status: RunnerStatus::Idle,
request_work: false,
submit_work: None,
};
let url = format!("{}api/runner/status", self.server_config.url);
self.client
.post(url)
.basic_auth(&self.config.runner_name, Some(&self.server_config.token))
.json(&request)
.send()
.await?;
Ok(())
}
}