Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

fix(up): the wadm pidfile is not removed when wadm is not started #631

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions src/up/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::fmt::Write;
use std::io::ErrorKind;
use std::path::Path;
use std::path::PathBuf;
use std::process::Stdio;
Expand Down Expand Up @@ -28,7 +29,7 @@ use wash_lib::start::start_wadm;
use wash_lib::start::WadmConfig;
use wash_lib::start::{
ensure_nats_server, ensure_wasmcloud, start_nats_server, start_wasmcloud_host, wait_for_server,
NatsConfig,
NatsConfig, WADM_PID,
};
use wasmcloud_control_interface::{Client as CtlClient, ClientBuilder as CtlClientBuilder};

Expand Down Expand Up @@ -444,7 +445,10 @@ pub(crate) async fn handle_up(cmd: UpCommand, output_kind: OutputKind) -> Result
{
wasmcloud_bin
} else {
// Ensure we clean up the NATS server if we can't start wasmCloud
// Ensure we clean up the NATS server and wadm if we can't start wasmCloud
if let Some(child) = wadm_process {
stop_wadm(child, &install_dir).await?;
}
if nats_bin.is_some() {
stop_nats(install_dir).await?;
}
Expand Down Expand Up @@ -477,8 +481,8 @@ pub(crate) async fn handle_up(cmd: UpCommand, output_kind: OutputKind) -> Result
Ok(child) => child,
Err(e) => {
// Ensure we clean up the NATS server and wadm if we can't start wasmCloud
if let Some(mut child) = wadm_process {
child.kill().await?;
if let Some(child) = wadm_process {
stop_wadm(child, &install_dir).await?;
}
if !cmd.nats_opts.connect_only {
stop_nats(install_dir).await?;
Expand All @@ -489,6 +493,10 @@ pub(crate) async fn handle_up(cmd: UpCommand, output_kind: OutputKind) -> Result

let url = format!("{LOCALHOST}:{}", host_port);
if wait_for_server(&url, "Washboard").await.is_err() {
// Ensure we clean up the NATS server and wadm if we can't start wasmCloud
if let Some(child) = wadm_process {
stop_wadm(child, &install_dir).await?;
}
if nats_bin.is_some() {
stop_nats(install_dir).await?;
}
Expand All @@ -505,8 +513,10 @@ pub(crate) async fn handle_up(cmd: UpCommand, output_kind: OutputKind) -> Result
"CTRL+c received, stopping wasmCloud, wadm, and NATS...".to_string(),
);

// remove wadm pidfile, the process is stopped automatically by CTRL+c
tokio::fs::remove_file(&install_dir.join("wadm.pid")).await?;
if wadm_process.is_some() {
// remove wadm pidfile, the process is stopped automatically by CTRL+c
remove_wadm_pidfile(&install_dir).await?;
}

spinner.finish_and_clear();
}
Expand Down Expand Up @@ -652,6 +662,26 @@ async fn is_wadm_running(nats_opts: &NatsOpts, lattice_prefix: &str) -> Result<b
)
}

async fn stop_wadm<P>(mut wadm: Child, install_dir: P) -> Result<()>
where
P: AsRef<Path>,
{
wadm.kill().await?;
remove_wadm_pidfile(install_dir).await
}

async fn remove_wadm_pidfile<P>(install_dir: P) -> Result<()>
where
P: AsRef<Path>,
{
if let Err(err) = tokio::fs::remove_file(install_dir.as_ref().join(WADM_PID)).await {
if err.kind() != ErrorKind::NotFound {
return Err(anyhow!(err));
}
}
Ok(())
}

/// Scans ports from 4000 to 5000 to find an open port for the wasmCloud dashboard
///
/// # Arguments
Expand Down