garbagecollector
garbagecollector

Reputation: 3880

How to do capture the termination of a rack app

I have a Sinatra app running, and there's some background threads running using Celluloid. When Ctrl+C is pressed, I want to cleanly terminate those running agents. How do I cleanly terminate them?

Upvotes: 0

Views: 84

Answers (1)

Chris Heald
Chris Heald

Reputation: 62668

To answer your question directly, your main thread will block while Sinatra is running; once it catches SIGINT, it'll break out of its runloop, and your script will continue. So, you can have a config.ru like:

require './my_app'
run MyApp
Celluloid.shutdown

run is going to block until the server is terminated, at which point Celluloid should start its shutdown mechanism.

However, that said, it's worth noting that Celluloid will invoke #shutdown automatically via an at_exit block. (https://github.com/celluloid/celluloid/blob/master/lib/celluloid.rb#L138-L154), and Celluloid already provides a mechanism for running code when an actor exits, so if you've defined finalizers for your actors, they should shut down cleanly without you having to do anything special WRT Sinatra.

Upvotes: 1

Related Questions