Sinatra restarts Webrick server after ctrl-c

require 'sinatra' require 'rubygems' class TestServer < Sinatra::Application set :port, 22340 get '/' do "Hello World" end run! if app_file == $0 end 

Very simple application with Ruby 2.0.0-p0 and Sinatra 1.4.2

When I ctrl-c, the webrick server restarts on the default port ... see output below

 LM-BOS-00715009:server joshughes$ ruby test.rb [2013-04-19 16:07:48] INFO WEBrick 1.3.1 [2013-04-19 16:07:48] INFO ruby 2.0.0 (2013-02-24) [x86_64-darwin11.4.2] == Sinatra/1.4.2 has taken the stage on 22340 for development with backup from WEBrick [2013-04-19 16:07:48] INFO WEBrick::HTTPServer#start: pid=63798 port=22340 ^C == Sinatra has ended his set (crowd applauds) [2013-04-19 16:07:56] INFO going to shutdown ... [2013-04-19 16:07:56] INFO WEBrick::HTTPServer#start done. [2013-04-19 16:07:56] INFO WEBrick 1.3.1 [2013-04-19 16:07:56] INFO ruby 2.0.0 (2013-02-24) [x86_64-darwin11.4.2] == Sinatra/1.4.2 has taken the stage on 4567 for development with backup from WEBrick [2013-04-19 16:07:56] INFO WEBrick::HTTPServer#start: pid=63798 port=4567 ^C 

Can someone help me on what might be wrong here?

+4
source share
1 answer

The problem is that you are not using the Sinatra modular style correctly. Instead of requiring sinatra and inheriting from Sinatra::Application , you need sinatra/base and inheriting from Sinatra::Base .

What's happening? You need a simple sinatra , which in turn requires sinatra/main . This file adds the at_exit handler, which launches the embedded server (unless you disable it). However, you also explicitly call run! into your own code, so the server starts because of your call, and then when you exit, the at_exit handler starts the server again. The sinatra/base request does not start the embedded server on exit, so you only have your own explicit run! call run! .

 require 'sinatra/base' # change here require 'rubygems' class TestServer < Sinatra::Base # and here set :port, 22340 get '/' do "Hello World" end run! if app_file == $0 end 
+6
source

All Articles