Skip to content

Commit bf2acc0

Browse files
committed
wait the new process
1 parent ef1dd37 commit bf2acc0

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "wait-service"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2021"
55
repository = "https://github.com/magiclen/wait-service"
66
homepage = "https://magiclen.org/wait-service"

src/main.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern crate tokio;
77

88
use std::error::Error;
99
use std::path::Path;
10-
use std::process::Command;
10+
use std::process::{self, Command};
1111
use std::time::Duration;
1212

1313
#[cfg(unix)]
@@ -34,9 +34,9 @@ fn exec(sources: Vec<&str>) -> Result<(), Box<dyn Error>> {
3434

3535
command.args(iter);
3636

37-
command.spawn()?;
37+
let exit_status = command.spawn()?.wait()?;
3838

39-
Ok(())
39+
process::exit(exit_status.code().unwrap_or(-1));
4040
}
4141

4242
async fn tcp(
@@ -45,23 +45,25 @@ async fn tcp(
4545
timeout: Duration,
4646
command: Vec<&str>,
4747
) -> Result<(), Box<dyn Error>> {
48-
let host_with_port = format!("{}:{}", host, port);
48+
{
49+
let host_with_port = format!("{}:{}", host, port);
4950

50-
let start = Instant::now();
51+
let start = Instant::now();
5152

52-
if timeout.is_zero() {
53-
while TcpStream::connect(host_with_port.as_str()).await.is_err() {
54-
sleep(SLEEP_INTERVAL).await;
55-
}
56-
} else {
57-
while let Err(err) =
58-
time::timeout(timeout, TcpStream::connect(host_with_port.as_str())).await?
59-
{
60-
if Instant::now() - start > timeout {
61-
return Err(err.into());
62-
} else {
53+
if timeout.is_zero() {
54+
while TcpStream::connect(host_with_port.as_str()).await.is_err() {
6355
sleep(SLEEP_INTERVAL).await;
6456
}
57+
} else {
58+
while let Err(err) =
59+
time::timeout(timeout, TcpStream::connect(host_with_port.as_str())).await?
60+
{
61+
if Instant::now() - start > timeout {
62+
return Err(err.into());
63+
} else {
64+
sleep(SLEEP_INTERVAL).await;
65+
}
66+
}
6567
}
6668
}
6769

@@ -72,19 +74,21 @@ async fn tcp(
7274

7375
#[cfg(unix)]
7476
async fn uds(path: &Path, timeout: Duration, command: Vec<&str>) -> Result<(), Box<dyn Error>> {
75-
let start = Instant::now();
77+
{
78+
let start = Instant::now();
7679

77-
if timeout.is_zero() {
78-
while UnixStream::connect(path).await.is_err() {
79-
sleep(SLEEP_INTERVAL).await;
80-
}
81-
} else {
82-
while let Err(err) = time::timeout(timeout, UnixStream::connect(path)).await? {
83-
if Instant::now() - start > timeout {
84-
return Err(err.into());
85-
} else {
80+
if timeout.is_zero() {
81+
while UnixStream::connect(path).await.is_err() {
8682
sleep(SLEEP_INTERVAL).await;
8783
}
84+
} else {
85+
while let Err(err) = time::timeout(timeout, UnixStream::connect(path)).await? {
86+
if Instant::now() - start > timeout {
87+
return Err(err.into());
88+
} else {
89+
sleep(SLEEP_INTERVAL).await;
90+
}
91+
}
8892
}
8993
}
9094

0 commit comments

Comments
 (0)