feat(core): prevent espanso from starting if legacy instance is running

This commit is contained in:
Federico Terzi 2021-06-02 12:28:35 +02:00
parent 2cf6cafdb6
commit 4dd3df2326
4 changed files with 25 additions and 19 deletions

View File

@ -27,14 +27,7 @@ use espanso_ipc::IPCClient;
use espanso_path::Paths; use espanso_path::Paths;
use log::{error, info, warn}; use log::{error, info, warn};
use crate::{ use crate::{exit_code::{DAEMON_ALREADY_RUNNING, DAEMON_GENERAL_ERROR, DAEMON_LEGACY_ALREADY_RUNNING, DAEMON_SUCCESS, WORKER_EXIT_ALL_PROCESSES, WORKER_RESTART, WORKER_SUCCESS}, ipc::{create_ipc_client_to_worker, IPCEvent}, lock::{acquire_daemon_lock, acquire_legacy_lock, acquire_worker_lock}};
exit_code::{
DAEMON_ALREADY_RUNNING, DAEMON_GENERAL_ERROR, DAEMON_SUCCESS, WORKER_EXIT_ALL_PROCESSES,
WORKER_RESTART, WORKER_SUCCESS,
},
ipc::{create_ipc_client_to_worker, IPCEvent},
lock::{acquire_daemon_lock, acquire_worker_lock},
};
use super::{CliModule, CliModuleArgs}; use super::{CliModule, CliModuleArgs};
@ -65,6 +58,14 @@ fn daemon_main(args: CliModuleArgs) -> i32 {
return DAEMON_ALREADY_RUNNING; return DAEMON_ALREADY_RUNNING;
} }
let legacy_lock_file = acquire_legacy_lock(&paths.runtime);
if legacy_lock_file.is_none() {
// TODO: show a (blocking) alert message using modulo
error!("an instance of legacy espanso is running, please terminate it, otherwise the new version cannot start");
return DAEMON_LEGACY_ALREADY_RUNNING;
}
// TODO: we might need to check preconditions: accessibility on macOS, presence of binaries on Linux, etc // TODO: we might need to check preconditions: accessibility on macOS, presence of binaries on Linux, etc
info!("espanso version: {}", VERSION); info!("espanso version: {}", VERSION);

View File

@ -20,14 +20,7 @@
use crossbeam::channel::unbounded; use crossbeam::channel::unbounded;
use log::{error, info}; use log::{error, info};
use crate::{ use crate::{engine::event::ExitMode, exit_code::{WORKER_ALREADY_RUNNING, WORKER_EXIT_ALL_PROCESSES, WORKER_GENERAL_ERROR, WORKER_LEGACY_ALREADY_RUNNING, WORKER_RESTART, WORKER_SUCCESS}, lock::{acquire_legacy_lock, acquire_worker_lock}};
engine::event::ExitMode,
exit_code::{
WORKER_ALREADY_RUNNING, WORKER_EXIT_ALL_PROCESSES, WORKER_GENERAL_ERROR, WORKER_RESTART,
WORKER_SUCCESS,
},
lock::acquire_worker_lock,
};
use self::ui::util::convert_icon_paths_to_tray_vec; use self::ui::util::convert_icon_paths_to_tray_vec;
@ -62,6 +55,12 @@ fn worker_main(args: CliModuleArgs) -> i32 {
return WORKER_ALREADY_RUNNING; return WORKER_ALREADY_RUNNING;
} }
let legacy_lock_file = acquire_legacy_lock(&paths.runtime);
if legacy_lock_file.is_none() {
error!("an instance of legacy espanso is running, please terminate it, otherwise the new version cannot start");
return WORKER_LEGACY_ALREADY_RUNNING;
}
let config_store = args let config_store = args
.config_store .config_store
.expect("missing config store in worker main"); .expect("missing config store in worker main");

View File

@ -20,9 +20,11 @@
pub const WORKER_SUCCESS: i32 = 0; pub const WORKER_SUCCESS: i32 = 0;
pub const WORKER_ALREADY_RUNNING: i32 = 1; pub const WORKER_ALREADY_RUNNING: i32 = 1;
pub const WORKER_GENERAL_ERROR: i32 = 2; pub const WORKER_GENERAL_ERROR: i32 = 2;
pub const WORKER_LEGACY_ALREADY_RUNNING: i32 = 3;
pub const WORKER_EXIT_ALL_PROCESSES: i32 = 101; pub const WORKER_EXIT_ALL_PROCESSES: i32 = 101;
pub const WORKER_RESTART: i32 = 102; pub const WORKER_RESTART: i32 = 102;
pub const DAEMON_SUCCESS: i32 = 0; pub const DAEMON_SUCCESS: i32 = 0;
pub const DAEMON_ALREADY_RUNNING: i32 = 1; pub const DAEMON_ALREADY_RUNNING: i32 = 1;
pub const DAEMON_GENERAL_ERROR: i32 = 2; pub const DAEMON_GENERAL_ERROR: i32 = 2;
pub const DAEMON_LEGACY_ALREADY_RUNNING: i32 = 3;

View File

@ -64,9 +64,13 @@ impl Drop for Lock {
} }
pub fn acquire_daemon_lock(runtime_dir: &Path) -> Option<Lock> { pub fn acquire_daemon_lock(runtime_dir: &Path) -> Option<Lock> {
Lock::acquire(runtime_dir, "espanso-daemon.lock") Lock::acquire(runtime_dir, "espanso-daemon")
} }
pub fn acquire_worker_lock(runtime_dir: &Path) -> Option<Lock> { pub fn acquire_worker_lock(runtime_dir: &Path) -> Option<Lock> {
Lock::acquire(runtime_dir, "espanso-worker.lock") Lock::acquire(runtime_dir, "espanso-worker")
}
pub fn acquire_legacy_lock(runtime_dir: &Path) -> Option<Lock> {
Lock::acquire(runtime_dir, "espanso")
} }