Skip to content

Instantly share code, notes, and snippets.

@nz
Created February 10, 2012 22:20
Show Gist options
  • Save nz/1793541 to your computer and use it in GitHub Desktop.
Save nz/1793541 to your computer and use it in GitHub Desktop.
require 'sunspot_ext/resque_session_proxy'
require 'sunspot_ext/resque_index_job'
Sunspot.session = Sunspot::ResqueSessionProxy.new(Sunspot.session)
module Sunspot
class IndexJob
@queue = :sunspot_indexer
def self.perform(*args)
objects = []
args.each_slice(2) do |(clazz, id)|
object = clazz.constantize.find_by_id(id)
# don't blow up if the object no longer exists in the db
if object
objects << object
end
end
Sunspot.session.original_session.index(*objects)
end
end
end
module Sunspot
class ResqueSessionProxy < Sunspot::SessionProxy::AbstractSessionProxy
attr_reader :original_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)
@original_session = session
end
alias_method :session, :original_session
def index(*objects)
args = []
objects.flatten.compact.each do |object|
args << object.class.name << object.id
end
Resque.enqueue(Sunspot::IndexJob, *args) unless args.empty?
end
end
end
@martinstreicher
Copy link

Fantastic. Got this running on Heroku very quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment