Why do I get an error on the first request after restarting a Sinatra app with Rack and Phusion Passenger?
Question
After I touch tmp/restart.txt and my app restarts, the first request throws an error, usually something about not finding Haml::Engine or some other gem. The second and subsequent requests all work fine. I was having this problem on Dreamhost which was running Rack 0.4.1 and Sinatra 0.3.3 but after moving to my own host and running a newer Rack (0.9.1) and Sinatra (0.9.0.4) I still see the problem.
If you don't know the exact answer but have tips on how I could track it down, please let me know.
Here's my config.ru:
require 'rubygems'
require 'sinatra'
disable :run
set :environment, :production
set :raise_errors, true
require 'app.rb'
run Sinatra::Application
Solution
I'd look into making sure you're requiring all the necessary gems. Perhaps there's something about the order you're doing it that's causing it to fail the first time. Are you requiring rubygems
?
It might also help if you posted your rack configuration for the app (config.ru
).
OTHER TIPS
The error is caused by the Sinatra gem loading too late. This is the solution:
#config.ru ENV['GEM_HOME'] ||= `gem env path`.strip.split(':').first ENV['GEM_PATH'] ||= `gem env path`.strip Gem.clear_paths require 'application-filename-goes-here' set :environment, :production run Sinatra::Application