Skip to content

Instantly share code, notes, and snippets.

@mguterl
Forked from d--j/sunspot_resque_init.rb
Created September 1, 2010 17:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mguterl/561055 to your computer and use it in GitHub Desktop.
Save mguterl/561055 to your computer and use it in GitHub Desktop.
require 'sunspot'
require 'sunspot/session_proxy/abstract_session_proxy'
require 'resque'
require 'resque-retry'
class SunspotResqueSessionProxy < Sunspot::SessionProxy::AbstractSessionProxy
attr_reader :session
delegate :config, :delete_dirty?, :dirty?,
:new_search, :search,
:new_more_like_this, :more_like_this,
:remove, :remove!,
:remove_by_id, :remove_by_id!,
:remove_all, :remove_all!,
:batch, :commit, :commit_if_delete_dirty, :commit_if_dirty,
:index!, :to => :session
def initialize(session)
@session = session
end
def index(*objects)
args = []
objects.flatten.compact.each do |object|
args << object.class.name << object.id
end
Resque.enqueue(SunspotIndexJob, *args) unless args.empty?
end
end
class SunspotIndexJob
extend Resque::Plugins::Retry
@retry_delay = 10
@queue = :indexer
def self.perform(*args)
objects = []
args.each_slice(2) do |(clazz, id)|
objects << clazz.constantize.find(id)
end
Sunspot.session.index!(*objects)
end
end
Sunspot.session = SunspotResqueSessionProxy.new(
Sunspot::SessionProxy::ThreadLocalSessionProxy.new(
Sunspot::Configuration.build())) # ThreadLocalSessionProxy calls Sunspot::Configuration.new but this is not public, so we give it a standard configuration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment