From 900e3fc2e5f6bee29433f7d779ed23a6f9091c5b Mon Sep 17 00:00:00 2001 From: JyJyJcr <82190170+JyJyJcr@users.noreply.github.com> Date: Fri, 27 Dec 2024 20:10:13 +0900 Subject: [PATCH] fix: stop when dropped --- src/test_util.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/test_util.rs b/src/test_util.rs index 7355fdc..8985abc 100644 --- a/src/test_util.rs +++ b/src/test_util.rs @@ -86,6 +86,14 @@ pub struct Nginx { http_uwsgi_temp_path: PathBuf, http_scgi_temp_path: PathBuf, // here all path are absolute + status: Status, +} + +#[derive(PartialEq, Eq)] +enum Status { + Unknown, + Running, + Stopped, } /// nginx harness builder @@ -179,6 +187,7 @@ impl NginxBuilder { http_fastcgi_temp_path, http_uwsgi_temp_path, http_scgi_temp_path, + status: Status::Unknown, } } } @@ -202,12 +211,19 @@ impl Nginx { /// complete stop the nginx binary pub fn stop(&mut self) -> Result { + self.status = Status::Stopped; self.cmd(&["-s", "stop"]) } /// start the nginx binary pub fn start(&mut self) -> Result { - self.cmd(&[]) + let output = self.cmd(&[]); + if let Ok(output) = &output { + if output.status.success() { + self.status = Status::Running; + } + } + output } /// make sure we stop existing nginx and start new master process @@ -336,3 +352,12 @@ impl Nginx { &self.http_scgi_temp_path } } + +impl Drop for Nginx { + fn drop(&mut self) { + // exec stop if running or unknown + if self.status != Status::Stopped { + let _ = self.stop(); + } + } +}