【问题标题】:Update controller after the job is done工作完成后更新控制器
【发布时间】:2019-08-27 19:15:34
【问题描述】:

我有一个控制器动作,它触发了一个延迟的作业,它有 after_perform 钩子,其中触发了另一个延迟的作业。

我需要在所有作业完成后渲染一个视图并向用户显示一个微调器,同时作业仍在运行。

职位如下所示:

class DuplicateShipmentJob < ApplicationJob
  after_perform do |job|
    shipment = job.arguments.first[:shipment]
    shipment_ids = Shipment.where(public_id: shipment.public_id).map(&:id)
    bulk_shipment_process = BulkShipmentProcess.new
    bulk_shipment_process.shipment_ids = shipment_ids
    bulk_shipment_process.user = shipment.user

    if bulk_shipment_process.save
      Shipment.where(public_id: shipment.public_id).each do |shipment|
        shipment.bulk_shipment_process_id = bulk_shipment_process.id
      end
      BulkShipmentsProcessor.delay.process_bulk_shipments(args)
    end
   end

 def perform(shipment:, label_count:)
   label_count.times do
     duplicated_shipment = shipment.dup
     duplicated_shipment.package = shipment.package.dup
     duplicated_shipment.save!
   end
  end
 end

最好的方法是什么?

我在对数据库的 ajax 请求和设置操作电缆以收听作业结果之间犹豫不决。

控制器如下所示:

class ShipmentsController < ApplicationController
 def replicate_shipment
  @shipment = build_shipment
  @shipment.save!
  DuplicateShipmentJob.perform_later(
    shipment: @shipment,
    label_count: @shipment.create_multiple_shipments.to_i - 1,
  )
  # here i need to wait until the process instance appears in the DB
  @bulk_shipment_process = BulkShipmentProcess.where(id: Shipment.last.bulk_shipment_process_id) 
  redirect_to bulk_shipment_process_path(@bulk_shipment_process)
 end
end

【问题讨论】:

  • 您可以使用 DuplicateShipmentJob.new.perform() 而不是稍后再执行。
  • @fidato,我使用perform later,因为我希望它异步运行

标签: javascript ruby-on-rails delayed-job


【解决方案1】:

我首先建议将作业存储在数据库中并循环访问与触发它的用户相关的相关作业并识别它。

提交作业后,触发微调器,然后尝试硬编码一个从数据库中检索相关作业的开环,并检查其状态是否为done

        loop do
          status = DelayedJob.find(JOB_ID)
          case status
          when 'DONE' then return true
          when 'PROCESSING' then sleep(10)
          else raise(JobRunFailed, "Job run failed with: #{status}")
          end
        end

,取下微调器,输出你想要的相关信息。

当然,如果您愿意,也可以在应用程序的前端硬编码开环逻辑。

所以回顾一下步骤:

  1. 存储后在数据库中识别相关作业
  2. 触发等待微调器在此处重定向到 bulk_shipment_process_path
  3. 循环遍历已识别的作业,直到它被标记为doneperformed
  4. 循环断开后更改相应界面。

第 3 步和第 4 步实际上可能只是与前端相关的步骤,与 ShipmentsController 无关。

【讨论】:

  • 好主意。我使用Delayed::Backend::ActiveRecord::Job。如何从中检索状态?
  • 这是你必须自己挖掘的东西。也许这是一个好的开始:medium.com/galvanize/…
【解决方案2】:

我最终得到了以下解决方案。

我将BulkShipmentProcess 的实例化移动到控制器,并在保存后,重定向到此实例的show 操作并触发第一个作业,将该实例作为作业参数传递。

在作业内部触发了第二个作业,并为BulkShipmentProcess 实例分配了一些属性。

bulk_shipment_process/id 页面上,JS 正在轮询数据库以获取实例以获取分配的新属性,并在成功后呈现这些属性。

控制器:

class ShipmentsController < ApplicationController
  def replicate_shipment
    @shipment = build_shipment
    @shipment.save!
    @bulk_shipment_process = BulkShipmentProcess.new()
    @bulk_shipment_process.save
    if @bulk_shipment_process.id?
       DuplicateShipmentJob.perform_later(@bulk_shipment_process)
       redirect_to bulk_shipment_process_path(@bulk_shipment_process)
    else
      flash.now[:error] = "error"
      render :new
    end
  end
end

工作:

class DuplicateShipmentJob < ApplicationJob
 def perform(bulk_shipment_process:)
  # do something
  # assign new attributes to the instance of interest
  # trigger second delayed job upon instance of interest
 end
end

轮询功能:

currentStatus = (element) ->
  element.find("div").data("status")

statusFinished = (status) ->
  status.startsWith("finish") ||
    status.startsWith("fail") ||
    status.startsWith("unknown") ||
    status.startsWith("stuck")

statusUpdate = (element, done_action) ->
  $.ajax
    url: element.data("target")
    success: (data) ->
      element.html data
      status = currentStatus element

      if statusFinished(status)
        console.log "done_action " + done_action
        if done_action == "reload"
          console.log "reload"
          location.reload()
      else
        setTimeout (->
          statusUpdate(element, done_action)
          return
        ), 5000
      return
  return

$ ->
  $('[data-controller="status-update"]').each (index) ->
    target = $(this).data("target")
    status = currentStatus $(this)
    done_action = $(this).data("done")

    unless statusFinished(status)
      statusUpdate($(this), done_action)
    return

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多