Class: Searchkick::ReindexQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/searchkick/reindex_queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ReindexQueue

Returns a new instance of ReindexQueue.

Raises:



5
6
7
8
9
# File 'lib/searchkick/reindex_queue.rb', line 5

def initialize(name)
  @name = name

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/searchkick/reindex_queue.rb', line 3

def name
  @name
end

Instance Method Details

#clearObject



49
50
51
# File 'lib/searchkick/reindex_queue.rb', line 49

def clear
  Searchkick.with_redis { |r| r.call("DEL", redis_key) }
end

#lengthObject



53
54
55
# File 'lib/searchkick/reindex_queue.rb', line 53

def length
  Searchkick.with_redis { |r| r.call("LLEN", redis_key) }
end

#push(record_ids) ⇒ Object

supports single and multiple ids



12
13
14
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object

TODO use reliable queuing



35
36
37
38
39
40
41
42
43
44
45
46
47
# 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