diff --git a/codchi/src/ctrl/ipc.rs b/codchi/src/ctrl/ipc.rs index 015af33a..bf2ba500 100644 --- a/codchi/src/ctrl/ipc.rs +++ b/codchi/src/ctrl/ipc.rs @@ -1,6 +1,7 @@ use super::ControllerServiceClient; use super::RUNTIME; use crate::consts::Dir; +use anyhow::bail; use anyhow::{anyhow, Result}; use futures::Future; use parity_tokio_ipc::Endpoint; @@ -19,7 +20,7 @@ pub fn get_socket_path() -> Result { } else if cfg!(target_family = "windows") { Ok(r"\\.\pipe\codchi.sock".to_string()) } else { - Err(anyhow!("Unsupported OS.")) + bail!("Unsupported OS.") } } diff --git a/codchi/src/ctrl/mod.rs b/codchi/src/ctrl/mod.rs index f02c3703..f9933fc0 100644 --- a/codchi/src/ctrl/mod.rs +++ b/codchi/src/ctrl/mod.rs @@ -171,9 +171,15 @@ mod utils { #[cfg(target_family = "windows")] pub fn daemonize() -> Result { use crate::cli::*; + use crate::ctrl::ipc; + use crate::util::UtilExt; + use anyhow::bail; + use spinoff::*; use std::env; use std::os::windows::process::CommandExt; - use std::process::{exit, Command}; + use std::process::Command; + use std::time::Duration; + use tokio::runtime::Runtime; use windows::Win32::System::Threading::*; let dir = Dir::Data.get_or_create()?; @@ -190,6 +196,23 @@ mod utils { .creation_flags(CREATE_NEW_PROCESS_GROUP.0 | CREATE_NO_WINDOW.0) .spawn()?; + let mut spinner = Spinner::new_with_stream( + spinners::Dots, + "Starting controller in background...", + Color::Blue, + spinoff::Streams::Stderr, + ); + Runtime::new()? + .block_on(async { + for _ in 0..15 { + if let Some(_) = ipc::connect_client_async().await? { + return Ok(()); + } + tokio::time::sleep(Duration::from_millis(100)).await; + } + bail!("Failed to start controller within 2 second."); + }) + .finally(|| spinner.clear())?; // We're always the parent here Ok(false) } diff --git a/codchi/src/platform/windows/mod.rs b/codchi/src/platform/windows/mod.rs index 384ab587..f8b8504f 100644 --- a/codchi/src/platform/windows/mod.rs +++ b/codchi/src/platform/windows/mod.rs @@ -22,7 +22,7 @@ impl Driver for DriverImpl { let names = wslapi::registry::distribution_names(); println!("{:#?}", names.collect::()); - todo!() + Ok(()) } fn get_controller_fs(&self) -> anyhow::Result {