Skip to content

Commit

Permalink
Revert to older state due to errors in log
Browse files Browse the repository at this point in the history
  • Loading branch information
thatandromeda committed May 9, 2019
1 parent 8e8abd8 commit 1586ae2
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions app/models/reindex_run.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
class ReindexRun < ActiveRecord::Base
REINDEXED_MODELS = [Notice, Entity].freeze
def self.last_run
order('created_at').last
end

def self.index_changed_model_instances
this_run = ReindexRun.create!
begin
last_run_instance = last_run
last_run_time = (last_run_instance && last_run_instance.created_at) || 100.years.ago

metadata = {}
REINDEXED_MODELS.each do |model|
metadata[model.name] = this_run.index_model_for(model)
end
this_run = ReindexRun.create!

this_run.apply_metadata(metadata)
entity_count = reindex_entities_updated_after(last_run_time)
notice_count = reindex_notices_updated_after(last_run_time)

sweep_search_result_caches
rescue => e
Rails.logger.error "Indexing did not succeed because: #{e.inspect}"
this_run.update_attributes(
notice_count: notice_count, entity_count: entity_count,
updated_at: Time.now
)

sweep_search_result_caches
rescue => e
Rails.logger.error "Indexing did not succeed because: #{e.inspect}"
end
end

def self.sweep_search_result_caches
ApplicationController.new.expire_fragment(/search-result-[a-f0-9]{32}/)
end

def self.indexed?(klass, id)
def self.is_indexed?(klass, id)
client = klass.__elasticsearch__.client
client.get(index: klass.index_name, id: id)['found'] rescue false
end

# The offset is needed to get the run previous to the current one.
def last_run
self.class.order('created_at').offset(1).last
end
private

def index_model_for(model)
def self.index_model_for(model, last_run_time)
count = 0
batch_size = (ENV['BATCH_SIZE'] || 100).to_i
updateable_set(model).find_in_batches(batch_size: batch_size) do |instances|
model.where('updated_at > ? or updated_at is null', last_run_time)
.find_in_batches(batch_size: batch_size) do |instances|
instances.each do |instance|
instance.__elasticsearch__.index_document
count += 1
Expand All @@ -42,21 +48,11 @@ def index_model_for(model)
count
end

def apply_metadata(metadata)
attrs = metadata.map { |k, v| ["#{k.name.downcase}_count", v] }.to_h
attrs[updated_at] = Time.now
update_attributes(attrs)
def self.reindex_notices_updated_after(last_run_time)
index_model_for(Notice, last_run_time)
end

private

def last_run_time
@last_run_time ||= begin
last_run&.created_at || 100.years.ago
end
end

def updateable_set(model)
model.where('updated_at > ? or updated_at is null', last_run_time)
def self.reindex_entities_updated_after(last_run_time)
index_model_for(Entity, last_run_time)
end
end

0 comments on commit 1586ae2

Please sign in to comment.