Skip to content

Instantly share code, notes, and snippets.

@bastosmichael
Forked from tompesman/indexer.rb
Last active August 29, 2015 14:17
Show Gist options
  • Save bastosmichael/9133755edb05b49b43f9 to your computer and use it in GitHub Desktop.
Save bastosmichael/9133755edb05b49b43f9 to your computer and use it in GitHub Desktop.
module Search
class Indexer
include Sidekiq::Worker
sidekiq_options queue: :low, retry: false, backtrace: true
LOGGER = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
SEARCH_POOL = ConnectionPool.new(size: SEARCH_UPDATE_POOL, timeout: 5) do
Elasticsearch::Client.new host: ELASTICSEARCH_URL, logger: LOGGER
end
def perform(operation, klass, record_id, options = {})
LOGGER.debug [operation, "#{klass}##{record_id} #{options.inspect}"] if LOGGER
case operation.to_s
when /index|update/
SEARCH_POOL.with do |client|
record = klass.constantize.find(record_id)
record.__elasticsearch__.client = client
record.__elasticsearch__.__send__ "#{operation}_document"
end
when /delete/
SEARCH_POOL.with do |client|
client.delete index: klass.constantize.index_name, type: klass.constantize.document_type, id: record_id
end
else
fail ArgumentError, "Unknown operation '#{operation}'"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment