From 14cf097794178b09ee2491dfa2a36e87b7bcade4 Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Thu, 13 Jun 2024 21:55:02 +0200 Subject: [PATCH 1/2] check if server correctly start in same time than the port was opened. Rename function into `wait_for_server` according. --- src/server.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/server.rs b/src/server.rs index 3ebc60a..afda94c 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,7 +1,7 @@ -use crate::server::utils::{get_free_port, wait_for_port}; +use crate::server::utils::{get_free_port, wait_for_server}; use mktemp::Temp; use std::io::{self, Cursor, Read}; -use std::process::{Child, Stdio}; +use std::process::{Child, ExitStatus, Stdio}; use thiserror::Error; use zip::{result::ZipError, ZipArchive}; @@ -13,6 +13,12 @@ pub enum Error { #[error("Get an available port failed")] GetFreePort(#[source] io::Error), + #[error("Server exit with `{status}`")] + ServerExitWithStatus { status: ExitStatus }, + + #[error("Error append during check grammalecte-server status")] + ServerCheckStatus(#[source] io::Error), + #[error("Port {port} did not open in time!")] WaitPortOpen { port: u16 }, @@ -80,7 +86,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()) @@ -89,7 +95,7 @@ impl EmbeddedServer { .spawn() .map_err(Error::StartServerProcess)?; - wait_for_port(port)?; + wait_for_server(&mut child, port)?; Ok(Self { _srv_dir: dest, @@ -111,9 +117,9 @@ impl Drop for EmbeddedServer { } mod utils { - use std::time::Duration; - use super::Error; + use std::process::Child; + use std::time::Duration; /// Get a free port pub fn get_free_port() -> u16 { @@ -130,8 +136,9 @@ mod utils { port } - pub fn wait_for_port(port: u16) -> Result<(), Error> { + pub fn wait_for_server(child: &mut Child, port: u16) -> Result<(), Error> { for _ in 0..50 { + check_server(child)?; if port_scanner::scan_port(port) { return Ok(()); } @@ -140,4 +147,11 @@ mod utils { Err(Error::WaitPortOpen { port }) } + + fn check_server(child: &mut Child) -> Result<(), Error> { + match child.try_wait().map_err(Error::ServerCheckStatus)? { + None => Ok(()), // Continue + Some(status) => Err(Error::ServerExitWithStatus { status }), + } + } } -- 2.45.2 From ed20b3c2bc44d11be4099b4c6af3558462079c87 Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Thu, 13 Jun 2024 22:16:45 +0200 Subject: [PATCH 2/2] add err output when server failed to start --- src/server.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/server.rs b/src/server.rs index afda94c..a8ad1f1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -16,6 +16,9 @@ pub enum Error { #[error("Server exit with `{status}`")] ServerExitWithStatus { status: ExitStatus }, + #[error("Server exit with `{status}` :\n{msg}")] + ServerExitWithError { status: ExitStatus, msg: String }, + #[error("Error append during check grammalecte-server status")] ServerCheckStatus(#[source] io::Error), @@ -91,7 +94,7 @@ impl EmbeddedServer { .arg("-p") .arg(port.to_string()) .stdout(Stdio::null()) - .stderr(Stdio::null()) + .stderr(Stdio::piped()) .spawn() .map_err(Error::StartServerProcess)?; @@ -118,6 +121,8 @@ impl Drop for EmbeddedServer { mod utils { use super::Error; + use std::fmt::Write; + use std::io::{BufRead, BufReader}; use std::process::Child; use std::time::Duration; @@ -151,7 +156,24 @@ mod utils { fn check_server(child: &mut Child) -> Result<(), Error> { match child.try_wait().map_err(Error::ServerCheckStatus)? { None => Ok(()), // Continue - Some(status) => Err(Error::ServerExitWithStatus { status }), + Some(status) => { + if let Some(err) = child.stderr.take() { + let mut msg = format!("grammalecte-server exit with `{status}`"); + writeln!(&mut msg, " :").unwrap(); + let err = BufReader::new(err); + err.lines().for_each(|line| match line { + Ok(line) => { + writeln!(&mut msg, "\t{}", line).unwrap(); + } + Err(err) => { + writeln!(&mut msg, "__{err:?}").unwrap(); + } + }); + Err(Error::ServerExitWithError { status, msg }) + } else { + Err(Error::ServerExitWithStatus { status }) + } + } } } } -- 2.45.2