Permalink
Browse files

handle shutting down state more gracefully. was previously waiting

for the queue to drain, now lets work fully complete, but lets new work
sit in the persister
  • Loading branch information...
1 parent 5e958b1 commit 792c93ccaa34d65bec923f16777f653fa714f7ce @joenoon joenoon committed Jul 15, 2012
Showing with 11 additions and 6 deletions.
  1. +9 −4 lib/girl_friday/work_queue.rb
  2. +2 −2 test/test_girl_friday_queue.rb
View
13 lib/girl_friday/work_queue.rb
@@ -14,6 +14,7 @@ def initialize(name, options={}, &block)
@error_handlers = (Array(options[:error_handler] || ErrorHandler.default)).map(&:new)
@shutdown = false
+ @shutting_down = false
@busy_workers = []
@ready_workers = nil
@created_at = Time.now.to_i
@@ -77,7 +78,7 @@ def shutdown(&block)
end
def working?
- @busy_workers.size > 0 || @total_queued != @total_processed + @total_errors
+ @busy_workers.size > 0 || @total_queued != @total_processed + @total_errors + @persister.size
end
private
@@ -93,7 +94,7 @@ def handle_error(ex)
def on_ready(who)
@total_processed += 1
- if running? && work = @persister.pop
+ if !shutting_down? && running? && work = @persister.pop
who.this << work
drain
else
@@ -112,10 +113,13 @@ def shutdown_complete
end
end
+ def shutting_down?
+ !!@shutting_down
+ end
+
def on_work(work)
@total_queued += 1
-
- if running? && worker = ready_workers.pop
+ if !shutting_down? && running? && worker = ready_workers.pop
@busy_workers << worker
worker << work
drain
@@ -178,6 +182,7 @@ def supervisor_loop
on_work(work)
end
f.when(Shutdown) do |stop|
+ @shutting_down = true
if !working?
@shutdown = true
@when_shutdown = stop.callback
View
4 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) unless msg == 0
+ queue.push(0)
end
end
queue.push(1)
@@ -191,7 +191,7 @@ def test_should_allow_graceful_shutdown
def test_should_allow_in_progress_work_to_finish
mutex = Mutex.new
- total = 20
+ total = 8
count = 0
incr = Proc.new do
mutex.synchronize do

0 comments on commit 792c93c

Please sign in to comment.