【发布时间】:2017-06-22 02:13:45
【问题描述】:
用户可以通过控制器操作处理的 Web 界面在我的系统中上传多个文件。
def import
paths = params[:files].map(&:path) if params[:files].present?
if paths
@results = MyRecord.create_from paths
end
redirect_to confirmation_my_records_path
end
class MyRecord < ActiveRecord::Base
def self.create_from(paths)
paths.each do |path|
MyRecordWorker.perform path
end
end
end
works/my_record_worker.rb
class MyRecordWorker
include Sidekiq::Worker
def perform(path)
# each time this is run, it is no I/O, just expensive math calculations that take minutes
collection = ExpensiveOperation.new(path).run
if collection && collection.any?
save_records(collection)
else
[]
end
end
end
由于sidekiq作业将在后台运行,我如何通过确认页面通知用户作业已完成?我们不知道作业何时完成,因此当确认页面加载时,Rails 请求和响应周期无法确定任何内容。确认页面是否应该只显示“处理中...”,然后在作业完成时让 Faye 更新页面?
【问题讨论】:
-
这是使用网络套接字 (ActionCable) 的最佳时机。我从来没有使用过 Faye,但是是的,这也应该可以。
标签: ruby-on-rails ruby sidekiq