A user can upload multiple files in my system via a web interface that is handled by a controller action.
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
Because the sidekiq jobs will run in the background, how do I notify the user through the confirmation page that the jobs are done? We don't know when the job will finish and therefore the Rails request and response cycle cannot determine anything when the confirmation page is loaded. Should the confirmation page just say "Processing..." and then have Faye update the page when the job is finished?