Skip to content

Commit

Permalink
feat(registry-mock): use reverse recursion instead
Browse files Browse the repository at this point in the history
  • Loading branch information
KSXGitHub committed Nov 16, 2023
1 parent d8f01f2 commit e93e6e6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
30 changes: 13 additions & 17 deletions crates/registry-mock/src/kill_verdaccio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,27 @@ use sysinfo::{
Pid, Process, ProcessExt, ProcessRefreshKind, RefreshKind, Signal, System, SystemExt,
};

pub fn kill_verdaccio_recursive_by_process(process: &Process, signal: Signal) -> u64 {
let kill_count = process
.tasks
.values()
.map(|process| kill_verdaccio_recursive_by_process(process, signal))
.sum();
if process.name().to_lowercase().contains("verdaccio")
&& process.kill_with(signal).unwrap_or_else(|| process.kill())
{
kill_count + 1
} else {
kill_count
fn is_descent_of(process: &Process, suspect_ancestor: Pid, system: &System) -> bool {
let Some(parent) = process.parent() else { return false };
if parent == suspect_ancestor {
return true;
}
let Some(parent) = system.processes().get(&parent) else { return false };
is_descent_of(parent, suspect_ancestor, system)
}

pub fn kill_verdaccio_recursive_by_pid_in(system: &System, pid: Pid, signal: Signal) -> u64 {
pub fn kill_all_verdaccio_children_in(root: Pid, signal: Signal, system: &System) -> usize {
system
.processes()
.get(&pid)
.map_or(0, |process| kill_verdaccio_recursive_by_process(process, signal))
.values()
.filter(|process| is_descent_of(process, root, system))
.filter(|process| process.kill_with(signal).unwrap_or_else(|| process.kill()))
.count()
}

pub fn kill_verdaccio_recursive_by_pid(pid: Pid, signal: Signal) -> u64 {
pub fn kill_all_verdaccio_children(root: Pid, signal: Signal) -> usize {
let system = RefreshKind::new()
.with_processes(ProcessRefreshKind::new())
.pipe(System::new_with_specifics);
kill_verdaccio_recursive_by_pid_in(&system, pid, signal)
kill_all_verdaccio_children_in(root, signal, &system)
}
6 changes: 3 additions & 3 deletions crates/registry-mock/src/mock_instance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{kill_verdaccio::kill_verdaccio_recursive_by_pid, node_registry_mock, registry_mock};
use crate::{kill_verdaccio::kill_all_verdaccio_children, node_registry_mock, registry_mock};
use advisory_lock::{AdvisoryFileLock, FileLockError, FileLockMode};
use assert_cmd::prelude::*;
use pipe_trait::Pipe;
Expand Down Expand Up @@ -34,7 +34,7 @@ impl Drop for MockInstance {
let MockInstance { process, .. } = self;
let pid = process.id();
eprintln!("info: Terminating all verdaccio instances below {pid}...");
let kill_count = kill_verdaccio_recursive_by_pid(Pid::from_u32(pid), Signal::Interrupt);
let kill_count = kill_all_verdaccio_children(Pid::from_u32(pid), Signal::Interrupt);
eprintln!("info: Terminated {kill_count} verdaccio instances");
}

Check warning on line 39 in crates/registry-mock/src/mock_instance.rs

View check run for this annotation

Codecov / codecov/patch

crates/registry-mock/src/mock_instance.rs#L33-L39

Added lines #L33 - L39 were not covered by tests
}
Expand Down Expand Up @@ -215,7 +215,7 @@ impl Drop for RegistryAnchor {
let pid = anchor.info.pid;
eprintln!("info: There are no more users that use the mocked server");
eprintln!("info: Terminating all verdaccio instances below {pid}...");
let kill_count = kill_verdaccio_recursive_by_pid(Pid::from_u32(pid), Signal::Interrupt);
let kill_count = kill_all_verdaccio_children(Pid::from_u32(pid), Signal::Interrupt);
eprintln!("info: Terminated {kill_count} verdaccio instances");

RegistryAnchor::delete();
Expand Down

0 comments on commit e93e6e6

Please sign in to comment.