Class: HLS::Lock

Inherits:
Object
  • Object
show all
Defined in:
lib/hls/lock.rb

Overview

Advisory file lock for an encode run. Two workers asked to process the same output directory at the same time would corrupt each other’s state.json and leave a half-uploaded bundle. The lock makes that case fail loudly instead of silently.

Uses ‘flock` with `LOCK_EX | LOCK_NB` — the second process gets `HLS::Lock::Busy` immediately rather than blocking. The lock file itself (`<output>/.hls-lock`) is left on disk; only the kernel-level advisory lock is released. This keeps the file’s inode stable so ‘flock` works reliably across re-runs.

Defined Under Namespace

Classes: Busy

Constant Summary collapse

FILENAME =
".hls-lock"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_dir) ⇒ Lock

Returns a new instance of Lock.



27
28
29
# File 'lib/hls/lock.rb', line 27

def initialize(output_dir)
  @path = Pathname.new(output_dir).join(FILENAME)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



25
26
27
# File 'lib/hls/lock.rb', line 25

def path
  @path
end

Class Method Details

.acquire(output_dir, &block) ⇒ Object



21
22
23
# File 'lib/hls/lock.rb', line 21

def self.acquire(output_dir, &block)
  new(output_dir).acquire(&block)
end

Instance Method Details

#acquireObject

Acquires the lock, yields, releases. If another process holds it, raises ‘HLS::Lock::Busy` immediately — we don’t wait.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hls/lock.rb', line 33

def acquire
  @path.parent.mkpath
  File.open(@path, File::CREAT | File::RDWR, 0o644) do |f|
    unless f.flock(File::LOCK_EX | File::LOCK_NB)
      raise Busy, "another process is already encoding #{@path.parent}"
    end
    f.truncate(0)
    f.write("#{Process.pid}\n")
    f.flush
    begin
      yield
    ensure
      f.flock(File::LOCK_UN)
    end
  end
end