Module: Zlk::Lockable

Defined in:
lib/zlk/lockable.rb

Instance Method Summary collapse

Instance Method Details

#lockObject



29
30
31
# File 'lib/zlk/lockable.rb', line 29

def lock
  Zlk.create_lock(lock_key)
end

#lock_keyObject



3
4
5
6
7
8
9
10
11
# File 'lib/zlk/lockable.rb', line 3

def lock_key
  suffix = if respond_to?(:id)
    id
  else
    respond_to?(:uuid) ? uuid : fail("Define #{self.class}#lock_key")
  end

  "#{self.class}-#{suffix}"
end

#lock_timeoutObject



21
22
23
# File 'lib/zlk/lockable.rb', line 21

def lock_timeout
  10
end

#locked?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/zlk/lockable.rb', line 25

def locked?
  !lock.acquirable?
end

#run_exclusivelyObject



13
14
15
# File 'lib/zlk/lockable.rb', line 13

def run_exclusively
  lock.run_exclusively { yield }
end

#run_with_timeout!Object



17
18
19
# File 'lib/zlk/lockable.rb', line 17

def run_with_timeout!
  lock.with_timeout!(lock_timeout) { yield }
end