From 407d2f8c167b6ba135608fcee753281d5fadf209 Mon Sep 17 00:00:00 2001 From: Thomas Marchand Date: Sat, 17 Jan 2026 13:28:37 +0000 Subject: [PATCH] Auto-start build for template-based container workspaces When creating a chroot workspace with a template specified, automatically trigger the build instead of leaving it in "pending" status. This improves UX by not requiring a separate POST to /api/workspaces/:id/build. The build runs asynchronously in the background, same as the manual build endpoint. --- src/api/workspaces.rs | 53 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/src/api/workspaces.rs b/src/api/workspaces.rs index 14f65ea..5749163 100644 --- a/src/api/workspaces.rs +++ b/src/api/workspaces.rs @@ -326,7 +326,7 @@ async fn create_workspace( None => None, }; - let workspace = match workspace_type { + let mut workspace = match workspace_type { WorkspaceType::Host => Workspace { id: Uuid::new_v4(), name: req.name, @@ -388,9 +388,56 @@ async fn create_workspace( } drop(library_guard); - let response: WorkspaceResponse = workspace.into(); + // Auto-start build for template-based chroot workspaces + // This improves UX by not requiring a separate build API call + if workspace.workspace_type == WorkspaceType::Chroot && req.template.is_some() { + let distro = workspace + .distro + .as_ref() + .map(|d| parse_distro(d)) + .transpose() + .ok() + .flatten(); - tracing::info!("Created workspace: {} ({})", response.name, id); + // Set status to Building + workspace.status = WorkspaceStatus::Building; + state.workspaces.update(workspace.clone()).await; + + // Spawn build task + let workspaces_store = Arc::clone(&state.workspaces); + let working_dir = state.config.working_dir.clone(); + let mut workspace_for_build = workspace.clone(); + + tokio::spawn(async move { + let result = crate::workspace::build_chroot_workspace( + &mut workspace_for_build, + distro, + false, // don't force rebuild + &working_dir, + ) + .await; + + if let Err(e) = result { + tracing::error!( + workspace = %workspace_for_build.name, + error = %e, + "Failed to auto-build container workspace" + ); + } + + workspaces_store.update(workspace_for_build).await; + }); + + tracing::info!( + "Created and auto-building workspace: {} ({})", + workspace.name, + id + ); + } else { + tracing::info!("Created workspace: {} ({})", workspace.name, id); + } + + let response: WorkspaceResponse = workspace.into(); Ok(Json(response)) }