Class: HLS::Lock
- Inherits:
-
Object
- Object
- HLS::Lock
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
-
#acquire ⇒ Object
Acquires the lock, yields, releases.
-
#initialize(output_dir) ⇒ Lock
constructor
A new instance of Lock.
Constructor Details
Instance Attribute Details
#path ⇒ Object (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
#acquire ⇒ Object
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 |