Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 11, 2024
1 parent 0a501fc commit 775067a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/cli/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/supervisor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
pub async fn kill_or_stop(existing_pid: u32, force: bool) -> Result<bool> {
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`");
Expand Down
2 changes: 1 addition & 1 deletion src/cli/supervisor/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/supervisor/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/supervisor/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/procs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::Result;
use miette::IntoDiagnostic;
use once_cell::sync::Lazy;
use std::sync::Mutex;
use sysinfo::ProcessesToUpdate;
Expand Down Expand Up @@ -25,15 +27,22 @@ impl Procs {
.is_some()
}

pub fn kill(&self, pid: u32) -> bool {
pub async fn kill_async(&self, pid: u32) -> Result<bool> {
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()
.unwrap()
.process(sysinfo::Pid::from_u32(pid))
{
process.kill();
process.wait();
process.wait(); // TODO: make this async
true
} else {
false
Expand Down
2 changes: 1 addition & 1 deletion src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 775067a

Please sign in to comment.