Class: Textus::Infra::BuildLock
- Inherits:
-
Object
- Object
- Textus::Infra::BuildLock
- Defined in:
- lib/textus/infra/build_lock.rb
Constant Summary collapse
- LOCK_FILENAME =
".build.lock"- MAX_HOLDER_BYTES =
512
Class Method Summary collapse
Instance Method Summary collapse
- #acquire_or_raise ⇒ Object
-
#initialize(root:) ⇒ BuildLock
constructor
A new instance of BuildLock.
Constructor Details
#initialize(root:) ⇒ BuildLock
Returns a new instance of BuildLock.
15 16 17 18 |
# File 'lib/textus/infra/build_lock.rb', line 15 def initialize(root:) @path = File.join(root, LOCK_FILENAME) @file = nil end |
Class Method Details
.with(root:) ⇒ Object
11 12 13 |
# File 'lib/textus/infra/build_lock.rb', line 11 def self.with(root:, &) new(root: root).acquire_or_raise(&) end |
Instance Method Details
#acquire_or_raise ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/textus/infra/build_lock.rb', line 20 def acquire_or_raise FileUtils.mkdir_p(File.dirname(@path)) @file = File.open(@path, File::RDWR | File::CREAT, 0o644) @file.close_on_exec = true unless @file.flock(File::LOCK_EX | File::LOCK_NB) holder = read_holder_safely @file.close @file = nil raise Textus::BuildInProgress.new(holder) end @file.truncate(0) @file.write("pid=#{Process.pid} started=#{Time.now.utc.iso8601} host=#{Socket.gethostname}\n") @file.flush yield ensure release end |