From 7d5fd6aa8b96f5acd537201f17130ce664b74261 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Thu, 4 Nov 2021 21:07:44 +0100 Subject: [PATCH] fix(core): improve reporting of espanso start command. Relates to #839 --- espanso/src/cli/service/mod.rs | 21 +++++++++++++++++---- espanso/src/exit_code.rs | 1 + 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/espanso/src/cli/service/mod.rs b/espanso/src/cli/service/mod.rs index c80e418..c5ba8f1 100644 --- a/espanso/src/cli/service/mod.rs +++ b/espanso/src/cli/service/mod.rs @@ -17,12 +17,14 @@ * along with espanso. If not, see . */ +use std::time::Instant; + use super::{CliModule, CliModuleArgs, PathsOverrides}; use crate::{ error_eprintln, exit_code::{ SERVICE_ALREADY_RUNNING, SERVICE_FAILURE, SERVICE_NOT_REGISTERED, SERVICE_NOT_RUNNING, - SERVICE_SUCCESS, + SERVICE_SUCCESS, SERVICE_TIMED_OUT, }, info_println, lock::acquire_worker_lock, @@ -131,12 +133,23 @@ fn start_main(paths: &Paths, _paths_overrides: &PathsOverrides, args: &ArgMatche if let Err(err) = start_service() { error_eprintln!("unable to start service: {}", err); return SERVICE_FAILURE; - } else { - info_println!("espanso started correctly!"); } } - SERVICE_SUCCESS + let now = Instant::now(); + while now.elapsed() < std::time::Duration::from_secs(5) { + let lock_file = acquire_worker_lock(&paths.runtime); + if lock_file.is_none() { + info_println!("espanso started correctly!"); + return SERVICE_SUCCESS; + } + drop(lock_file); + + std::thread::sleep(std::time::Duration::from_millis(200)); + } + + error_eprintln!("unable to start service: timed out"); + SERVICE_TIMED_OUT } fn stop_main(paths: &Paths) -> i32 { diff --git a/espanso/src/exit_code.rs b/espanso/src/exit_code.rs index 15be31b..c3bd78a 100644 --- a/espanso/src/exit_code.rs +++ b/espanso/src/exit_code.rs @@ -51,6 +51,7 @@ pub const SERVICE_FAILURE: i32 = 1; pub const SERVICE_NOT_REGISTERED: i32 = 2; pub const SERVICE_ALREADY_RUNNING: i32 = 3; pub const SERVICE_NOT_RUNNING: i32 = 4; +pub const SERVICE_TIMED_OUT: i32 = 5; pub const WORKAROUND_SUCCESS: i32 = 0; #[allow(dead_code)]