Fix /api/runner/status

This commit is contained in:
Joscha 2023-08-10 23:31:46 +02:00
parent f3d646c8d5
commit 2079d0b12d
3 changed files with 19 additions and 10 deletions

View file

@ -12,6 +12,7 @@ use axum::{
}; };
use sqlx::SqlitePool; use sqlx::SqlitePool;
use time::OffsetDateTime; use time::OffsetDateTime;
use tracing::debug;
use crate::{ use crate::{
config::Config, config::Config,
@ -24,7 +25,7 @@ use crate::{
}; };
async fn post_status( async fn post_status(
TypedHeader(auth): TypedHeader<Authorization<Basic>>, auth: Option<TypedHeader<Authorization<Basic>>>,
State(config): State<&'static Config>, State(config): State<&'static Config>,
State(db): State<SqlitePool>, State(db): State<SqlitePool>,
State(bench_repo): State<Option<BenchRepo>>, State(bench_repo): State<Option<BenchRepo>>,
@ -62,6 +63,8 @@ async fn post_status(
let abort_work = guard.should_abort_work(&name); let abort_work = guard.should_abort_work(&name);
drop(guard); drop(guard);
// TODO Insert finished work into DB
// Find new work // Find new work
let work = if let Some(hash) = work { let work = if let Some(hash) = work {
let bench = match bench_repo { let bench = match bench_repo {
@ -78,14 +81,16 @@ async fn post_status(
None None
}; };
debug!("Received status update from {name}");
Ok(Json(ServerResponse { work, abort_work }).into_response()) Ok(Json(ServerResponse { work, abort_work }).into_response())
} }
pub fn router(server: &Server) -> Router<Server> { pub fn router(server: &Server) -> Router<Server> {
if server.repo.is_none() { if server.repo.is_none() {
return Router::new().route("/api/runner/status", post(post_status)); return Router::new();
} }
// TODO Add routes // TODO Get repo tar
Router::new() // TODO Get bench repo tar
Router::new().route("/api/runner/status", post(post_status))
} }

View file

@ -3,6 +3,7 @@ use axum::{
headers::{authorization::Basic, Authorization}, headers::{authorization::Basic, Authorization},
http::{header, HeaderValue, StatusCode}, http::{header, HeaderValue, StatusCode},
response::Response, response::Response,
TypedHeader,
}; };
use crate::config::Config; use crate::config::Config;
@ -23,11 +24,13 @@ fn is_password_valid(password: &str, config: &'static Config) -> bool {
pub fn authenticate( pub fn authenticate(
config: &'static Config, config: &'static Config,
auth: Authorization<Basic>, auth: Option<TypedHeader<Authorization<Basic>>>,
) -> Result<String, Response> { ) -> Result<String, Response> {
if let Some(auth) = auth {
if is_username_valid(auth.username()) && is_password_valid(auth.password(), config) { if is_username_valid(auth.username()) && is_password_valid(auth.password(), config) {
return Ok(auth.username().to_string()); return Ok(auth.username().to_string());
} }
}
Err(( Err((
StatusCode::UNAUTHORIZED, StatusCode::UNAUTHORIZED,

View file

@ -23,7 +23,7 @@ pub struct Measurement {
} }
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
#[serde(rename = "snake_case")] #[serde(rename_all = "snake_case")]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum Line { pub enum Line {
Stdout(String), Stdout(String),
@ -41,7 +41,7 @@ pub struct FinishedRun {
} }
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
#[serde(rename = "snake_case")] #[serde(rename_all = "snake_case")]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum RunnerStatus { pub enum RunnerStatus {
/// The runner is not performing any work. /// The runner is not performing any work.
@ -74,6 +74,7 @@ pub struct RunnerRequest {
/// ///
/// If the server has a commit available, it should respond with a non-null /// If the server has a commit available, it should respond with a non-null
/// [`Response::work`]. /// [`Response::work`].
#[serde(default)]
pub request_work: bool, pub request_work: bool,
/// The runner has finished a run and wants to submit the results. /// The runner has finished a run and wants to submit the results.
@ -81,7 +82,7 @@ pub struct RunnerRequest {
} }
#[derive(Clone, Serialize, Deserialize)] #[derive(Clone, Serialize, Deserialize)]
#[serde(rename = "snake_case")] #[serde(rename_all = "snake_case")]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum BenchMethod { pub enum BenchMethod {
/// Use internal (deterministic) benchmarking code. /// Use internal (deterministic) benchmarking code.