Sha256: d9545baa237513ebca210cc7c3e2873ebf9f1b4352b5775372a6fda5542d3338

Contents?: true

Size: 1.48 KB

Versions: 30

Compression:

Stored size: 1.48 KB

Contents

module Lipsiadmin
  module Loops
    # 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
    #
    class Base#:nodoc:
      attr_accessor :name, :config, :logger
      
      # The initialize method, default we pass the logger
      def initialize(logger)
        self.logger = logger
      end

      # Ovveride this method for check dependencies
      def self.check_dependencies; end

      # Proxy logger calls to our logger
      [ :debug, :error, :fatal, :info, :warn ].each do |meth_name|
        class_eval <<-EVAL
          def #{meth_name}(message)
            logger.#{meth_name}("\#{Time.now}: loop[\#{name}/\#{Process.pid}]: \#{message}")
          end
        EVAL
      end

      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
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
lipsiadmin-5.1.6 lib/loops/base.rb
lipsiadmin-5.1.5 lib/loops/base.rb
lipsiadmin-5.1.4 lib/loops/base.rb
lipsiadmin-5.1.3 lib/loops/base.rb
lipsiadmin-5.1.2 lib/loops/base.rb
lipsiadmin-5.1.1 lib/loops/base.rb
lipsiadmin-5.1.0 lib/loops/base.rb
lipsiadmin-5.0.9 lib/loops/base.rb
lipsiadmin-5.0.8 lib/loops/base.rb
lipsiadmin-5.0.7 lib/loops/base.rb
lipsiadmin-5.0.6 lib/loops/base.rb
lipsiadmin-5.0.5 lib/loops/base.rb
lipsiadmin-5.0.4 lib/loops/base.rb
lipsiadmin-4.1.8 lib/loops/base.rb
lipsiadmin-5.0.0 lib/loops/base.rb
lipsiadmin-5.0.1 lib/loops/base.rb
lipsiadmin-5.0.2 lib/loops/base.rb
lipsiadmin-5.0.3 lib/loops/base.rb
lipsiadmin-3.4.2 lib/loops/base.rb
lipsiadmin-3.4.1 lib/loops/base.rb