Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(builder): improve the nix build capturing of stdout/stderr #1268

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ tracing-subscriber = { version = "0.3.16", default-features = false, features =
"registry",
] }
ttl_cache = "0.5.1"
os_pipe = { version = "1.1.4" }
utoipa = { version = "3.2.1", features = [ "uuid", "chrono" ] }
utoipa-swagger-ui = { version = "3.1.3", features = ["axum"] }
ulid = "1.0.0"
Expand Down
1 change: 1 addition & 0 deletions builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ toml = { workspace = true }
tonic = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["default"] }
os_pipe = { workspace = true }
ulid = { workspace = true }

[dependencies.shuttle-common]
Expand Down
62 changes: 24 additions & 38 deletions builder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{
collections::BTreeMap,
fs::{self, remove_file},
io::Read,
io::{BufRead, BufReader, Read},
path::{Path, PathBuf},
process::Stdio,
};

use async_trait::async_trait;
Expand All @@ -16,10 +15,7 @@ use shuttle_proto::builder::{
use tar::Archive;
use tempfile::tempdir;
use thiserror::Error;
use tokio::{
io::{AsyncBufReadExt, BufReader},
process::Command,
};
use tokio::process::Command;
use tonic::{Request, Response, Status};
use tracing::{debug, error, info, instrument};

Expand Down Expand Up @@ -62,8 +58,11 @@ impl Service {
build_flake_file(path)?;
info!(deployment_id, "created the project flake file successfully");

let (reader, writer) = os_pipe::pipe()?;
let writer_clone = writer.try_clone()?;
let output_path = path.join("_archive");
let mut child = Command::new("nix")
let mut command = Command::new("nix");
command
.args([
"build",
"--no-write-lock-file",
Expand All @@ -74,37 +73,24 @@ impl Service {
output_path.to_str().unwrap(),
path.to_str().unwrap(),
])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;

let stdout = child.stdout.take().expect("to get handle on stderr");
let stderr = child.stderr.take().expect("to get handle on stderr");
let mut stderr_reader = BufReader::new(stderr).lines();
let mut stdout_reader = BufReader::new(stdout).lines();
let mut stdout_ended = false;
let mut stderr_ended = false;
while !(stdout_ended && stderr_ended) {
tokio::select! {
stderr_line = stderr_reader.next_line() => {
match stderr_line {
Ok(Some(line)) => info!(deployment_id, "{line}"),
Ok(None) => stderr_ended = true,
Err(err) => {
error!(deployment_id, "unexpected stderr stream close: {}", err);
stderr_ended = true;
}
}
},
stdout_line = stdout_reader.next_line() => {
match stdout_line {
Ok(Some(line)) => info!(deployment_id, "{line}"),
Ok(None) => stdout_ended = true,
Err(err) => {
error!(deployment_id, "unexpected stdout stream close: {}", err);
stdout_ended = true;
}
}
.stdout(writer)
.stderr(writer_clone);
let mut child = command.spawn()?;

// Avoid a deadlock.
drop(command);
iulianbarbu marked this conversation as resolved.
Show resolved Hide resolved

let reader = BufReader::new(reader);
for line in reader.lines() {
match line {
Ok(line) => {
info!(deployment_id, "{line}")
}
Err(err) => {
error!(
deployment_id,
"unexpected stdout/stderr stream close: {}", err
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ anyhow = { workspace = true }
async-trait = { workspace = true }
cargo_metadata = { workspace = true, optional = true }
crossbeam-channel = { workspace = true, optional = true }
os_pipe = { version = "1.1.4", optional = true }
os_pipe = { workspace = true, optional = true }
serde = { workspace = true, features = ["derive"] }
strfmt = { workspace = true }
thiserror = { workspace = true }
Expand Down