From 775067a8517af81302c1364b1e5ef91cfd656455 Mon Sep 17 00:00:00 2001 From: jdx <216188+jdx@users.noreply.github.com> Date: Wed, 11 Dec 2024 17:33:27 -0600 Subject: [PATCH] wip --- src/cli/stop.rs | 2 +- src/cli/supervisor/mod.rs | 4 ++-- src/cli/supervisor/run.rs | 2 +- src/cli/supervisor/start.rs | 2 +- src/cli/supervisor/stop.rs | 2 +- src/procs.rs | 13 +++++++++++-- src/supervisor.rs | 2 +- 7 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/cli/stop.rs b/src/cli/stop.rs index 84e24c6..62a173f 100644 --- a/src/cli/stop.rs +++ b/src/cli/stop.rs @@ -16,7 +16,7 @@ impl Stop { if let Some(d) = pid_file.daemons.get(&self.id) { if let Some(pid) = d.pid { info!("stopping {} with pid {}", self.id, pid); - kill_or_stop(pid, true)?; + kill_or_stop(pid, true).await?; return Ok(()); } } diff --git a/src/cli/supervisor/mod.rs b/src/cli/supervisor/mod.rs index 0575d12..e75424c 100644 --- a/src/cli/supervisor/mod.rs +++ b/src/cli/supervisor/mod.rs @@ -37,11 +37,11 @@ impl Supervisor { /// if --force is passed, will kill existing process /// Returns false if existing pid is running and --force was not passed (so we should cancel starting the daemon) -pub fn kill_or_stop(existing_pid: u32, force: bool) -> Result { +pub async fn kill_or_stop(existing_pid: u32, force: bool) -> Result { if PROCS.is_running(existing_pid) { if force { debug!("killing pid {existing_pid}"); - PROCS.kill(existing_pid); + PROCS.kill_async(existing_pid).await?; Ok(true) } else { warn!("pitchfork supervisor is already running with pid {existing_pid}. Kill it with `--force`"); diff --git a/src/cli/supervisor/run.rs b/src/cli/supervisor/run.rs index fe12712..0f6624d 100644 --- a/src/cli/supervisor/run.rs +++ b/src/cli/supervisor/run.rs @@ -17,7 +17,7 @@ impl Run { let pid_file = StateFile::read(&*env::PITCHFORK_STATE_FILE)?; if let Some(d) = pid_file.daemons.get("pitchfork") { if let Some(pid) = d.pid { - if !(kill_or_stop(pid, self.force)?) { + if !(kill_or_stop(pid, self.force).await?) { return Ok(()); } } diff --git a/src/cli/supervisor/start.rs b/src/cli/supervisor/start.rs index 99711e5..12a90b3 100644 --- a/src/cli/supervisor/start.rs +++ b/src/cli/supervisor/start.rs @@ -18,7 +18,7 @@ impl Start { let mut running = false; if let Some(d) = sf.daemons.get("pitchfork") { if let Some(pid) = d.pid { - if !(kill_or_stop(pid, self.force)?) { + if !(kill_or_stop(pid, self.force).await?) { running = true; } } diff --git a/src/cli/supervisor/stop.rs b/src/cli/supervisor/stop.rs index 8843dc0..a5b53c4 100644 --- a/src/cli/supervisor/stop.rs +++ b/src/cli/supervisor/stop.rs @@ -13,7 +13,7 @@ impl Stop { if let Some(d) = pid_file.daemons.get("pitchfork") { if let Some(pid) = d.pid { info!("Stopping pitchfork daemon with pid {}", pid); - if !(kill_or_stop(pid, false)?) { + if !(kill_or_stop(pid, false).await?) { return Ok(()); } } diff --git a/src/procs.rs b/src/procs.rs index e9b9b31..35ee969 100644 --- a/src/procs.rs +++ b/src/procs.rs @@ -1,3 +1,5 @@ +use crate::Result; +use miette::IntoDiagnostic; use once_cell::sync::Lazy; use std::sync::Mutex; use sysinfo::ProcessesToUpdate; @@ -25,7 +27,14 @@ impl Procs { .is_some() } - pub fn kill(&self, pid: u32) -> bool { + pub async fn kill_async(&self, pid: u32) -> Result { + let result = tokio::task::spawn_blocking(move || PROCS.kill(pid)) + .await + .into_diagnostic()?; + Ok(result) + } + + fn kill(&self, pid: u32) -> bool { if let Some(process) = self .system .lock() @@ -33,7 +42,7 @@ impl Procs { .process(sysinfo::Pid::from_u32(pid)) { process.kill(); - process.wait(); + process.wait(); // TODO: make this async true } else { false diff --git a/src/supervisor.rs b/src/supervisor.rs index 98854cb..398cc42 100644 --- a/src/supervisor.rs +++ b/src/supervisor.rs @@ -280,7 +280,7 @@ impl Supervisor { if let Some(pid) = daemon.pid { PROCS.refresh_processes(); if PROCS.is_running(pid) { - PROCS.kill(pid); + PROCS.kill_async(pid).await?; } self.active_pids.remove(&pid); self.state_file