forked from pierre/GrammalecteClient
Compare commits
3 Commits
89f3bd5d71
...
2d86172f17
Author | SHA1 | Date | |
---|---|---|---|
|
2d86172f17 | ||
|
a5cd13771b | ||
|
959eab61b0 |
@ -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 mktemp::Temp;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::io::{Cursor, Read};
|
use std::io::{self, Cursor, Read};
|
||||||
use std::process::{Child, Stdio};
|
use std::process::{Child, Stdio};
|
||||||
use zip::ZipArchive;
|
use zip::ZipArchive;
|
||||||
|
|
||||||
@ -48,15 +48,15 @@ impl EmbeddedServer {
|
|||||||
log::info!("Will execute file {}", server_file);
|
log::info!("Will execute file {}", server_file);
|
||||||
|
|
||||||
// Start server
|
// 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(server_file)
|
||||||
.arg("-p")
|
.arg("-p")
|
||||||
.arg(port.to_string())
|
.arg(port.to_string())
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::piped())
|
||||||
.spawn()?;
|
.spawn()?;
|
||||||
|
|
||||||
wait_for_port(port)?;
|
wait_for_server(&mut child, port)?;
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
_srv_dir: dest,
|
_srv_dir: dest,
|
||||||
@ -78,7 +78,9 @@ impl Drop for EmbeddedServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mod utils {
|
mod utils {
|
||||||
use std::io::ErrorKind;
|
use std::fmt::Write;
|
||||||
|
use std::io::{BufRead, BufReader, ErrorKind};
|
||||||
|
use std::process::Child;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
/// Get a free port
|
/// Get a free port
|
||||||
@ -96,8 +98,9 @@ mod utils {
|
|||||||
Ok(port)
|
Ok(port)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wait_for_port(port: u16) -> std::io::Result<()> {
|
pub fn wait_for_server(child: &mut Child, port: u16) -> std::io::Result<()> {
|
||||||
for _ in 0..50 {
|
for _ in 0..50 {
|
||||||
|
check_server(child)?;
|
||||||
if port_scanner::scan_port(port) {
|
if port_scanner::scan_port(port) {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@ -109,4 +112,32 @@ mod utils {
|
|||||||
format!("Port {} did not open in time!", port),
|
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)) => {
|
||||||
|
let mut err_msg = format!("grammalecte-server exit with `{status}`");
|
||||||
|
|
||||||
|
if let Some(err) = child.stderr.take() {
|
||||||
|
writeln!(&mut err_msg, " :").unwrap();
|
||||||
|
let err = BufReader::new(err);
|
||||||
|
err.lines().for_each(|line| match line {
|
||||||
|
Ok(line) => {
|
||||||
|
writeln!(&mut err_msg, "\t{}", line).unwrap();
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
writeln!(&mut err_msg, "__{err:?}").unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Err(std::io::Error::new(ErrorKind::Other, err_msg))
|
||||||
|
}
|
||||||
|
Err(err) => Err(std::io::Error::new(
|
||||||
|
ErrorKind::Other,
|
||||||
|
format!("Error append during check grammalecte-server status {err}"),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user