Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
feat: add flock
Browse files Browse the repository at this point in the history
Avoid multiple install at the same time.
  • Loading branch information
xtuc committed Jun 1, 2019
1 parent 0a8eb21 commit d4d3d70
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
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 @@ -27,6 +27,7 @@ toml = "0.5.0"
uuid = "0.7"
which = "2.0.1"
rand = "0.6.5"
fs2 = "0.4.3"

[dev-dependencies]
assert_cmd = "0.11.1"
Expand Down
10 changes: 10 additions & 0 deletions src/wranglerjs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::commands::publish::package::Package;
use crate::install;
use binary_install::Cache;
use fs2::FileExt;
use log::info;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -191,6 +192,11 @@ pub fn run_build(
// Run {npm install} in the specified directory. Skips the install if a
// {node_modules} is found in the directory.
pub fn run_npm_install(dir: PathBuf) -> Result<(), failure::Error> {
let flock_path = dir.join(&".install.lock");
let flock = File::create(&flock_path)?;
// avoid running multiple {npm install} at the same time (eg. in tests)
flock.lock_exclusive()?;

if dir.join("node_modules").exists() {
info!("skipping npm install because node_modules exists");
return Ok(());
Expand All @@ -202,6 +208,10 @@ pub fn run_npm_install(dir: PathBuf) -> Result<(), failure::Error> {
info!("Running {:?} in directory {:?}", command, dir);

let status = command.status()?;

flock.unlock()?;
fs::remove_file(&flock_path)?;

if status.success() {
Ok(())
} else {
Expand Down

0 comments on commit d4d3d70

Please sign in to comment.