Module: WatcherOnTheWall::InstanceMethods

Defined in:
lib/watcher_on_the_wall.rb

Instance Method Summary collapse

Instance Method Details

#watcher_on_the_wall(options = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/watcher_on_the_wall.rb', line 20

def watcher_on_the_wall(options = {}, &block)
  configuration = {
    unique_key: (0..60).map { ('a'..'z').to_a[rand(26)] }.join,
    auto_expire_in_seconds: 300
  }.merge(options)

  key = configuration[:unique_key]
  if configuration[:auto_expire_in_seconds] == false
    expire_in = false
  else
    expire_in = configuration[:auto_expire_in_seconds].to_i
  end

  self.watch(key)
  if self.exists(key) == true

    raise Exceptions::AlreadyRunning
  end
  self.multi do
    if expire_in != false
      self.setex(key, expire_in, true)
    end
  end

  begin
    yield if block_given?
  ensure
    self.del(key)
  end
end