mirror of
https://github.com/Noratrieb/cargo-bisect-rustc-service.git
synced 2026-01-14 16:25:01 +01:00
stuff
This commit is contained in:
parent
4bcd725fb0
commit
e78b5becde
8 changed files with 1614 additions and 54 deletions
135
src/bisect.rs
Normal file
135
src/bisect.rs
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
use std::{fs, process::Command, sync::mpsc};
|
||||
|
||||
use color_eyre::eyre::Context;
|
||||
use rusqlite::Connection;
|
||||
use serde::Serialize;
|
||||
use tracing::{error, info};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::Options;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub enum BisectStatus {
|
||||
InProgress,
|
||||
Error(String),
|
||||
Success(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Bisection {
|
||||
pub id: Uuid,
|
||||
pub code: String,
|
||||
pub status: BisectStatus,
|
||||
}
|
||||
|
||||
pub struct Job {
|
||||
id: Uuid,
|
||||
code: String,
|
||||
options: Options,
|
||||
}
|
||||
|
||||
enum JobState {
|
||||
Failed(String),
|
||||
Success(String),
|
||||
}
|
||||
|
||||
impl JobState {
|
||||
fn status(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Failed(_) => "error",
|
||||
Self::Success(_) => "success",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Job {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Job")
|
||||
.field("id", &self.id)
|
||||
.field("options", &self.options)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
impl Job {
|
||||
pub fn new(id: Uuid, code: String, options: Options) -> Self {
|
||||
Self { id, code, options }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bisect_worker(jobs: mpsc::Receiver<Job>, conn: Connection) {
|
||||
loop {
|
||||
let job = match jobs.recv() {
|
||||
Ok(job) => job,
|
||||
Err(_) => return,
|
||||
};
|
||||
|
||||
info!(id = %job.id, "Starting bisection job");
|
||||
|
||||
bisect_job(job);
|
||||
}
|
||||
}
|
||||
|
||||
#[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");
|
||||
}
|
||||
Err(err) => {
|
||||
error!(?err, "Error during bisection");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run_bisect_for_file(
|
||||
input: String,
|
||||
start: Option<chrono::NaiveDate>,
|
||||
end: Option<chrono::NaiveDate>,
|
||||
) -> color_eyre::Result<JobState> {
|
||||
let temp_dir = tempdir::TempDir::new("bisect").wrap_err("creating tempdir")?;
|
||||
let mut cargo_new = Command::new("cargo");
|
||||
cargo_new
|
||||
.arg("new")
|
||||
.arg("bisect")
|
||||
.arg("--lib") // lib even works with `fn main() {}`
|
||||
.current_dir(&temp_dir);
|
||||
|
||||
let output = cargo_new.output().wrap_err("cargo init")?;
|
||||
output
|
||||
.status
|
||||
.exit_ok()
|
||||
.wrap_err_with(|| format!("running cargo: {}", String::from_utf8_lossy(&output.stderr)))?;
|
||||
|
||||
let cargo_dir = temp_dir.path().join("bisect");
|
||||
|
||||
fs::write(cargo_dir.join("src").join("lib.rs"), input).wrap_err("writing code to lib.rs")?;
|
||||
|
||||
let mut bisect = Command::new("cargo-bisect-rustc");
|
||||
bisect.arg("--preserve"); // preserve toolchains for future runs
|
||||
bisect.arg("--access").arg("github"); // ask the github api about the commits
|
||||
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());
|
||||
}
|
||||
|
||||
if let Some(end) = end {
|
||||
bisect.arg("--end").arg(end.to_string());
|
||||
}
|
||||
|
||||
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")?,
|
||||
))
|
||||
} else {
|
||||
Ok(JobState::Failed(
|
||||
String::from_utf8(output.stderr)
|
||||
.wrap_err("cargo-bisect-rustc stderr utf8 validation")?,
|
||||
))
|
||||
}
|
||||
}
|
||||
50
src/db.rs
Normal file
50
src/db.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
use color_eyre::eyre::Context;
|
||||
use rusqlite::Connection;
|
||||
use tracing::info;
|
||||
|
||||
use crate::bisect::{BisectStatus, Bisection};
|
||||
|
||||
pub fn setup(conn: &Connection) -> color_eyre::Result<()> {
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS bisect (
|
||||
job_id STRING PRIMARY KEY,
|
||||
code STRING NOT NULL,
|
||||
status INTEGER NOT NULL DEFAULT 0,
|
||||
stdout_stderr STRING -- stdout or stderr depending on the status
|
||||
)",
|
||||
(),
|
||||
)
|
||||
.wrap_err("setup sqlite table")?;
|
||||
|
||||
info!("Finished db setup");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_bisection(conn: &Connection) -> color_eyre::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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")
|
||||
.wrap_err("preparing select")?;
|
||||
|
||||
let iter = select
|
||||
.query_map([], |row| {
|
||||
Ok(Bisection {
|
||||
id: row.get(0)?,
|
||||
code: row.get(1)?,
|
||||
status: match row.get(2)? {
|
||||
0 => BisectStatus::InProgress,
|
||||
1 => BisectStatus::Error(row.get(3)?),
|
||||
2 => BisectStatus::Success(row.get(3)?),
|
||||
_ => return Err(rusqlite::Error::InvalidQuery), // actually not lol
|
||||
},
|
||||
})
|
||||
})
|
||||
.wrap_err("getting bisections from db query")?;
|
||||
|
||||
iter.collect::<Result<Vec<_>, rusqlite::Error>>()
|
||||
.wrap_err("getting bisections from db")
|
||||
}
|
||||
107
src/main.rs
Normal file
107
src/main.rs
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
#![feature(exit_status_error)]
|
||||
|
||||
mod bisect;
|
||||
mod db;
|
||||
|
||||
use std::sync::{mpsc, Arc, Mutex};
|
||||
|
||||
use axum::{
|
||||
extract::Query,
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse},
|
||||
routing::{get, post, Router},
|
||||
Extension, Json,
|
||||
};
|
||||
use bisect::Job;
|
||||
use color_eyre::eyre::Context;
|
||||
use rusqlite::Connection;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
use tracing::{error, info};
|
||||
use uuid::Uuid;
|
||||
|
||||
type SendChannel = Arc<Mutex<mpsc::Sender<Job>>>;
|
||||
type Conn = Arc<Mutex<Connection>>;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> color_eyre::Result<()> {
|
||||
color_eyre::install()?;
|
||||
|
||||
tracing_subscriber::fmt().init();
|
||||
|
||||
let (job_queue_send, job_queue_recv) = mpsc::channel();
|
||||
|
||||
let sqlite_db = env::var("SQLITE_DB").unwrap_or_else(|_| "bisect.sqlite".to_string());
|
||||
|
||||
let main_conn = Connection::open(&sqlite_db)
|
||||
.wrap_err_with(|| format!("connect to sqlite with file path: {}", sqlite_db))?;
|
||||
let main_conn = Arc::new(Mutex::new(main_conn));
|
||||
|
||||
let worker_conn = Connection::open(&sqlite_db)
|
||||
.wrap_err_with(|| format!("connect to sqlite with file path: {}", sqlite_db))?;
|
||||
|
||||
db::setup(&worker_conn).wrap_err("db setup")?;
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(|| async { index_html() }))
|
||||
// .route("/bisect/:id", get(get_bisection))
|
||||
.route("/bisect", get(get_bisections))
|
||||
.route("/bisect", post(do_bisection))
|
||||
// this is really stupid and hacky
|
||||
.layer(Extension(Arc::new(Mutex::new(job_queue_send))))
|
||||
.layer(Extension(main_conn));
|
||||
|
||||
std::thread::spawn(|| bisect::bisect_worker(job_queue_recv, worker_conn));
|
||||
|
||||
info!("Starting up server");
|
||||
|
||||
axum::Server::bind(&"0.0.0.0:4000".parse().unwrap())
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
.wrap_err("failed to start server")
|
||||
}
|
||||
|
||||
fn index_html() -> impl IntoResponse {
|
||||
Html(include_str!("../index.html"))
|
||||
}
|
||||
|
||||
async fn get_bisections(Extension(conn): Extension<Conn>) -> impl IntoResponse {
|
||||
db::get_bisections(&conn.lock().unwrap())
|
||||
.map(|bisections| Json(bisections))
|
||||
.map_err(|err| {
|
||||
error!(?err, "error getting bisections");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Options {
|
||||
start: Option<chrono::NaiveDate>,
|
||||
end: Option<chrono::NaiveDate>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
struct JobIdReturn {
|
||||
job_id: Uuid,
|
||||
}
|
||||
|
||||
async fn do_bisection(
|
||||
options: Query<Options>,
|
||||
body: String,
|
||||
send_channel: Extension<SendChannel>,
|
||||
) -> impl IntoResponse {
|
||||
let job_id = Uuid::new_v4();
|
||||
|
||||
let job = Job::new(job_id, body, options.0);
|
||||
|
||||
match send_channel.0.lock().unwrap().send(job) {
|
||||
Ok(_) => {
|
||||
info!(id = %job_id, "Added new job to queue");
|
||||
Ok(Json(JobIdReturn { job_id }))
|
||||
}
|
||||
Err(_) => Err((
|
||||
StatusCode::TOO_MANY_REQUESTS,
|
||||
"Too many jobs in the queue already",
|
||||
)),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue