如何异步调用after_commit(太阳黑子注入)

how to call after_commit(inject by sunspot) asynchronously

我在 rails 中使用太阳黑子。 我知道 sunspot 会使用 after_commit 挂钩重新索引... 但是,如果 after_commit 失败,事务将回滚并且我要保存的 Account(ActiveRecord::Base) 将被删除。

我想使用 sidekiq,perform_async 调用 after_commit 挂钩,但不知道该怎么做。

有什么建议吗?

    module Reindex
      extend ActiveSupport::Concern

      def async_reindex
        AsyncIndexJob.perform_later(self.class.to_s, self.reload.id)
      end

      included do
        after_save :async_reindex
      end
    end

    class AsyncIndexJob < ActiveJob::Base
      queue_as :index

      def perform(*args)
        obj = args[0].constantize.find_by_id(args[1])
        if obj
          Sunspot.index obj
          Sunspot.commit
        end
      end
    end

1.include ActiveRecord::Base

中的重新索引模块

2.set :auto_index => 假

3.all完成

AsyncIndex