Skip to content

Commit

Permalink
fix(registry-mock): use sysinfo to kill
Browse files Browse the repository at this point in the history
  • Loading branch information
KSXGitHub committed Nov 16, 2023
1 parent ce54c07 commit 8ee89ca
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 48 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ sha2 = { version = "0.10.8" }
split-first-char = { version = "0.0.0" }
ssri = { version = "9.0.0" }
strum = { version = "0.25.0", features = ["derive"] }
sysinfo = { version = "0.29.10" }
tar = { version = "0.4.40" }
text-block-macros = { version = "0.1.1" }
tracing = { version = "0.1.37" }
Expand Down
1 change: 1 addition & 0 deletions crates/registry-mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
which = { workspace = true }
sysinfo = { workspace = true }
31 changes: 31 additions & 0 deletions crates/registry-mock/src/kill_verdaccio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use pipe_trait::Pipe;
use sysinfo::{
Pid, Process, ProcessExt, ProcessRefreshKind, RefreshKind, Signal, System, SystemExt,
};

pub fn kill_verdaccio_recursive_by_process(process: &Process, signal: Signal) -> u64 {
let kill = |process: &Process| -> u64 {
if !process.name().to_lowercase().contains("verdaccio") {
kill_verdaccio_recursive_by_process(process, signal)
} else if process.kill_with(signal).unwrap_or_else(|| process.kill()) {
1

Check warning on line 11 in crates/registry-mock/src/kill_verdaccio.rs

View check run for this annotation

Codecov / codecov/patch

crates/registry-mock/src/kill_verdaccio.rs#L10-L11

Added lines #L10 - L11 were not covered by tests
} else {
0

Check warning on line 13 in crates/registry-mock/src/kill_verdaccio.rs

View check run for this annotation

Codecov / codecov/patch

crates/registry-mock/src/kill_verdaccio.rs#L13

Added line #L13 was not covered by tests
}
};
process.tasks.values().map(kill).sum()
}

pub fn kill_verdaccio_recursive_by_pid_in(system: &System, pid: Pid, signal: Signal) -> u64 {
system
.processes()
.get(&pid)
.map_or(0, |process| kill_verdaccio_recursive_by_process(process, signal))
}

pub fn kill_verdaccio_recursive_by_pid(pid: Pid, signal: Signal) -> u64 {
let system = RefreshKind::new()
.with_processes(ProcessRefreshKind::new())
.pipe(System::new_with_specifics);
kill_verdaccio_recursive_by_pid_in(&system, pid, signal)
}
1 change: 1 addition & 0 deletions crates/registry-mock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod dirs;
mod kill_verdaccio;
mod mock_instance;
mod node_registry_mock;

Expand Down
56 changes: 8 additions & 48 deletions crates/registry-mock/src/mock_instance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{node_registry_mock, registry_mock};
use crate::{kill_verdaccio::kill_verdaccio_recursive_by_pid, node_registry_mock, registry_mock};
use advisory_lock::{AdvisoryFileLock, FileLockError, FileLockMode};
use assert_cmd::prelude::*;
use pipe_trait::Pipe;
Expand All @@ -13,6 +13,7 @@ use std::{
process::{Child, Command, Stdio},
sync::OnceLock,
};
use sysinfo::{Pid, PidExt, Signal};
use tokio::{
runtime::Builder,
time::{sleep, Duration},
Expand All @@ -32,33 +33,9 @@ impl Drop for MockInstance {
fn drop(&mut self) {
let MockInstance { process, .. } = self;
let pid = process.id();

eprintln!("info: Terminating mocked registry with the kill command (kill {pid})...");
match Command::new("kill").arg(pid.to_string()).output() {
Err(error) => {
eprintln!(
"warn: Failed to terminate mocked registry with the kill command: {error}"
);
}
Ok(output) => {
if output.status.success() {
eprintln!("info: Mocked registry terminated");
return;
}

let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!(
"warn: Failed to terminate mocked registry with the kill command: {stderr}"
);
}
}

eprintln!("info: Terminating mocked registry with SIGKILL...");
if let Err(error) = process.kill() {
eprintln!("warn: Failed to terminate mocked registry with SIGKILL: {error}");
} else {
eprintln!("info: mocked registry terminated");
}
eprintln!("info: Terminating all verdaccio instances below {pid}...");
let kill_count = kill_verdaccio_recursive_by_pid(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 @@ -237,26 +214,9 @@ impl Drop for RegistryAnchor {

let pid = anchor.info.pid;
eprintln!("info: There are no more users that use the mocked server");
eprintln!("info: Terminating mocked registry with the kill command (kill {pid})...");

match Command::new("kill").arg(pid.to_string()).output() {
Err(error) => {
eprintln!(
"warn: Failed to terminate mocked registry with the kill command: {error}"
);
}
Ok(output) => {
if output.status.success() {
eprintln!("info: Mocked registry terminated");
return;
}

let stderr = String::from_utf8_lossy(&output.stderr);
eprintln!(
"warn: Failed to terminate mocked registry with the kill command: {stderr}"
);
}
}
eprintln!("info: Terminating all verdaccio instances below {pid}...");
let kill_count = kill_verdaccio_recursive_by_pid(Pid::from_u32(pid), Signal::Interrupt);
eprintln!("info: Terminated {kill_count} verdaccio instances");

RegistryAnchor::delete();
guard.unlock();
Expand Down

0 comments on commit 8ee89ca

Please sign in to comment.