我有一个控制器动作,它触发一个延迟的作业,它有一个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发布于 2019-08-26 20:17:24
我首先建议将作业存储在数据库中,并循环遍历与触发它的用户相关的作业,并识别它。
提交作业后,触发旋转器,然后尝试硬编码一个从数据库检索相关作业的开放循环,并检查其状态是否为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,取下微调控件并输出您想要的相关消息。
当然,如果您愿意,也可以将开环逻辑硬编码在应用程序的前端。
因此,简单回顾一下这些步骤:
或
第3步和第4步实际上可能只是与前端相关的步骤,与ShipmentsController无关。
发布于 2019-08-27 19:15:35
我最终得到了以下解决方案。
我将BulkShipmentProcess的实例化移到了控制器,并在保存它之后,重定向到此实例的show操作,并触发了第一个作业,将该实例作为作业参数传递。
在作业内部,触发第二个作业,并将一些属性分配给BulkShipmentProcess实例。
当在bulk_shipment_process/id页面上时,JS正在为实例轮询DB,以获得分配的新属性,并在成功时呈现这些属性。
控制器:
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)
returnhttps://stackoverflow.com/questions/57657030
复制相似问题