I'd like to know how to programmatically exit a Sinatra application from within the application itself. (There's another SO thread about stopping it from outside the app.)
I want to use Sinatra as a means of receiving control and configuration commands while my application does something unrelated to the Sinatra stuff. I'd like one of the control commands to be 'exit'. Ruby's 'exit' method just seems to result in Sinatra recovering and resuming. I found this in base.rb that I think confirms this:
at_exit { Application.run! if $!.nil? && Application.run? }
So far, the only way I've found is to call Ruby's exit!
method, but that bypasses exit
hooks and is not a very clean solution.
Is there no way to programmatically tell Sinatra to stop?
I used the following code:
# Exit 'correctly'
get '/exit' do
# /exit causes:
# 15:20:24 web.1 | Damn!
# 15:20:24 web.1 | exited with code 1
Process.kill('TERM', Process.pid)
end
# Just terminate
get '/fail' do
Process.kill('KILL', Process.pid)
end
Take a look at at_exit
in config.ru it works for TERM signal:
at_exit do
puts 'Damn!'
exit false
end
Full example is here.
Cheers.