我尝试使用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}}
如何等待所有任务完成?
SuckerPunch::Counter::Processed.new(@queue).value
。 - Rajarshi Dasall_stats = SuckerPunch::Queue.stats
然后stats = all_stats[MyJob.to_s]
,然后您现在可以查看stats["jobs"]["processed"]
,现在有total
、processed
、busy
等。 - Rajarshi Das