Exiting a Sinatra App from Within the App

1,106 views
Skip to first unread message

Keith Bennett

unread,
Sep 25, 2013, 10:19:45 AM9/25/13
to sina...@googlegroups.com
I'd like to know how to programmatically exit a Sinatra app *from within the app itself*.

I want to use Sinatra as a means of receiving control and configuration commands while my app 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?

-- Keith

Carlos Eduardo L. Lopes

unread,
Sep 26, 2013, 8:03:28 PM9/26/13
to sina...@googlegroups.com
I think this is not possible, and using exit will not work because it raises a SystemExit exception and the exceptions raised inside routes are handled by sinatra.

Maybe you can kill the process, but this will be really ugly.
--
Carlos Eduardo L. Lopes
gtalk: carlos....@gmail.com | Twitter: http://twitter.com/_carloslopes
GitHub: http://github.com/carloslopes



September 25, 2013 11:19 AM
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

George Timoschenko

unread,
Sep 27, 2013, 8:47:57 PM9/27/13
to sina...@googlegroups.com

I'd like to know how to programmatically exit a Sinatra app *from within the app itself*.

Not a problem!

In the newest Sinatra from the master branch you do it like that:

require 'sinatra'

get '/' do
  Sinatra::Application.quit!
end

Please note that it is not released yet. This feature will be avaliable in Sinatra 1.5.0.
So if you want to use most recent Sinatra version with this feature right now, use this declaration in your Gemfile:

gem 'sinatra', :github => 'sinatra/sinatra'

Build great apps!
github.com/somu

AR (aka AleiPhoenix)

unread,
Sep 26, 2013, 11:35:38 PM9/26/13
to sina...@googlegroups.com
On Fri, Sep 27, 2013 at 8:03 AM, Carlos Eduardo L. Lopes <carlos....@gmail.com> wrote:
I think this is not possible, and using exit will not work because it raises a SystemExit exception and the exceptions raised inside routes are handled by sinatra.

Maybe you can kill the process, but this will be really ugly.

--
Silence is golden.

twitter: @AccelReality
wikipedia: AleiPhoenix
blog: weblog.areverie.org
wiki: wiki.areverie.org

Reply all
Reply to author
Forward
0 new messages