Class: Chewy::Journal

Inherits:
Object show all
Defined in:
lib/chewy/journal.rb

Overview

A class to perform journal-related actions for the specified indexes/types.

Examples:

journal = Chewy::Journal.new('places#city', UsersIndex)
journal.apply(20.minutes.ago)
journal.clean

Instance Method Summary collapse

Constructor Details

#initialize(*only) ⇒ Journal

Returns a new instance of Journal.

Parameters:



11
12
13
# File 'lib/chewy/journal.rb', line 11

def initialize(*only)
  @only = only
end

Instance Method Details

#apply(since_time, retries: 10, **import_options) ⇒ Integer

Applies all changes that were done since the specified time to the specified indexes/types.

Parameters:

  • since_time (Time, DateTime)

    timestamp from which changes will be applied

  • retries (Integer) (defaults to: 10)

    maximum number of attempts to make journal empty, 10 by default

Returns:

  • (Integer)

    the amount of journal entries found



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chewy/journal.rb', line 21

def apply(since_time, retries: 10, **import_options)
  stage = 1
  since_time -= 1
  count = 0
  while stage <= retries
    entries = Chewy::Stash::Journal.entries(since_time, only: @only).to_a.presence or break
    count += entries.size
    groups = reference_groups(entries)
    ActiveSupport::Notifications.instrument 'apply_journal.chewy', stage: stage, groups: groups
    groups.each do |type, references|
      type.import(references, import_options.merge(journal: false))
    end
    stage += 1
    since_time = entries.map(&:created_at).max
  end
  count
end

#clean(until_time = nil) ⇒ Hash

Cleans journal for the specified indexes/types.

Parameters:

  • until_time (Time, DateTime) (defaults to: nil)

    time to clean up until it

Returns:

  • (Hash)

    delete_by_query ES API call result



43
44
45
# File 'lib/chewy/journal.rb', line 43

def clean(until_time = nil)
  Chewy::Stash::Journal.clean(until_time, only: @only)
end