Module: Elastics::LiveReindex

Defined in:
lib/elastics/admin_live_reindex.rb

Overview

private module

Defined Under Namespace

Modules: Redis Classes: ExtraIndexError, LiveReindexInProgressError, MissingAppIdError, MissingEnsureIndicesError, MissingOnReindexBlockError, MissingRedisError, MissingStopIndexingProcError, MultipleReindexError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#each_changeObject (readonly)

Returns the value of attribute each_change.



65
66
67
# File 'lib/elastics/admin_live_reindex.rb', line 65

def each_change
  @each_change
end

Instance Method Details

#get_timestamp_from_index(index) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/elastics/admin_live_reindex.rb', line 136

def get_timestamp_from_index(index)
  index =~ /^(\d{14})_/
  timestr = $1
  return unless timestr
  timestr
  timearr = timestr.scan /.{1,2}/
  Time.mktime *timearr.unshift([timearr.shift,timearr.shift].join)
end

#on_each_change(&block) ⇒ Object



62
63
64
# File 'lib/elastics/admin_live_reindex.rb', line 62

def on_each_change(&block)
  @each_change = block
end

#on_reindex(&block) ⇒ Object



58
59
60
# File 'lib/elastics/admin_live_reindex.rb', line 58

def on_reindex(&block)
  @reindex = block
end

#on_stop_indexing(&block) ⇒ Object



67
68
69
# File 'lib/elastics/admin_live_reindex.rb', line 67

def on_stop_indexing(&block)
  @stop_indexing = block
end

#prefix_index(index) ⇒ Object

Raises:



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/elastics/admin_live_reindex.rb', line 111

def prefix_index(index)
  base = unprefix_index(index)
  # raise if base is not included in @ensure_indices
  raise ExtraIndexError, "The index #{base} is missing from the :ensure_indices option. Reindexing aborted." \
        if @ensure_indices && !@ensure_indices.include?(base)
  prefixed = @prefix + base
  unless @indices.include?(base)
    unless Elastics.indices_exists(:index => prefixed)
      Conf.indices.create_index(base, prefixed, :raise => false) # it might trigger an error if 2 threads create the index at the same time
      if Conf.optimize_indexing
        @refresh_intervals[index] ||= Elastics.get_index_settings(:index => prefixed)[prefixed]['settings']['index.refresh_interval']
        Elastics.put_index_settings(:index => prefixed,
                                    :data  => {:index => {:refresh_interval => '-1'}}) unless @refresh_intervals[index] == '-1'
      end
    end
    @indices |= [base]
  end
  prefixed
end

#reindex(opts = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



71
72
73
74
75
# File 'lib/elastics/admin_live_reindex.rb', line 71

def reindex(opts={})
  yield self
  opts[:verbose] = true unless opts.has_key?(:verbose)
  perform(opts)
end

#reindex_indices(opts = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/elastics/admin_live_reindex.rb', line 77

def reindex_indices(opts={})
  yield self if block_given?

  opts[:verbose] = true unless opts.has_key?(:verbose)
  opts[:index] ||= opts.delete(:indices) || Conf.indices.keys

  # we override the on_reindex eventually set
  on_reindex do
    migrate_indices(opts)
  end

  perform(opts)
end

#should_prefix_index?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/elastics/admin_live_reindex.rb', line 91

def should_prefix_index?
  Redis.get(:pid) == $$.to_s
end

#should_track_change?Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/elastics/admin_live_reindex.rb', line 95

def should_track_change?
  pid = Redis.get(:pid)
  !!pid && !(pid == $$.to_s)
end

#track_change(action, document) ⇒ Object



100
101
102
# File 'lib/elastics/admin_live_reindex.rb', line 100

def track_change(action, document)
  Redis.rpush(:changes, MultiJson.encode([action, document]))
end

#track_external_change(app_id, action, document) ⇒ Object

use this method when you are tracking the change of another app you must pass the app_id of the app being affected by the change



106
107
108
109
# File 'lib/elastics/admin_live_reindex.rb', line 106

def track_external_change(app_id, action, document)
  return unless Conf.redis
  Conf.redis.rpush("#{KEYS[:changes]}-#{app_id}", MultiJson.encode([action, document]))
end

#unprefix_index(index) ⇒ Object

remove the (eventual) prefix



132
133
134
# File 'lib/elastics/admin_live_reindex.rb', line 132

def unprefix_index(index)
  index.sub(/^\d{14}_/, '')
end