Module: Tins::Once
- Includes:
- File::Constants
- Defined in:
- lib/tins/once.rb
Overview
A module for ensuring exclusive execution of code blocks using file-based locking.
This module provides mechanisms to prevent multiple instances of a script from running simultaneously by using file system locks on the script itself or a specified lock file.
Class Method Summary collapse
-
.only_once(lock_filename = nil, locking_constant = nil) {|void| ... } ⇒ Object
Executes a block of code exclusively, ensuring only one instance runs at a time.
-
.try_only_once(lock_filename = nil, locking_constant = nil) {|void| ... } ⇒ Object
Attempts to execute a block of code exclusively, but fails immediately if another instance holds the lock.
Instance Method Summary collapse
-
#only_once(lock_filename = nil, locking_constant = nil) {|void| ... } ⇒ Object
private
Executes a block of code exclusively, ensuring only one instance runs at a time.
-
#try_only_once(lock_filename = nil, locking_constant = nil) {|void| ... } ⇒ Object
private
Attempts to execute a block of code exclusively, but fails immediately if another instance holds the lock.
Class Method Details
.only_once(lock_filename = nil, locking_constant = nil) {|void| ... } ⇒ Object
Executes a block of code exclusively, ensuring only one instance runs at a time.
Uses the script name (or specified lock file) as the locking mechanism. The first invocation will acquire an exclusive lock and execute the block, while subsequent invocations will block until the lock is released.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tins/once.rb', line 50 def only_once(lock_filename = nil, locking_constant = nil) lock_filename ||= $0 locking_constant ||= LOCK_EX f = File.new(lock_filename, RDONLY) f.flock(locking_constant) and yield ensure if f f.flock LOCK_UN f.close end end |
.try_only_once(lock_filename = nil, locking_constant = nil) {|void| ... } ⇒ Object
Attempts to execute a block of code exclusively, but fails immediately if another instance holds the lock.
This is a non-blocking version that will raise an exception if the lock cannot be acquired immediately.
76 77 78 |
# File 'lib/tins/once.rb', line 76 def try_only_once(lock_filename = nil, locking_constant = nil, &block) only_once(lock_filename, locking_constant || LOCK_EX | LOCK_NB, &block) end |
Instance Method Details
#only_once(lock_filename = nil, locking_constant = nil) {|void| ... } ⇒ Object (private)
Executes a block of code exclusively, ensuring only one instance runs at a time.
Uses the script name (or specified lock file) as the locking mechanism. The first invocation will acquire an exclusive lock and execute the block, while subsequent invocations will block until the lock is released.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tins/once.rb', line 50 def only_once(lock_filename = nil, locking_constant = nil) lock_filename ||= $0 locking_constant ||= LOCK_EX f = File.new(lock_filename, RDONLY) f.flock(locking_constant) and yield ensure if f f.flock LOCK_UN f.close end end |
#try_only_once(lock_filename = nil, locking_constant = nil) {|void| ... } ⇒ Object (private)
Attempts to execute a block of code exclusively, but fails immediately if another instance holds the lock.
This is a non-blocking version that will raise an exception if the lock cannot be acquired immediately.
76 77 78 |
# File 'lib/tins/once.rb', line 76 def try_only_once(lock_filename = nil, locking_constant = nil, &block) only_once(lock_filename, locking_constant || LOCK_EX | LOCK_NB, &block) end |