Class: Lipsiadmin::Loops::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/loops/base.rb

Overview

This is the base class for make a simple loop with its own custom run method

Examples:

class SimpleLoop < Lipsiadmin::Loops::Base
  def run
    debug(Time.now)
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Base

The initialize method, default we pass the logger



17
18
19
# File 'lib/loops/base.rb', line 17

def initialize(logger)
  self.logger = logger
end

Instance Attribute Details

#configObject

:nodoc:



14
15
16
# File 'lib/loops/base.rb', line 14

def config
  @config
end

#loggerObject

:nodoc:



14
15
16
# File 'lib/loops/base.rb', line 14

def logger
  @logger
end

#nameObject

:nodoc:



14
15
16
# File 'lib/loops/base.rb', line 14

def name
  @name
end

Class Method Details

.check_dependenciesObject

Ovveride this method for check dependencies



22
# File 'lib/loops/base.rb', line 22

def self.check_dependencies; end

Instance Method Details

#with_lock(entity_id, loop_id, timeout, entity_name = '') ⇒ Object

:nodoc:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/loops/base.rb', line 33

def with_lock(entity_id, loop_id, timeout, entity_name = '')#:nodoc:
  entity_name = 'item' if entity_name.to_s.empty?

  debug("Locking #{entity_name} #{entity_id}")
  lock = LoopLock.lock(:entity_id => entity_id, :loop => loop_id.to_s, :timeout => timeout)
  unless lock
    warn("Race condition detected for the #{entity_name}: #{entity_id}. Skipping the item.")
    return
  end

  begin
    yield
  ensure
    debug("Unlocking #{entity_name} #{entity_id}")
    LoopLock.unlock(:entity_id => entity_id, :loop => loop_id.to_s)
  end
end