From 85878f570704a19d986e64302f8f104f36e10583 Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Tue, 1 Jul 2025 20:10:25 +0200 Subject: [PATCH] change the FileStore::is_open() to try to connect to the sock to tetermine if a FileStore is open instead of a PID file. because when deploying with docker a process with the same PID can be running (and often is the mize process itself) --- src/platform/os/cli.rs | 14 ++++---------- src/platform/os/fsstore.rs | 28 ++++++++++++++++++++++++---- src/platform/os/main.rs | 2 ++ src/platform/os/mod.rs | 4 ++-- 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/platform/os/cli.rs b/src/platform/os/cli.rs index a571f77..e9b9ba9 100644 --- a/src/platform/os/cli.rs +++ b/src/platform/os/cli.rs @@ -1,3 +1,5 @@ +use core::result::Result::Err; +use core::unimplemented; use std::ffi::OsString; use std::path::PathBuf; use std::str::FromStr; @@ -63,16 +65,8 @@ pub fn create(sub_matches: &ArgMatches) -> MizeResult<()> { pub fn is_running(sub_matches: &ArgMatches) -> MizeResult<()> { - let home_dir = env!("HOME"); - - if FileStore::store_is_opened(home_dir.to_owned() + "/.mize")? { - println!("true"); - } else { - println!("false"); - } - - Ok(()) - + println!("not implemented"); + Err(mize_err!("not implemented")) } diff --git a/src/platform/os/fsstore.rs b/src/platform/os/fsstore.rs index 4bf4313..3b7c4d0 100644 --- a/src/platform/os/fsstore.rs +++ b/src/platform/os/fsstore.rs @@ -2,8 +2,10 @@ use std::fs; use std::path::{Path, PathBuf}; use std::fs::OpenOptions; use sysinfo::{System, Pid, ProcessRefreshKind, RefreshKind}; +use tracing::field::debug; use std::io::Write; use ciborium::Value as CborValue; +use tracing::debug; use crate::instance::store::IdIter; use crate::item::IntoItemData; @@ -13,7 +15,8 @@ use crate::error::{IntoMizeResult, MizeResult, MizeError}; use crate::core::item::{ItemData, Item}; use crate::core::id::MizeId; use crate::item::get_raw_from_cbor; -use crate::instance::Instance; +use crate::instance::{self, Instance}; +use tokio::net::UnixStream; #[derive(Clone, Debug)] pub struct FileStore { @@ -29,6 +32,7 @@ impl FileStore { fs::create_dir_all(Path::new(&path).join("store"))?; // check for valid pid file + /* if let Some(pid) = valid_pid_file(path)? { // store already opened return Err(mize_err!("MizeStore at path {} is already opened by process with pid {}", path.display(), pid)); @@ -40,6 +44,7 @@ impl FileStore { let mut file = OpenOptions::new().write(true).create(true).open(path.join("pid"))?; write!(file, "{}", pid)?; } + */ // init the store if !path.join("next_id").exists() { @@ -49,9 +54,24 @@ impl FileStore { Ok(FileStore { path: Path::new(&path).to_owned() }) } - pub fn store_is_opened(store_path: String) -> MizeResult { - let res = valid_pid_file(Path::new(&store_path))?.is_some(); - Ok(res) + pub fn store_is_opened(store_path: String, instance: &mut Instance) -> MizeResult { + + // this was the old method.... + //let valid_pid_file = valid_pid_file(Path::new(&store_path))?.is_some(); + + let can_connect_to_sock = match instance.spawn_async_blocking("con_test_thread", try_connect_to_sock(store_path.clone())) { + Ok(_) => true, + Err(_) => false, + }; + debug!("MiZe Store at {} opened: {}", store_path, can_connect_to_sock); + Ok(can_connect_to_sock) + } +} + +async fn try_connect_to_sock(store_path: String) -> MizeResult<()> { + match UnixStream::connect(PathBuf::from(store_path).join("sock")).await { + Err(_) => Err(mize_err!("")), + Ok(_) => Ok(()), } } diff --git a/src/platform/os/main.rs b/src/platform/os/main.rs index 49113b7..35faba0 100644 --- a/src/platform/os/main.rs +++ b/src/platform/os/main.rs @@ -3,6 +3,7 @@ use std::ffi::OsString; use std::path::PathBuf; +use std::process::exit; use clap::builder::OsStr; use clap::{ArgAction, ArgMatches}; use clap::{Arg, crate_version, Command}; @@ -80,6 +81,7 @@ fn main() { if let Err(err) = result { err.log(); + exit(1); } } diff --git a/src/platform/os/mod.rs b/src/platform/os/mod.rs index 1826807..8dce92a 100644 --- a/src/platform/os/mod.rs +++ b/src/platform/os/mod.rs @@ -100,7 +100,7 @@ pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> { }, }; - if FileStore::store_is_opened(store_path.to_owned())? { + if FileStore::store_is_opened(store_path.to_owned(), instance)? { // if the store is already opened, connect to the instance, that opened it and join // it's namespace @@ -114,7 +114,7 @@ pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> { #[cfg(target_family = "windows")] { - error!("CONNECTING... would connect, but that is not imon't identify as anything, to not cause anyone problems... So that everybody can be satisfied...plemented on windows yet"); + error!("CONNECTING... would connect, but that is not implemented on windows yet"); } } else {