Skip to content

Instantly share code, notes, and snippets.

@xrl
Forked from mguterl/index_job.rb
Created August 3, 2012 22:27
Show Gist options
  • Save xrl/3252155 to your computer and use it in GitHub Desktop.
Save xrl/3252155 to your computer and use it in GitHub Desktop.
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
module Sunspot
class IndexJob
@queue = :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
@xrl
Copy link
Author

xrl commented Aug 3, 2012

  1. put index_job.rb in app/jobs. Remove from that top level module
  2. Register SessionProxy with Sunspot

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