Sha256: 833e8d5b9505b0a5ed8ffc99cecc9eab86a09b7e09f3d370ff972cf1f91e4504

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'sidekiq'
require 'redis-namespace'
require 'redis-lock'

module ChewyKiqqer
  class Worker

    include Sidekiq::Worker

    def perform(index_name, ids)
      ActiveSupport::Notifications.instrument('perform.chewy_kiqqer', index_name: index_name, ids: ids) do
        with_lock(index_name, ids)
      end
    end

    private

    def sidekiq_redis
      # Sidekiq.redis may both be a Redis client directly, or a
      # redis namespace. Since namespaces don't allow for passing through
      # the lock behaviour any more, we want the "real" connection in
      # any case
      Sidekiq.redis do |redis|
        real_redis = redis.is_a?(Redis::Namespace) ? redis.redis : redis
        yield real_redis
      end
    end

    def with_lock(index_name, ids)
      sidekiq_redis do |redis|
        lock_name = "chewy-kiqqer:#{ChewyKiqqer.locking_scope}:#{index_name}-#{ids.join('-')}"
        redis.lock(lock_name, life: 60, acquire: 5) { index(index_name, ids) }
      end
    end

    def index(index_name, ids)
      ActiveSupport::Notifications.instrument('index.chewy_kiqqer', index_name: index_name, ids: ids) do
        Chewy.derive_type(index_name).import ids
      end
    end


  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chewy_kiqqer-0.2.6 lib/chewy_kiqqer/worker.rb