Created
May 27, 2010 16:52
-
-
Save d--j/416040 to your computer and use it in GitHub Desktop.
This file contains 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
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