diff --git a/CHANGELOG.md b/CHANGELOG.md index ee043afdb..cd16219af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ Changes since last non-beta release. ### Added - `append_stylesheet_pack_tag` helper. It helps in configuring stylesheet pack names from the view for a route or partials. It is also required for filesystem-based automated Component Registry API on React on Rails gem. [PR 144](https://github.com/shakacode/shakapacker/pull/144) by [pulkitkkr](https://github.com/pulkitkkr). +### Fixed +- Make sure at most one compilation runs at a time (#139). + _Please add entries here for your pull requests that are not yet released._ ## [v6.4.1] - June 5, 2022 diff --git a/lib/webpacker/compiler.rb b/lib/webpacker/compiler.rb index d295250d4..1107d86a5 100644 --- a/lib/webpacker/compiler.rb +++ b/lib/webpacker/compiler.rb @@ -14,19 +14,53 @@ def initialize(webpacker) end def compile - if stale? - run_webpack.tap do |success| - after_compile_hook - end - else + unless stale? logger.debug "Everything's up-to-date. Nothing to do" + return true + end + + if compiling? + wait_for_compilation_to_complete true + else + acquire_ipc_lock do + run_webpack.tap do |success| + after_compile_hook + end + end end end private attr_reader :webpacker + def acquire_ipc_lock + open_lock_file do |lf| + lf.flock(File::LOCK_EX) + yield if block_given? + end + end + + def locked? + open_lock_file do |lf| + lf.flock(File::LOCK_EX | File::LOCK_NB) != 0 + end + end + + alias compiling? locked? + + def wait_for_compilation_to_complete + logger.info "Waiting for the compilation to complete..." + acquire_ipc_lock + end + + def open_lock_file + lock_file_name = File.join(Dir.tmpdir, "shakapacker.lock") + File.open(lock_file_name, File::CREAT) do |lf| + return yield lf + end + end + def optionalRubyRunner bin_webpack_path = config.root_path.join("bin/webpacker") first_line = File.readlines(bin_webpack_path).first.chomp