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.
This commit is contained in:
0x1eef 2023-11-27 18:11:22 -03:00
parent df78d78455
commit 594d76fd56
3 changed files with 47 additions and 9 deletions

View file

@ -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"

View file

@ -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

View file

@ -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"