Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

shutdown waits for all work to finish processing instead of just wait…

…ing for the

queue to be drained before returning
  • Loading branch information...
commit 5e958b1938a37e8eaacff22fb855ce1cd7bd5f72 1 parent 3c96d20
@joenoon joenoon authored
Showing with 43 additions and 8 deletions.
  1. +16 −7 lib/girl_friday/work_queue.rb
  2. +27 −1 test/test_girl_friday_queue.rb
View
23 lib/girl_friday/work_queue.rb
@@ -76,6 +76,10 @@ def shutdown(&block)
@supervisor << Shutdown[block]
end
+ def working?
+ @busy_workers.size > 0 || @total_queued != @total_processed + @total_errors
+ end
+
private
def running?
@@ -174,13 +178,18 @@ def supervisor_loop
on_work(work)
end
f.when(Shutdown) do |stop|
- @shutdown = true
- @when_shutdown = stop.callback
- @busy_workers.each { |w| w << stop }
- ready_workers.each { |w| w << stop }
- shutdown_complete
- GirlFriday.remove_queue @weakref
- return
+ if !working?
+ @shutdown = true
+ @when_shutdown = stop.callback
+ @busy_workers.each { |w| w << stop }
+ ready_workers.each { |w| w << stop }
+ shutdown_complete
+ GirlFriday.remove_queue @weakref
+ return
+ else
+ Thread.pass
+ shutdown(&stop.callback)
+ end
end
f.when(Actor::DeadActorError) do |ex|
if running?
View
28 test/test_girl_friday_queue.rb
@@ -48,7 +48,7 @@ def test_should_handle_worker_error_with_retry
cb.call
end
ensure
- queue.push(0)
+ queue.push(0) unless msg == 0
end
end
queue.push(1)
@@ -189,6 +189,32 @@ def test_should_allow_graceful_shutdown
end
end
+ def test_should_allow_in_progress_work_to_finish
+ mutex = Mutex.new
+ total = 20
+ count = 0
+ incr = Proc.new do
+ mutex.synchronize do
+ count += 1
+ end
+ end
+
+ async_test(10) do |cb|
+ queue = GirlFriday::WorkQueue.new('finish', :size => 10) do |msg|
+ sleep 1
+ incr.call
+ end
+ total.times do
+ queue.push(:text => 'foo')
+ end
+
+ GirlFriday.shutdown!
+ assert_equal total, queue.instance_variable_get("@total_processed")
+ assert_equal total, count
+ cb.call
+ end
+ end
+
def test_should_create_workers_lazily
async_test do |cb|
queue = GirlFriday::Queue.new('lazy', :size => 2) do |msg|
Please sign in to comment.
Something went wrong with that request. Please try again.