Module: Chewy::Journal::Apply

Defined in:
lib/chewy/journal/apply.rb

Class Method Summary collapse

Class Method Details

.since(time, options = {}) ⇒ Object

Applies all changes that were done since some moment

Parameters:

  • time (Integer)

    timestamp from which changes will be applied

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :retries (Integer)

    maximum number of attempts to make journal “empty”. By default is set to 10

  • :once (Boolean)

    shows whether we should try until the journal is clean. If set to true, :retries is ignored

  • :only (Array<Chewy::Index>)

    filters the resulting set of records by index name



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chewy/journal/apply.rb', line 11

def since(time, options = {})
  previous_entries = []
  retries = options[:retries] || 10
  stage = 0
  while stage < retries
    stage += 1
    previous_entries.select { |entry| entry.created_at.to_i >= time }
    entries = Entry.group(Entry.since(time, options[:only]))
    Entry.subtract(entries, previous_entries)
    break if entries.length.zero?
    ActiveSupport::Notifications.instrument 'apply_journal.chewy', stage: stage
    entries.each { |entry| entry.index.import(entry.object_ids, journal: false) }
    break if options[:once]
    time = Entry.recent_timestamp(entries)
    previous_entries = entries
  end
end