Record error when setting up repo fails rather than abort entire script.

This commit is contained in:
Tom Alexander
2026-08-08 23:50:23 -04:00
parent 6386febdb8
commit 0a270dcbdd
5 changed files with 85 additions and 44 deletions

View File

@@ -1,4 +1,7 @@
use std::process::ExitStatus;
use sqlx::Row;
use tracing::error;
use tracing::info;
use crate::Result;
@@ -37,20 +40,46 @@ pub(crate) async fn run_build(args: BuildArgs) -> Result<()> {
verify_nix_store().await?;
for target_name in args.target {
let target_config = {
let target_config = config.get_target_config(&target_name)?;
if let Some(conf) = target_config {
conf
} else {
return Err(format!("Could not find target {}", target_name).into());
// Record start of build
let build_id: i64 = sqlx::query(
r#"INSERT INTO build (start_time, target) SELECT unixepoch('now'), ? RETURNING id"#,
)
.bind(&target_name)
.fetch_one(&db_handle.conn)
.await?
.try_get("id")?;
// Put the rest into a function
let result = full_build_target(&config, &db_handle, &target_name, build_id).await;
match result {
Ok(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(&db_handle.conn)
.await?
.rows_affected();
assert!(update == 1);
}
Err(e) => {
error!("Error building target {}: {}", target_name, e);
let update: u64 = sqlx::query(
r#"UPDATE build SET end_time=unixepoch('now'), status=? WHERE id=?"#,
)
.bind(-1)
.bind(build_id)
.execute(&db_handle.conn)
.await?
.rows_affected();
assert!(update == 1);
}
};
prepare_flake_repo(&config, target_config).await?;
if target_config.get_update() {
run_nix_update(&config, target_config).await?;
}
build_target(&db_handle, &config, target_config).await?;
}
db_handle.conn.close().await;
@@ -58,6 +87,29 @@ pub(crate) async fn run_build(args: BuildArgs) -> Result<()> {
Ok(())
}
async fn full_build_target(
config: &Config,
db_handle: &DbHandle,
target_name: &str,
build_id: i64,
) -> Result<ExitStatus> {
let target_config = {
let target_config = config.get_target_config(&target_name)?;
if let Some(conf) = target_config {
conf
} else {
return Err(format!("Could not find target {}", target_name).into());
}
};
prepare_flake_repo(&config, target_config).await?;
if target_config.get_update() {
run_nix_update(&config, target_config).await?;
}
let exit_status = build_target(&db_handle, &config, target_config, build_id).await?;
Ok(exit_status)
}
async fn prepare_flake_repo(config_root: &Config, target_config: &TargetConfig) -> Result<()> {
let repo_directory = target_config.get_repo_directory(config_root)?;
assert_directory!(
@@ -106,7 +158,8 @@ async fn build_target(
db_handle: &DbHandle,
config_root: &Config,
target_config: &TargetConfig,
) -> Result<()> {
build_id: i64,
) -> Result<ExitStatus> {
let flake_directory = target_config.get_flake_directory(config_root)?;
let build_directory = target_config.get_build_directory(config_root)?;
assert_directory!(
@@ -117,18 +170,10 @@ async fn build_target(
let target_name = target_config.get_name()?;
let build_id: i64 = sqlx::query(
r#"INSERT INTO build (start_time, target) SELECT unixepoch('now'), ? RETURNING id"#,
)
.bind(target_name)
.fetch_one(&db_handle.conn)
.await?
.try_get("id")?;
let flake_lock = parse_flake_lock(&flake_directory).await?;
write_input_revs_to_db(db_handle, &flake_lock, build_id).await?;
nixos_build_target(
let exit_status = nixos_build_target(
db_handle,
build_directory,
flake_directory,
@@ -138,7 +183,7 @@ async fn build_target(
)
.await?;
Ok(())
Ok(exit_status)
}
async fn write_input_revs_to_db(