我尝试使用Sucker Punch gem 来并行处理任务。但我没有找到如何等待其终止的文档。
require 'sucker_punch'
class SuckerJob
include SuckerPunch::Job
workers 4
def perform(event)
sleep(rand(5))
puts "[#{Thread.current.object_id}] End processing event #{event}."
end
end
10.times { |i| SuckerJob.perform_async(i) }
puts "Shutting down ..."
SuckerPunch::Queue.shutdown_all
puts "Shutdown finished, status: #{SuckerPunch::Queue.stats[SuckerJob.name]}"
# [Ugly] call internal method
SuckerPunch::Queue::QUEUES.fetch_or_store(SuckerJob.name).wait_for_termination(10)
puts "Wait finished, status: #{SuckerPunch::Queue.stats[SuckerJob.name]}"
似乎,SuckerPunch::Queue.shutdown_all()
在所有任务完成之前返回。
Shutting down ...
[17487240] End processing event 1.
[17488760] End processing event 0.
[17487240] End processing event 4.
[17488760] End processing event 5.
[17486120] End processing event 2.
[17484940] End processing event 3.
[17487240] End processing event 6.
Shutdown finished, status: {"workers"=>{"total"=>3, "busy"=>3, "idle"=>0}, "jobs"=>{"processed"=>7, "failed"=>0, "enqueued"=>0}}
[17484940] End processing event 9.
[17488760] End processing event 7.
[17486120] End processing event 8.
Wait finished, status: {"workers"=>{"total"=>0, "busy"=>0, "idle"=>0}, "jobs"=>{"processed"=>10, "failed"=>0, "enqueued"=>0}}
我怎样才能等到所有任务完成?