Write the build start to the database.

This commit is contained in:
Tom Alexander
2026-02-16 20:42:03 -05:00
parent ab1f86384b
commit c4c811a099
5 changed files with 40 additions and 51 deletions

48
flake.lock generated
View File

@@ -1,48 +0,0 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1771008912,
"narHash": "sha256-gf2AmWVTs8lEq7z/3ZAsgnZDhWIckkb+ZnAo5RzSxJg=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "a82ccc39b39b621151d6732718e3e250109076fa",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs",
"rust-overlay": "rust-overlay"
}
},
"rust-overlay": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1771125043,
"narHash": "sha256-ldf/s49n6rOAxl7pYLJGGS1N/assoHkCOWdEdLyNZkc=",
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "4912f951a26dc8142b176be2c2ad834319dc06e8",
"type": "github"
},
"original": {
"owner": "oxalica",
"repo": "rust-overlay",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

View File

@@ -94,6 +94,7 @@ async fn build_target(
build_directory, build_directory,
flake_directory, flake_directory,
target_config.get_attr()?, target_config.get_attr()?,
target_config.get_name()?,
) )
.await?; .await?;

View File

@@ -121,6 +121,11 @@ impl TargetConfig {
pub(crate) fn get_attr(&'_ self) -> Result<&String, CustomError> { pub(crate) fn get_attr(&'_ self) -> Result<&String, CustomError> {
Ok(&self.attr) Ok(&self.attr)
} }
/// The name to identify this target
pub(crate) fn get_name(&'_ self) -> Result<&String, CustomError> {
Ok(&self.name)
}
} }
fn hex_string(data: &[u8]) -> String { fn hex_string(data: &[u8]) -> String {

View File

@@ -10,16 +10,18 @@ use crate::database::db_handle::DbHandle;
use super::running_build::RunningBuild; use super::running_build::RunningBuild;
pub(crate) async fn nixos_build_target<B, F, A>( pub(crate) async fn nixos_build_target<B, F, A, TN>(
db_handle: &DbHandle, db_handle: &DbHandle,
build_path: B, build_path: B,
flake_path: F, flake_path: F,
attr: A, attr: A,
target_name: TN,
) -> Result<()> ) -> Result<()>
where where
B: AsRef<Path>, B: AsRef<Path>,
F: AsRef<Path>, F: AsRef<Path>,
A: AsRef<str>, A: AsRef<str>,
TN: AsRef<str>,
{ {
let reference = { let reference = {
let path = AsRef::<OsStr>::as_ref(flake_path.as_ref()); let path = AsRef::<OsStr>::as_ref(flake_path.as_ref());
@@ -55,7 +57,7 @@ where
let child = command.spawn()?; let child = command.spawn()?;
let mut running_build = RunningBuild::new(db_handle)?; let mut running_build = RunningBuild::new(db_handle)?;
running_build.run_to_completion(child).await?; running_build.run_to_completion(child, target_name).await?;
Ok(()) Ok(())
} }

View File

@@ -1,3 +1,4 @@
use sqlx::Row;
use tokio::io::AsyncBufReadExt; use tokio::io::AsyncBufReadExt;
use tokio::io::BufReader; use tokio::io::BufReader;
use tokio::io::Lines; use tokio::io::Lines;
@@ -17,7 +18,22 @@ impl<'db> RunningBuild<'db> {
Ok(RunningBuild { db_handle }) Ok(RunningBuild { db_handle })
} }
pub(crate) async fn run_to_completion(&mut self, mut child: Child) -> Result<()> { pub(crate) async fn run_to_completion<TN>(
&mut self,
mut child: Child,
target_name: TN,
) -> Result<()>
where
TN: AsRef<str>,
{
let build_id: i64 = sqlx::query(
r#"INSERT INTO build (start_time, target) SELECT unixepoch('now'), ? RETURNING id"#,
)
.bind(target_name.as_ref())
.fetch_one(&self.db_handle.conn)
.await?
.try_get("id")?;
let mut output_stream = OutputStream::from_child(&mut child)?; let mut output_stream = OutputStream::from_child(&mut child)?;
let exit_status_handle = tokio::spawn(async move { let exit_status_handle = tokio::spawn(async move {
@@ -41,6 +57,19 @@ impl<'db> RunningBuild<'db> {
let exit_status = exit_status_handle.await?; let exit_status = exit_status_handle.await?;
println!("nixos-rebuild status was: {}", exit_status); println!("nixos-rebuild status was: {}", exit_status);
let update: u64 =
sqlx::query(r#"UPDATE build SET end_time=unixepoch('now'), status=? WHERE id=?"#)
.bind(
exit_status
.code()
.expect("Process should have an exit code."),
)
.bind(build_id)
.execute(&self.db_handle.conn)
.await?
.rows_affected();
assert!(update == 1);
Ok(()) Ok(())
} }