Skip to content

Commit 67ffd61

Browse files
committed
Correct error handling for the psql call.
1 parent 2de4cb0 commit 67ffd61

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/tasks/dump_db.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,29 @@ fn run_psql(
149149
use std::process::{Command, Stdio};
150150

151151
let psql_script = config.gen_psql_script();
152-
// TODO Redirect stdout and stderr to avoid polluting the worker logs.
153152
let mut psql = Command::new("psql")
154153
.arg(database_url)
155154
.current_dir(export_dir)
156155
.stdin(Stdio::piped())
156+
.stdout(Stdio::null())
157+
.stderr(Stdio::piped())
157158
.spawn()?;
158159
let mut stdin = psql.stdin.take().unwrap();
159-
stdin.write_all(psql_script.as_bytes())?;
160-
drop(stdin);
161-
psql.wait()?;
160+
let input_thread = std::thread::spawn(move || -> std::io::Result<()> {
161+
stdin.write_all(psql_script.as_bytes())?;
162+
Ok(())
163+
});
164+
let output = psql.wait_with_output()?;
165+
input_thread.join().unwrap()?;
166+
if !output.stderr.is_empty() {
167+
Err(format!(
168+
"Error while executing psql: {}",
169+
String::from_utf8_lossy(&output.stderr)
170+
))?;
171+
}
172+
if !output.status.success() {
173+
Err("psql did not finish successfully.")?;
174+
}
162175
Ok(())
163176
}
164177

0 commit comments

Comments
 (0)