brandonhilkert / sucker_punch

Sucker Punch is a Ruby asynchronous processing library using concurrent-ruby, heavily influenced by Sidekiq and girl_friday.
MIT License
2.64k stars 114 forks source link

Better shutdown handling. #148

Closed jdantonio closed 8 years ago

jdantonio commented 8 years ago

Better shutdown handling. See #144, #146, and #147.

jdantonio commented 8 years ago

This update moves more functionality into the Queue class. A Queue is now a full Concurrent::Executor service with the associated lifecycle methods. Most of the attribute methods are simply delegated to the underlying ThreadPoolExecutor using Ruby's Forwardable module. The remaining methods are implemented according to Sucker Punch's specific needs.

The "soft" shutdown is now handled by the Job class. When it sees that the application is shutting down it ignores any remaining jobs. The thread pool will still try to execute every job in its queue, but the aforementioned short-circuit renders all enqueued jobs effectively noop.

Finally, the default shutdown handler posts a final "tracking" job to every queue on shutdown. This job simply counts do a Concurrent::CountDownLatch given to it by the handler. Because the thread pool still runs every job in queue, these tracking jobs still get run. Because these jobs get post directly to the pool, they skip the shirt-circuit of Job. This allows the shutdown handler to wait on all queues with one aggregate timeout rather than iterating over each and calling wait_for_termination.

brandonhilkert commented 8 years ago

This is great stuff. Should've thought to use Concurrent::CountDownLatch in the shutdown. It's been a great tool for testing. Thanks so much for the contribution. I'm really excited about getting a beta out there!

openface commented 8 years ago

Nice work here!