Skip to content

Commit 7d3f7ac

Browse files
authored
Merge pull request #115 from teamclouday/single-instance
Ensure single instance
2 parents 512c45b + 0f45ab1 commit 7d3f7ac

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

RustApp/Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RustApp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ speexdsp = { git = "https://github.com/wiiznokes/speexdsp-rs-fork", branch = "ve
101101
"sys",
102102
"vendored-sys",
103103
] }
104+
fslock = "0.2.1"
104105

105106
[build-dependencies]
106107
prost-build = "0.14"

RustApp/src/main.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{fs::File, path::Path};
88
use clap::Parser;
99
use config::{Args, Config};
1010
use directories::ProjectDirs;
11+
use fslock::LockFile;
1112
use ui::app::run_ui;
1213
use utils::{APP, ORG, QUALIFIER};
1314
use zconf::ConfigManager;
@@ -47,6 +48,7 @@ fn main() {
4748
utils::setup_wgpu();
4849

4950
let project_dirs = ProjectDirs::from(QUALIFIER, ORG, APP).unwrap();
51+
5052
let log_path = if cfg!(debug_assertions) {
5153
Path::new("log")
5254
} else {
@@ -75,6 +77,21 @@ fn main() {
7577
.parse_default_env()
7678
.init();
7779

80+
// ensure single instance
81+
let instance_lock_path = if cfg!(debug_assertions) {
82+
std::path::PathBuf::from("log").join("app.lock")
83+
} else {
84+
project_dirs.cache_dir().join("app.lock")
85+
};
86+
let mut app_lock = LockFile::open(&instance_lock_path).expect("Failed to open app lock file");
87+
if !app_lock.try_lock_with_pid().unwrap_or(false) {
88+
error!(
89+
"Another instance is already running. PID can be found in {:?}",
90+
instance_lock_path
91+
);
92+
return;
93+
}
94+
7895
// generated from https://patorjk.com/software/taag/#p=display&h=2&f=Doom&t=AndroidMic
7996
info!(
8097
r"

0 commit comments

Comments
 (0)