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

37
Cargo.lock generated
View file

@ -479,6 +479,15 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "matchers"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
dependencies = [
"regex-automata",
]
[[package]]
name = "matchit"
version = "0.5.0"
@ -699,6 +708,30 @@ dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
@ -1005,9 +1038,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60db860322da191b40952ad9affe65ea23e7dd6a5c442c2c42865810c6ab8e6b"
dependencies = [
"ansi_term",
"matchers",
"once_cell",
"regex",
"sharded-slab",
"smallvec",
"thread_local",
"tracing",
"tracing-core",
"tracing-log",
]

View file

@ -14,5 +14,5 @@ serde = { version = "1.0.145", features = ["derive"] }
tempdir = "0.3.7"
tokio = { version = "1.21.2", features = ["full"] }
tracing = "0.1.36"
tracing-subscriber = "0.3.15"
tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
uuid = { version = "1.1.2", features = ["serde", "v4"] }

View file

@ -7,6 +7,10 @@
font-family: Arial, Helvetica, sans-serif;
}
#result {
font-family: monospace;
}
.hidden {
display: none;
}
@ -30,55 +34,89 @@
cols="80"
placeholder="// Rust code goes here..."
>
fn uwu() {}
</textarea>
fn main() {}</textarea
>
<br />
<label for="start">Start</label>
<input id="start" value="2022-01-01" />
<label for="end">End (optional)</label>
<input id="end" />
<br />
<br />
<button class="bisect-btn" onclick="bisect()">Bisect!</button>
<div id="status" class="hidden" />
<div id="result" class="hidden" />
<br />
<br />
<div id="status" class="hidden"></div>
<br />
<textarea
id="result"
class="hidden"
cols="80"
rows="20"
readonly
></textarea>
<script>
const BASE_URL = `${document.location}`;
const code = document.getElementById("code");
const status = document.getElementById("status");
const result = document.getElementById("result");
const start = document.getElementById("start");
const end = document.getElementById("end");
let bisecting = false;
async function bisect() {
if (bisecting) {
return;
}
if (!start.value.trim()) {
alert("Must provide a start");
return;
}
bisecting = true;
const code = document.getElementById("code");
const status = document.getElementById("status");
const result = document.getElementById("result");
status.classList.remove("hidden");
result.classList.add("hidden");
status.innerText = "Sending request...";
const fetched = await fetch(
"https://bisect-rustc.nilstrieb.dev/bisect",
{
method: "POST",
body: code.value,
}
);
const { id } = await fetched.json();
const params = new URLSearchParams();
params.append("start", start.value.trim());
if (end.value.trim()) {
params.append("end", end.value.trim());
}
const fetched = await fetch(`${BASE_URL}bisect?${params}`, {
method: "POST",
body: code.value,
});
const { job_id } = await fetched.json();
async function tryFetch() {
const fetched = await fetch(
`https://bisect-rustc.nilstrieb.dev/bisect/${id}`
);
const { done, output, time } = await fetched.json();
const fetched = await fetch(`${BASE_URL}bisect/${job_id}`);
const response = await fetched.json();
if (done) {
if (response.status.status !== "InProgress") {
bisecting = false;
result.innerText = output;
status.innerText = `Bisected job ${id} successfully in ${time}ms`;
}
console.log(response.status.output);
setTimeout(tryFetch, 3000);
status.classList.add("hidden");
result.classList.remove("hidden");
result.value = response.status.output;
status.innerHTML = `Bisected job ${job_id}`;
} else {
console.log("Waiting for bisection", response.status.status);
setTimeout(tryFetch, 3000);
}
}
tryFetch();
status.innerHTML = `Waiting for bisection, job id=${id}`;
status.innerHTML = `Waiting for result, job id=${job_id}`;
}
</script>
</body>

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>,
}