ActiveJob - 延迟执行after_perform方法几秒钟

时间:2018-03-15 16:40:18

标签: ruby-on-rails actioncable rails-activejob

我想在执行作业后向用户显示flash消息。

问题是,在作业排队之前,我必须在我的页面上刷新。如果作业需要一段时间,那么我的信息就会正确显示。

但如果作业执行得太快,则会在页面刷新之前显示该消息,并在页面刷新后丢失。

那么,我可以在执行after_perform方法之前等待几秒钟,有点像这样:

class CsvImportJob < ApplicationJob
  queue_as :default
  after_perform :flash, wait: 5.seconds

我的控制器:

def upload_merchant_list
    process_upload(:merchant_list)
    flash[:notice] = "Done processing merchants list. Starting job in the background..." if status == :ok

    respond_to do |format|
      format.html { redirect_to financing_campaign_path(@campaign) }
      format.json { render json: { location: financing_campaign_path(@campaign) }, status: status }
    end
  end

和我的工作:

class CsvImportJob < ApplicationJob
  queue_as :default
  after_perform :flash

  def perform(params)
    [...]
  end

  private

  def flash
    @notification_service ||= NotificationService.instance
    @notification_service.flash(job_user, 'Finished processing list in the background.',  NotificationService::INFO)
  end
end

谢谢!

0 个答案:

没有答案
相关问题