class Searchkick::ReindexQueue

Attributes

name[R]

Public Class Methods

new(name) click to toggle source
# File lib/searchkick/reindex_queue.rb, line 5
def initialize(name)
  @name = name

  raise Error, "Searchkick.redis not set" unless Searchkick.redis
end

Public Instance Methods

clear() click to toggle source
# File lib/searchkick/reindex_queue.rb, line 49
def clear
  Searchkick.with_redis { |r| r.call("DEL", redis_key) }
end
length() click to toggle source
# File lib/searchkick/reindex_queue.rb, line 53
def length
  Searchkick.with_redis { |r| r.call("LLEN", redis_key) }
end
push(record_ids) click to toggle source

supports single and multiple ids

# File lib/searchkick/reindex_queue.rb, line 12
def push(record_ids)
  Searchkick.with_redis { |r| r.call("LPUSH", redis_key, record_ids) }
end
push_records(records) click to toggle source
# File lib/searchkick/reindex_queue.rb, line 16
def push_records(records)
  record_ids =
    records.map do |record|
      # always pass routing in case record is deleted
      # before the queue job runs
      if record.respond_to?(:search_routing)
        routing = record.search_routing
      end

      # escape pipe with double pipe
      value = escape(record.id.to_s)
      value = "#{value}|#{escape(routing)}" if routing
      value
    end

  push(record_ids)
end
reserve(limit: 1000) click to toggle source

TODO use reliable queuing

# File lib/searchkick/reindex_queue.rb, line 35
def reserve(limit: 1000)
  if supports_rpop_with_count?
    Searchkick.with_redis { |r| r.call("RPOP", redis_key, limit) }.to_a
  else
    record_ids = []
    Searchkick.with_redis do |r|
      while record_ids.size < limit && (record_id = r.call("RPOP", redis_key))
        record_ids << record_id
      end
    end
    record_ids
  end
end

Private Instance Methods

escape(value) click to toggle source
# File lib/searchkick/reindex_queue.rb, line 76
def escape(value)
  value.to_s.gsub("|", "||")
end
redis_key() click to toggle source
# File lib/searchkick/reindex_queue.rb, line 59
def redis_key
  "searchkick:reindex_queue:#{name}"
end
redis_version() click to toggle source
# File lib/searchkick/reindex_queue.rb, line 67
def redis_version
  @redis_version ||=
    Searchkick.with_redis do |r|
      info = r.call("INFO")
      matches = /redis_version:(\S+)/.match(info)
      Gem::Version.new(matches[1])
    end
end
supports_rpop_with_count?() click to toggle source
# File lib/searchkick/reindex_queue.rb, line 63
def supports_rpop_with_count?
  redis_version >= Gem::Version.new("6.2")
end