diff --git a/linkup-cli/src/commands/health.rs b/linkup-cli/src/commands/health.rs index 9d4959a9..55071f53 100644 --- a/linkup-cli/src/commands/health.rs +++ b/linkup-cli/src/commands/health.rs @@ -10,7 +10,7 @@ use std::{ use crate::{ linkup_dir_path, - services::{self, find_service_pid, BackgroundService}, + services::{self, BackgroundService}, state::State, Result, }; @@ -106,7 +106,7 @@ impl BackgroundServices { pub fn load(state: Option<&State>) -> Self { let mut managed_pids: Vec = Vec::with_capacity(4); - let linkup_server = match find_service_pid(services::LocalServer::ID) { + let linkup_server = match services::LocalServer::find_pid() { Some(pid) => { managed_pids.push(pid); @@ -116,7 +116,7 @@ impl BackgroundServices { }; let cloudflared = if services::is_cloudflared_installed() { - match find_service_pid(services::CloudflareTunnel::ID) { + match services::CloudflareTunnel::find_pid() { Some(pid) => { managed_pids.push(pid); @@ -128,7 +128,7 @@ impl BackgroundServices { BackgroundServiceHealth::NotInstalled }; - let dns_server = match find_service_pid(services::LocalDnsServer::ID) { + let dns_server = match services::LocalDnsServer::find_pid() { Some(pid) => { managed_pids.push(pid); diff --git a/linkup-cli/src/commands/local.rs b/linkup-cli/src/commands/local.rs index 90f72b44..c92db073 100644 --- a/linkup-cli/src/commands/local.rs +++ b/linkup-cli/src/commands/local.rs @@ -2,7 +2,7 @@ use anyhow::anyhow; use colored::Colorize; use crate::{ - services::{self, find_service_pid, BackgroundService}, + services::{self, BackgroundService}, state::{upload_state, ServiceTarget, State}, Result, }; @@ -35,7 +35,7 @@ pub async fn local(args: &Args) -> Result<()> { return Ok(()); } - if find_service_pid(services::LocalServer::ID).is_none() { + if services::LocalServer::find_pid().is_none() { println!( "{}", "Seems like your local Linkup server is not running. Please run 'linkup start' first." diff --git a/linkup-cli/src/commands/remote.rs b/linkup-cli/src/commands/remote.rs index 4b1b9d01..6f60a694 100644 --- a/linkup-cli/src/commands/remote.rs +++ b/linkup-cli/src/commands/remote.rs @@ -1,5 +1,5 @@ use crate::{ - services::{self, find_service_pid, BackgroundService}, + services::{self, BackgroundService}, state::{upload_state, ServiceTarget, State}, Result, }; @@ -37,7 +37,7 @@ pub async fn remote(args: &Args) -> Result<()> { let mut state = State::load()?; - if find_service_pid(services::LocalServer::ID).is_none() { + if services::LocalServer::find_pid().is_none() { println!( "{}", "Seems like your local Linkup server is not running. Please run 'linkup start' first." diff --git a/linkup-cli/src/commands/stop.rs b/linkup-cli/src/commands/stop.rs index b44a7151..306dbf93 100644 --- a/linkup-cli/src/commands/stop.rs +++ b/linkup-cli/src/commands/stop.rs @@ -4,7 +4,7 @@ use std::path::{Path, PathBuf}; use anyhow::Context; use crate::env_files::clear_env_file; -use crate::services::{stop_service, BackgroundService}; +use crate::services::BackgroundService; use crate::state::State; use crate::{services, Result}; @@ -35,9 +35,9 @@ pub fn stop(_args: &Args, clear_env: bool) -> Result<()> { } } - stop_service(services::LocalServer::ID); - stop_service(services::CloudflareTunnel::ID); - stop_service(services::LocalDnsServer::ID); + services::LocalServer::stop(); + services::CloudflareTunnel::stop(); + services::LocalDnsServer::stop(); println!("Stopped linkup"); diff --git a/linkup-cli/src/services/cloudflare_tunnel.rs b/linkup-cli/src/services/cloudflare_tunnel.rs index 707f0a14..93bd5d1d 100644 --- a/linkup-cli/src/services/cloudflare_tunnel.rs +++ b/linkup-cli/src/services/cloudflare_tunnel.rs @@ -15,7 +15,7 @@ use url::Url; use crate::{linkup_file_path, state::State, worker_client::WorkerClient, Result}; -use super::{find_service_pid, BackgroundService, PidError}; +use super::{BackgroundService, PidError}; #[derive(thiserror::Error, Debug)] #[allow(dead_code)] @@ -170,7 +170,7 @@ impl BackgroundService for CloudflareTunnel { return Err(Error::InvalidSessionName(state.linkup.session_name.clone()).into()); } - if find_service_pid(Self::ID).is_some() { + if Self::find_pid().is_some() { self.notify_update_with_details( &status_sender, super::RunStatus::Started, diff --git a/linkup-cli/src/services/mod.rs b/linkup-cli/src/services/mod.rs index 32e175d6..374ea50a 100644 --- a/linkup-cli/src/services/mod.rs +++ b/linkup-cli/src/services/mod.rs @@ -26,16 +26,12 @@ pub enum RunStatus { Error, } -impl Display for RunStatus { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::Pending => write!(f, "pending"), - Self::Starting => write!(f, "starting"), - Self::Started => write!(f, "started"), - Self::Skipped => write!(f, "skipped"), - Self::Error => write!(f, "error"), - } - } +#[derive(Error, Debug)] +pub enum PidError { + #[error("no pid file: {0}")] + NoPidFile(String), + #[error("bad pid file: {0}")] + BadPidFile(String), } #[derive(Clone)] @@ -55,6 +51,14 @@ pub trait BackgroundService { status_sender: sync::mpsc::Sender, ) -> anyhow::Result<()>; + fn stop() { + if let Some(pid) = Self::find_pid() { + system() + .process(pid) + .map(|process| process.kill_with(Signal::Interrupt)); + } + } + fn notify_update(&self, status_sender: &sync::mpsc::Sender, status: RunStatus) { status_sender .send(RunUpdate { @@ -79,35 +83,31 @@ pub trait BackgroundService { }) .unwrap(); } -} -#[derive(Error, Debug)] -pub enum PidError { - #[error("no pid file: {0}")] - NoPidFile(String), - #[error("bad pid file: {0}")] - BadPidFile(String), -} - -pub fn find_service_pid(service_id: &str) -> Option { - for (pid, process) in system().processes() { - if process - .environ() - .iter() - .any(|item| item.to_string_lossy() == format!("LINKUP_SERVICE_ID={service_id}")) - { - return Some(*pid); + fn find_pid() -> Option { + for (pid, process) in system().processes() { + if process + .environ() + .iter() + .any(|item| item.to_string_lossy() == format!("LINKUP_SERVICE_ID={}", Self::ID)) + { + return Some(*pid); + } } - } - None + None + } } -pub fn stop_service(service_id: &str) { - if let Some(pid) = find_service_pid(service_id) { - system() - .process(pid) - .map(|process| process.kill_with(Signal::Interrupt)); +impl Display for RunStatus { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Pending => write!(f, "pending"), + Self::Starting => write!(f, "starting"), + Self::Started => write!(f, "started"), + Self::Skipped => write!(f, "skipped"), + Self::Error => write!(f, "error"), + } } }