-
Notifications
You must be signed in to change notification settings - Fork 142
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
WaitFor command exit with 0 #702
Comments
Hi @tisonkun 👋 Could you elaborate on this, please? In general, both WaitFor(starting from 0.20.0) and CmdWaitFor supports waiting for exit code. Also, But I'm not sure I got the request correctly |
This seems the way I'd like to go. If |
Yes, these commands are executed before returning But there is no retries of the command itself. We retry only the check of the exit status (because it might be a long-running command). Workaround could be to include retry logic in the command itself (loop for example) But sounds like a candidate for a separate config option 🤔 |
@DDtKey I try:
But it panics with |
Yes, as I mentioned above - there is no retry of the command itself for now. It seems to be a candidate for a separate feature(?). But as a workaround you may consider to use Something like: until pg_isready -U USERNAME -d postgres; do sleep 1; done |
Btw, speaking of https://github.com/testcontainers/testcontainers-rs-modules-community/blob/66bbad597d4bbed30ef210e6a0afdb64089a3bb7/src/postgres/mod.rs#L86 (based on this issue) These ones are widely used across different implementations of testcontainers (Java, Go, etc) I'd like to know if there are any issues, because it may improve the existing community module. |
Here's my workaround to wait until a command finishes and printing errors when the exit code is not 0: let mut result = self
.container
.exec(ExecCommand::new(cmd))
.await
.expect("Failed to execute command in container");
let mut timeout = Duration::from_secs(5);
let mut exit_code = result.exit_code().await?;
loop {
if exit_code.is_some() {
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
timeout -= Duration::from_millis(100);
if timeout.as_millis() <= 0 {
return Err("Timeout while waiting command to finish".into());
}
exit_code = result.exit_code().await?;
}
match exit_code {
Some(0) => {}
_ => {
let code = result.exit_code().await?.unwrap_or(-1);
let mut buffer = String::new();
let mut stderr = result.stderr();
stderr.read_to_string(&mut buffer).await?;
buffer.split('\n').for_each(|line| {
if !line.is_empty() {
eprintln!("stderr: {}", line);
}
});
return Err(format!("Failed to execute command (code = {})", code).into());
}
}; Might be useful for you case as well. |
I think ability to retry command should be incorporated into testcontainers. See no issues with that. |
I came looking for this for Postgres as well. The message on stdout can do for me, but it would be nice to be able to use For instance, testcontainers Node has: .withWaitStrategy(Wait.forSuccessfulCommand(`pg_isready -d ${POSTGRES_DB}`)) https://node.testcontainers.org/features/wait-strategies/#shell-command |
Some image, like Postgres, would have a best match wait strategy like
pg_isready
returns 0.Maybe we can implement such a wait strategy for testing with command?
The text was updated successfully, but these errors were encountered: