Sha256: 69f18ac7862e7a6367930e4e3ecbf7c7661e8b218cb7953bbbc13ec92a4fdcfc

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require "sidekiq/expected_failures/version"
require "sidekiq/expected_failures/middleware"


module Sidekiq
  def self.expected_failures=(exceptions)
    @expected_failures = exceptions
  end

  def self.expected_failures
    @expected_failures || {}
  end

  module ExpectedFailures
    def self.dates
      Sidekiq.redis do |c|
        c.smembers "expected:dates"
      end.sort.reverse.each_with_object({}) do |d, hash|
        hash[d] = Sidekiq.redis { |c| c.llen("expected:#{d}") }
      end
    end

    def self.counters
      Sidekiq.redis { |r| r.hgetall("expected:count") }
    end

    def self.clear_all
      clear(dates.keys)
    end

    def self.clear_old(days_ago = 1)
      range = dates.keys.delete_if { |d| Date.parse(d) > Date.today.prev_day(days_ago) }
      clear(range)
    end

    def self.clear_counters
      Sidekiq.redis { |r| r.del("expected:count") }
    end

    private

    def self.clear(dates)
      dates.each do |date|
        Sidekiq.redis do |c|
          c.multi do |m|
            m.srem("expected:dates", date)
            m.del("expected:#{date}")
          end
        end
      end
    end
  end
end

Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    chain.add Sidekiq::ExpectedFailures::Middleware
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sidekiq-expected_failures-0.4.0 lib/sidekiq/expected_failures.rb