Sha256: f773452c65cf31392e70add0c518664129c4086b8fe252d03cc8fb246de9cdba

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

module BunnyCarrot
  class BaseWorker
    include BunnyCarrot::Logger

    STRATEGY_MAP = {
        block:             Strategy::Block,
        drop:              Strategy::Drop,
        restart_and_block: Strategy::RestartAndBlock,
        restart_and_drop:  Strategy::RestartAndDrop
    }

    def self.error_handling_strategies(hash)
      raise 'Invalid strategies hash' unless hash.kind_of? Hash
      @@strategies = hash
    end

    def initialize
      @@strategies ||= {}
    end

    def run(payload, headers)
      perform(payload, headers)
    end

    def strategy(message, exception)
      strategy, restart_count = Array(@@strategies[exception.class])
      strategy                ||= default_strategy
      klass                   = STRATEGY_MAP[strategy]
      klass.nil? ? raise("Undefined queue strategy #{strategy}") : logger.info("Applying #{klass}")
      hash = message.merge(Hamster.hash(restart_count: restart_count))
      klass.new(hash)
    end

    def default_strategy
      :block
    end

    protected

    def perform(payload, headers)
      raise NotImplememtedError
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bunny_carrot-0.0.2 lib/bunny_carrot/base_worker.rb