Module: Tins::Once

Includes:
File::Constants
Defined in:
lib/tins/once.rb

Class Method Summary collapse

Class Method Details

.only_once(lock_filename = nil, locking_constant = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/tins/once.rb', line 7

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, &block) ⇒ Object



19
20
21
# File 'lib/tins/once.rb', line 19

def try_only_once(lock_filename = nil, locking_constant = nil, &block)
  only_once(lock_filename, locking_constant || LOCK_EX | LOCK_NB, &block)
end