【发布时间】:2020-10-09 13:27:25
【问题描述】:
之前我有我的控制器类,它正在创建服务实例并调用服务方法“同步”,如下所示。在“同步”方法完成后,rails 用于将 JSON 消息呈现回我的 node.js。
if @service.valid_connection?
@service.synchronize
render json: {
status: :unprocessable_entity,
errors: @service.message
}, status: :ok
else
render json: {
status: :unprocessable_entity,
errors: @event.errors.full_messages
}, status: :ok
end
但是,由于我的“同步”方法执行时间有点长,所以我创建了一个延迟作业来占用我的“同步”任务。所以目前我的控制器看起来像这样
if @service.valid_connection?
::Events::WegSyncJob.perform_later(event_id, b_cancelled)
else
render json: {
status: :unprocessable_entity,
errors: @event.errors.full_messages
}, status: :ok
end
现在我无法将我的渲染 JSON 放在这里,因为它会在作业传递给延迟作业后立即执行,我在作业类中使用 after_perform 方法:
after_perform do |job|
//below code is wrong
render json: {
status: :ok,
message: "Check notifications"
}, status: :ok
end
def perform(event_id, b_cancelled)
//call to synchronize
end
但是,我不能从我的 Job 类中调用“渲染”,因为它只能从控制器中调用。完成此后台作业后,如何将 JSON 消息返回到我的 node.js (UI)?
【问题讨论】:
标签: ruby-on-rails response sidekiq delayed-job rails-activejob