问题
I have a modular Sinatra web app running using Thin, with EventMachine running additional tasks.
It works, but there's something a bit odd about the webserver: Any requests, whether successful or 404s, don't appear in the log output from Thin/Sinatra. And when I cancel the process, the server ends twice.
Here's the rough, basic structure of the app:
Procfile:
web: ruby app.rb
app.rb:
require 'thin'
require 'eventmachine'
require 'app/frontend'
EM.run do
# Start some background tasks here...
EM.add_periodic_timer(1200) do
# Do a repeating task here...
end
App::Frontend.run!
end
app/frontend.rb:
require 'sinatra/base'
module App
class Frontend < Sinatra::Base
get '/' do
# Display page
end
# etc
end
end
When I do foreman start
then I get:
16:50:00 web.1 | started with pid 76423
16:50:01 web.1 | [messages about EventMachine background tasks starting]
16:50:01 web.1 | == Sinatra/1.4.3 has taken the stage on 5000 for development with backup from Thin
16:50:01 web.1 | >> Thin web server (v1.5.1 codename Straight Razor)
16:50:01 web.1 | >> Maximum connections set to 1024
16:50:01 web.1 | >> Listening on 0.0.0.0:5000, CTRL+C to stop
Nothing more is output when I request existing web pages (which load OK) or not-existing web pages. When I cancel the process I get:
^CSIGINT received
16:50:08 system | sending SIGTERM to all processes
SIGTERM received
16:50:08 web.1 | >> Stopping ...
16:50:08 web.1 | == Sinatra has ended his set (crowd applauds)
16:50:08 web.1 | >> Stopping ...
16:50:08 web.1 | == Sinatra has ended his set (crowd applauds)
16:50:08 web.1 | exited with code 0
That Sinatra finishes twice makes me think I'm somehow running it twice, and the one that's serving the web pages isn't being logged... but I don't know how I'm managing this!
回答1:
The docs on Modular vs. Classic style mention that there are some changes to the default settings, and one of these is logging, which is turned off by default.
Adding settings.logging = true
to the top of class Frontend < Sinatra::Base
gives me a log in my terminal window for the localhost:5000 requests.
I don't think the second issue is that it's creating two processes, but rather that it's killing and re-starting the process right before closing the server. This can be solved by following the Sinatra recipe for using EventMachine with Sinatra, which is a little more complicated than what you've done. Here's their code, modified to fit your app:
The new app.rb
:
EM.run do
server = 'thin'
host = '0.0.0.0'
port = ENV['PORT'] || '8181'
web_app = App::Frontend.new
# Start some background tasks here...
EM.add_periodic_timer(1200) do
# Do a repeating task here...
end
dispatch = Rack::Builder.app do
map '/' do
run web_app
end
end
Rack::Server.start({
app: dispatch,
server: server,
Host: host,
Port: port
})
end
(original source, from Sinatra Recipes)
The use of ENV['PORT']
in the app.rb lets you use multiple instances in foreman (e.g., foreman start -p 4000 -c web=2
, which will run services on ports 4000 and 4001). And both of them appear in the log!
Hope that helps.
来源:https://stackoverflow.com/questions/18516053/running-a-modular-sinatra-app-with-thin-and-eventmachine-it-starts-twice