Skip to content

Commit

Permalink
Merge pull request #174 from Lodestone-Team/added_lock_file
Browse files Browse the repository at this point in the history
Added lock file check
  • Loading branch information
C0W0 authored Jul 15, 2023
2 parents cf73fbf + eb25229 commit e26b262
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ target/
## vscode local history
.history/

# jetbrains IDE project files
.idea/

# Test ground should be local to every developer
src/test_ground/

# lockfile
lodestone.lock

## dev
InstanceTest/
.lodestone/
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ tar = "0.4.38"
tempfile = "3.5.0"
clap = { version = "4.3.0", features = ["derive"] }
once_cell = "1.17.1"
fs3 = "0.5.0"
import_map = "0.15.0"
[dependencies.uuid]
version = "1.1.2"
Expand Down
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, EnvFilter}
use traits::{t_configurable::TConfigurable, t_server::MonitorReport, t_server::TServer};
use types::{DotLodestoneConfig, InstanceUuid};
use uuid::Uuid;
use fs3::FileExt;

pub mod auth;
pub mod db;
mod deno_ops;
Expand Down Expand Up @@ -409,6 +411,16 @@ pub async fn run(
check_for_core_update().await;
output_sys_info();

let lockfile_path = lodestone_path.join("lodestone.lock");
let file = if lockfile_path.as_path().exists() {
std::fs::File::open(lockfile_path.as_path()).expect("failed to open lockfile")
} else {
std::fs::File::create(lockfile_path.as_path()).expect("failed to create lockfile")
};
if file.try_lock_exclusive().is_err() {
panic!("Another instance of lodestone might be running");
}

let _ = migrate(&lodestone_path).map_err(|e| {
error!("Error while migrating lodestone: {}. Lodestone will still start, but one or more instance may be in an erroneous state", e);
});
Expand Down Expand Up @@ -644,6 +656,8 @@ pub async fn run(
.unwrap();
}
});
// capture file into the move block
let _file = file;
select! {
_ = write_to_db_task => info!("Write to db task exited"),
_ = event_buffer_task => info!("Event buffer task exited"),
Expand Down Expand Up @@ -710,4 +724,4 @@ pub async fn run(
guard,
shutdown_tx,
)
}
}

0 comments on commit e26b262

Please sign in to comment.