Rails 6 个来自 Sidekiq Job 的抓取结果 - 这可能吗?
Rails 6 grab results from Sidekiq Job - is it possible?
我有 Sidekiq 工作 (SyncProductsWorker
),它会触发一个 class Imports::SynchronizeProducts
负责几个外部 API 调用。
module Imports
class SyncProductsWorker
include Sidekiq::Worker
sidekiq_options queue: 'imports_sync'
def perform(list)
::Imports::SynchronizeProducts.new(list).call
end
end
end
Imports::SynchronizeProducts
class 给出了一组带有一些注释的 monad 结果,例如
=> [Failure("999999 Product code is not valid"), Failure(" 8888889 Product code is not valid")]
我想捕获这些结果以在 FE 上显示它们。有可能这样做吗?如果我这样做:
def perform(list)
response = ::Imports::SynchronizeProducts.new(list).call
response
end
然后在控制器内部:
def create
response = ::Imports::SyncProductsWorker.perform_async(params[:product_codes])
render json: { result: response, head: :ok }
end
结果我会有一些数字
=> "df3615e8efc56f8a062ba1c2"
我不相信你想要的是可能的。
https://github.com/mperham/sidekiq/issues/3532
The return value will be GC'd like any other unused data in a Ruby process. Jobs do not have a "result" in Sidekiq and Sidekiq does nothing with the value.
您可能需要某种模型来跟踪您的后台任务。这是即兴的,但应该给你一个想法。
EG
# @attr result [Array]
# @attr status [String] Values of 'Pending', 'Error', 'Complete', etc..
class BackgroundTask < ActiveRecord
attr_accessor :product_codes
after_create :enqueue
def enqueue
::Imports::SyncProductsWorker.perform_async(product_codes, self.id)
end
end
def perform(list, id)
response = ::Imports::SynchronizeProducts.new(list).call
if (response.has_errors?)
BackgroundTask.find(id).update(status: 'Error', result: response)
else
BackgroundTask.find(id).update(status: 'Complete', result: response)
end
end
然后只需将 BackgroundTask 模型用于前端显示即可。
我有 Sidekiq 工作 (SyncProductsWorker
),它会触发一个 class Imports::SynchronizeProducts
负责几个外部 API 调用。
module Imports
class SyncProductsWorker
include Sidekiq::Worker
sidekiq_options queue: 'imports_sync'
def perform(list)
::Imports::SynchronizeProducts.new(list).call
end
end
end
Imports::SynchronizeProducts
class 给出了一组带有一些注释的 monad 结果,例如
=> [Failure("999999 Product code is not valid"), Failure(" 8888889 Product code is not valid")]
我想捕获这些结果以在 FE 上显示它们。有可能这样做吗?如果我这样做:
def perform(list)
response = ::Imports::SynchronizeProducts.new(list).call
response
end
然后在控制器内部:
def create
response = ::Imports::SyncProductsWorker.perform_async(params[:product_codes])
render json: { result: response, head: :ok }
end
结果我会有一些数字
=> "df3615e8efc56f8a062ba1c2"
我不相信你想要的是可能的。
https://github.com/mperham/sidekiq/issues/3532
The return value will be GC'd like any other unused data in a Ruby process. Jobs do not have a "result" in Sidekiq and Sidekiq does nothing with the value.
您可能需要某种模型来跟踪您的后台任务。这是即兴的,但应该给你一个想法。
EG
# @attr result [Array]
# @attr status [String] Values of 'Pending', 'Error', 'Complete', etc..
class BackgroundTask < ActiveRecord
attr_accessor :product_codes
after_create :enqueue
def enqueue
::Imports::SyncProductsWorker.perform_async(product_codes, self.id)
end
end
def perform(list, id)
response = ::Imports::SynchronizeProducts.new(list).call
if (response.has_errors?)
BackgroundTask.find(id).update(status: 'Error', result: response)
else
BackgroundTask.find(id).update(status: 'Complete', result: response)
end
end
然后只需将 BackgroundTask 模型用于前端显示即可。