This commit is contained in:
nora 2022-09-28 22:11:38 +02:00
parent e78b5becde
commit b36cab3491
6 changed files with 248 additions and 63 deletions

View file

@ -1,18 +1,21 @@
use std::process::Output;
use std::{fs, process::Command, sync::mpsc};
use color_eyre::eyre::Context;
use color_eyre::eyre::{Context, ContextCompat};
use color_eyre::Result;
use rusqlite::Connection;
use serde::Serialize;
use tracing::{error, info};
use uuid::Uuid;
use crate::Options;
use crate::{db, Options};
#[derive(Debug, Serialize)]
#[serde(tag = "status")]
pub enum BisectStatus {
InProgress,
Error(String),
Success(String),
Error { output: String },
Success { output: String },
}
#[derive(Debug, Serialize)]
@ -29,15 +32,15 @@ pub struct Job {
}
enum JobState {
Failed(String),
Success(String),
Failed,
Success,
}
impl JobState {
fn status(&self) -> &'static str {
match self {
Self::Failed(_) => "error",
Self::Success(_) => "success",
Self::Failed => "error",
Self::Success => "success",
}
}
}
@ -66,27 +69,69 @@ pub fn bisect_worker(jobs: mpsc::Receiver<Job>, conn: Connection) {
info!(id = %job.id, "Starting bisection job");
bisect_job(job);
let mut bisect = Bisection {
id: job.id,
code: job.code.clone(),
status: BisectStatus::InProgress,
};
match db::add_bisection(&conn, &bisect).wrap_err("insert bisection") {
Ok(()) => {
let status = match bisect_job(job) {
Ok(status) => status,
Err(err) => {
error!(?err, "error processing bisection");
BisectStatus::Error {
output: err.to_string(),
}
}
};
bisect.status = status;
match db::update_bisection_status(&conn, &bisect) {
Ok(()) => {}
Err(err) => error!(?err, "error updating bisection"),
}
}
Err(err) => error!(?err, "error inserting bisection"),
}
}
}
#[tracing::instrument(skip(job), fields(id = %job.id))]
fn bisect_job(job: Job) {
match run_bisect_for_file(job.code, job.options.start, job.options.end) {
Ok(state) => {
info!(state = %state.status(), "Bisection finished");
fn bisect_job(job: Job) -> Result<BisectStatus> {
let (output, state) = run_bisect_for_file(job.code, job.options.start, job.options.end)?;
info!(state = %state.status(), "Bisection finished");
process_result(output, state).wrap_err("process result")
}
fn process_result(output: Output, state: JobState) -> Result<BisectStatus> {
let stderr =
String::from_utf8(output.stderr).wrap_err("cargo-bisect-rustc stderr utf8 validation")?;
match state {
JobState::Failed => {
let output = stderr.lines().rev().take(10).collect::<String>();
info!(?output, "output");
Ok(BisectStatus::Error { output })
}
Err(err) => {
error!(?err, "Error during bisection");
JobState::Success => {
let cutoff = stderr.rfind("searched nightlies:").wrap_err_with(|| {
format!("cannot find `searched nightlies:` in output. output:\n{stderr}")
})?;
let output = stderr[cutoff..].to_string();
Ok(BisectStatus::Success { output })
}
}
}
fn run_bisect_for_file(
input: String,
start: Option<chrono::NaiveDate>,
start: chrono::NaiveDate,
end: Option<chrono::NaiveDate>,
) -> color_eyre::Result<JobState> {
) -> Result<(Output, JobState)> {
let temp_dir = tempdir::TempDir::new("bisect").wrap_err("creating tempdir")?;
let mut cargo_new = Command::new("cargo");
cargo_new
@ -111,9 +156,7 @@ fn run_bisect_for_file(
bisect.arg("--timeout").arg("30"); // don't hang
bisect.current_dir(&cargo_dir);
if let Some(start) = start {
bisect.arg("--start").arg(start.to_string());
}
bisect.arg("--start").arg(start.to_string());
if let Some(end) = end {
bisect.arg("--end").arg(end.to_string());
@ -122,14 +165,8 @@ fn run_bisect_for_file(
let output = bisect.output().wrap_err("spawning cargo-bisect-rustc")?;
if output.status.success() {
Ok(JobState::Success(
String::from_utf8(output.stdout)
.wrap_err("cargo-bisect-rustc stdout utf8 validation")?,
))
Ok((output, JobState::Success))
} else {
Ok(JobState::Failed(
String::from_utf8(output.stderr)
.wrap_err("cargo-bisect-rustc stderr utf8 validation")?,
))
Ok((output, JobState::Failed))
}
}

View file

@ -1,6 +1,7 @@
use color_eyre::eyre::Context;
use rusqlite::Connection;
use tracing::info;
use uuid::Uuid;
use crate::bisect::{BisectStatus, Bisection};
@ -21,13 +22,39 @@ pub fn setup(conn: &Connection) -> color_eyre::Result<()> {
Ok(())
}
pub fn add_bisection(conn: &Connection) -> color_eyre::Result<()> {
Ok(())
fn status_to_sql(status: &BisectStatus) -> (u8, Option<&str>) {
match status {
BisectStatus::InProgress => (0, None),
BisectStatus::Error { output } => (1, Some(&output)),
BisectStatus::Success { output } => (2, Some(&output)),
}
}
pub fn add_bisection(conn: &Connection, bisect: &Bisection) -> color_eyre::Result<()> {
let (status, stdout_stderr) = status_to_sql(&bisect.status);
conn.execute(
"INSERT INTO bisect (job_id, code, status, stdout_stderr) VALUES (?1, ?2, ?3, ?4)",
(bisect.id, &bisect.code, status, stdout_stderr),
)
.wrap_err("insert into database")
.map(drop)
}
pub fn update_bisection_status(conn: &Connection, bisect: &Bisection) -> color_eyre::Result<()> {
let (status, stdout_stderr) = status_to_sql(&bisect.status);
conn.execute(
"UPDATE bisect SET status = ?1, stdout_stderr = ?2 WHERE bisect.job_id = ?3",
(status, stdout_stderr, bisect.id),
)
.wrap_err("insert into database")
.map(drop)
}
pub fn get_bisections(conn: &Connection) -> color_eyre::Result<Vec<Bisection>> {
let mut select = conn
.prepare("SELECT job_id, code, status, stdout, stderr FROM bisect")
.prepare("SELECT job_id, code, status, stdout_stderr FROM bisect")
.wrap_err("preparing select")?;
let iter = select
@ -37,8 +64,12 @@ pub fn get_bisections(conn: &Connection) -> color_eyre::Result<Vec<Bisection>> {
code: row.get(1)?,
status: match row.get(2)? {
0 => BisectStatus::InProgress,
1 => BisectStatus::Error(row.get(3)?),
2 => BisectStatus::Success(row.get(3)?),
1 => BisectStatus::Error {
output: row.get(3)?,
},
2 => BisectStatus::Success {
output: row.get(3)?,
},
_ => return Err(rusqlite::Error::InvalidQuery), // actually not lol
},
})
@ -48,3 +79,30 @@ pub fn get_bisections(conn: &Connection) -> color_eyre::Result<Vec<Bisection>> {
iter.collect::<Result<Vec<_>, rusqlite::Error>>()
.wrap_err("getting bisections from db")
}
pub fn get_bisection(conn: &Connection, id: Uuid) -> color_eyre::Result<Option<Bisection>> {
let mut select = conn
.prepare("SELECT job_id, code, status, stdout_stderr FROM bisect WHERE job_id = ?1")
.wrap_err("preparing select")?;
let mut iter = select
.query_map([id], |row| {
Ok(Bisection {
id: row.get(0)?,
code: row.get(1)?,
status: match row.get(2)? {
0 => BisectStatus::InProgress,
1 => BisectStatus::Error {
output: row.get(3)?,
},
2 => BisectStatus::Success {
output: row.get(3)?,
},
_ => return Err(rusqlite::Error::InvalidQuery), // actually not lol
},
})
})
.wrap_err("getting bisections from db query")?;
iter.next().transpose().wrap_err("getting bisection")
}

View file

@ -6,7 +6,7 @@ mod db;
use std::sync::{mpsc, Arc, Mutex};
use axum::{
extract::Query,
extract::{Path, Query},
http::StatusCode,
response::{Html, IntoResponse},
routing::{get, post, Router},
@ -18,6 +18,7 @@ use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use std::env;
use tracing::{error, info};
use tracing_subscriber::EnvFilter;
use uuid::Uuid;
type SendChannel = Arc<Mutex<mpsc::Sender<Job>>>;
@ -27,7 +28,9 @@ type Conn = Arc<Mutex<Connection>>;
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
tracing_subscriber::fmt().init();
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
let (job_queue_send, job_queue_recv) = mpsc::channel();
@ -44,7 +47,7 @@ async fn main() -> color_eyre::Result<()> {
let app = Router::new()
.route("/", get(|| async { index_html() }))
// .route("/bisect/:id", get(get_bisection))
.route("/bisect/:id", get(get_bisection))
.route("/bisect", get(get_bisections))
.route("/bisect", post(do_bisection))
// this is really stupid and hacky
@ -65,6 +68,18 @@ fn index_html() -> impl IntoResponse {
Html(include_str!("../index.html"))
}
async fn get_bisection(
Extension(conn): Extension<Conn>,
Path(id): Path<Uuid>,
) -> impl IntoResponse {
db::get_bisection(&conn.lock().unwrap(), id)
.map(|bisections| Json(bisections))
.map_err(|err| {
error!(?err, "error getting bisections");
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
})
}
async fn get_bisections(Extension(conn): Extension<Conn>) -> impl IntoResponse {
db::get_bisections(&conn.lock().unwrap())
.map(|bisections| Json(bisections))
@ -76,7 +91,7 @@ async fn get_bisections(Extension(conn): Extension<Conn>) -> impl IntoResponse {
#[derive(Debug, Deserialize)]
pub struct Options {
start: Option<chrono::NaiveDate>,
start: chrono::NaiveDate,
end: Option<chrono::NaiveDate>,
}