From bf4b7484f99b89eb7c976213624d396ff1ca649b Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Mon, 27 Nov 2023 18:11:22 -0300 Subject: [PATCH] Add lock to avoid builds running in parallel When one build is running, we don't want a second one to run as well as the two builds could step on each other toes. This change adds a locking mechanism that waits for the current build to finish before starting the next build. --- Gemfile | 5 +++-- Gemfile.lock | 12 ++++++++++-- tasks/nanoc.rake | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index 58886e1a1..6cf20c4f3 100644 --- a/Gemfile +++ b/Gemfile @@ -19,8 +19,9 @@ gem "server.rb", path: "./packages/ruby/server" ## # Everything else -gem "ryo.rb", "~> 0.3", github: "0x1eef/ryo.rb", tag: "v0.3.0" -gem "test-cmd.rb", "~> 0.3", github: "0x1eef/test-cmd.rb", tag: "v0.3.0" +gem "ryo.rb", github: "0x1eef/ryo.rb", tag: "v0.3.0" +gem "test-cmd.rb", github: "0x1eef/test-cmd.rb", tag: "v0.3.0" +gem "lockf.rb", github: "0x1eef/lockf.rb", tag: "v0.12.0" gem "standard", "~> 1.24" gem "paint", "~> 2.3" gem "dotenv", "~> 2.8" diff --git a/Gemfile.lock b/Gemfile.lock index 4f94c4df9..8f092c9af 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,10 @@ +GIT + remote: https://github.com/0x1eef/lockf.rb.git + revision: fd79284a8d3af8562654742a23b39288a0e4e24f + tag: v0.12.0 + specs: + lockf.rb (0.12.0) + GIT remote: https://github.com/0x1eef/nanoc-gzip.rb.git revision: 3bb1719ad11d983a25945c1017df76aa2f6b8540 @@ -172,6 +179,7 @@ PLATFORMS DEPENDENCIES dotenv (~> 2.8) listen (~> 3.0) + lockf.rb! memoize (~> 1.3) nanoc (~> 4.12) nanoc-gzip.rb! @@ -180,11 +188,11 @@ DEPENDENCIES paint (~> 2.3) rainpress (~> 1.0) rake - ryo.rb (~> 0.3)! + ryo.rb! sass (~> 3.7) server.rb! standard (~> 1.24) - test-cmd.rb (~> 0.3)! + test-cmd.rb! BUNDLED WITH 2.4.10 diff --git a/tasks/nanoc.rake b/tasks/nanoc.rake index 29f1a6084..e78faf48c 100644 --- a/tasks/nanoc.rake +++ b/tasks/nanoc.rake @@ -1,34 +1,63 @@ # frozen_string_literal: true +require "fileutils" +require "lockf" build_dir = Ryo.from(YAML.load_file("./nanoc.yaml")).output_dir +lockp = File.join Dir.getwd, "tmp", "build.lock" +FileUtils.touch(lockp) +lockf = LockFile.new(lockp) + namespace :nanoc do task :compile do + warn "[build] Acquire lock..." + lockf.lock ENV["SASS_PATH"] = "./src/css/" sh "bundle exec nanoc co" + rescue Interrupt + warn "SIGINT: exit" + exit + ensure + warn "[build] Release lock..." + lockf.release end task :clean do + warn "[build] Acquire lock..." + lockf.lock sh "rm -rf #{build_dir}" + ensure + warn "[build] Release lock..." + lockf.release end task :clean_css do + warn "[build] Acquire lock..." + lockf.lock cssdir = File.join(build_dir, "css") - if Dir.exist?(cssdir) - sh "rm -rf #{cssdir}" - end + sh "rm -rf #{cssdir}" if Dir.exist?(cssdir) + ensure + warn "[build] Release lock..." + lockf.release end - task watch: [:compile] do + task watch: ['build'] do + warn "[build] Acquire lock..." + lockf.lock require "listen" Listen.to File.join(Dir.getwd, "src"), force_polling: true do sh "rake build" end.start sleep + rescue Interrupt + warn "SIGINT: exit" + warn "[build] Release lock..." + lockf.release + exit end end desc "Build the website" -task build: ["nanoc:clean_css", "nanoc:compile"] +task build: ["nanoc:compile"] desc "Trigger a build when src/ is modified" task "build:watch" => "nanoc:watch"