【发布时间】: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