check if server correctly start

in same time than the port was opened
This commit is contained in:
Gwen Lg 2024-06-13 21:55:02 +02:00
parent b09034d6fb
commit 959eab61b0

View File

@ -48,7 +48,7 @@ impl EmbeddedServer {
log::info!("Will execute file {}", server_file);
// Start server
let child = std::process::Command::new("/usr/bin/python3")
let mut child = std::process::Command::new("/usr/bin/python3")
.arg(server_file)
.arg("-p")
.arg(port.to_string())
@ -56,7 +56,7 @@ impl EmbeddedServer {
.stderr(Stdio::null())
.spawn()?;
wait_for_port(port)?;
wait_for_port(&mut child, port)?;
Ok(Self {
_srv_dir: dest,
@ -79,6 +79,7 @@ impl Drop for EmbeddedServer {
mod utils {
use std::io::ErrorKind;
use std::process::Child;
use std::time::Duration;
/// Get a free port
@ -96,8 +97,9 @@ mod utils {
Ok(port)
}
pub fn wait_for_port(port: u16) -> std::io::Result<()> {
pub fn wait_for_port(child: &mut Child, port: u16) -> std::io::Result<()> {
for _ in 0..50 {
check_server(child)?;
if port_scanner::scan_port(port) {
return Ok(());
}
@ -109,4 +111,18 @@ mod utils {
format!("Port {} did not open in time!", port),
))?
}
fn check_server(child: &mut Child) -> Result<(), std::io::Error> {
match child.try_wait() {
Ok(None) => Ok(()), // Continue
Ok(Some(status)) => Err(std::io::Error::new(
ErrorKind::Other,
format!("grammalecte-server exit with {status}"),
)),
Err(err) => Err(std::io::Error::new(
ErrorKind::Other,
format!("Error append during check grammalecte-server status {err}"),
)),
}
}
}