Class: ProcessLock::WithProcessLock

Inherits:
Object
  • Object
show all
Defined in:
lib/process_lock/with_process_lock.rb

Class Method Summary collapse

Class Method Details

.can_execute?(key) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/process_lock/with_process_lock.rb', line 20

def self.can_execute?(key)
  !redis.exists(key)
end

.execute(key, &blk) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/process_lock/with_process_lock.rb', line 4

def self.execute(key, &blk)
  if can_execute?(key)
    redis.set(key, true)
    begin
      yield blk
    ensure
      # Delete the key and quit redis even if the block raises an exception
      redis.del(key)
      redis.quit
    end
  else
    redis.quit
    raise StandardError, "Can not run process because #{key} taken."
  end
end

.redisObject



24
25
26
# File 'lib/process_lock/with_process_lock.rb', line 24

def self.redis
  @_redis ||= Redis.new
end