Created
October 10, 2014 12:45
-
-
Save tompesman/0af86c463301bf58f83e to your computer and use it in GitHub Desktop.
Sidekiq worker for Elasticsearch with connection pool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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