My Own Damn Fault: Sinatra::Base.run! != Rack::Builder#run
March 14, 2011
Ever had one of those problems that makes you feel like a complete idiot? You tinker and tinker and it’s still there. You recreate an entire configuration file from scratch trying to figure out the exact point where everything goes haywire.
Sorry, I should rephrase that first sentence: the solution makes you feel like an idiot.
Often the culprit is a misspelled variable or filename (which looked right every one of the last hundred times you looked at it!). The worst is when you realize you’ve been looking at cached content the whole time or the system has been running a stale version of your code for some reason.
Facepalm.
We prideful IT-types usually move onto the next problem grumbling excuses and muttering curses, but not me! Not this time! I will help my fellow human beings who are desperately Googling for a solution!
So here’s what happened to me recently. I’ve been hacking on Github’s wonderful Gollum git-based wiki. As it is, Gollum is setup to run using a `bin/gollum` script. Nice for someone poking around with the project, but I wanted something that worked nicely with Capistrano and Passenger.
I extracted what I thought was a reasonable config.ru from the bin/gollum script:
require 'rubygems'
require 'bundler'
Bundler.require(:default, ENV['RACK_ENV'].to_sym)
options = {}
wiki_options = {}
require 'gollum/frontend/app'
Precious::App.set(:gollum_path, "wiki_data.git")
Precious::App.set(:wiki_options, wiki_options)
Precious::App.run! # <- SPOILER ALERT: this is what's wrong
What happened when I started this up? The damnedest thing: Passenger was trying to spin up WEBrick!
[Mon Mar 14 07:16:00 2011] [notice] Apache/2.2.16 (Ubuntu) Phusion_Passenger/3.0.5 configured -- resuming normal operations
[2011-03-14 07:16:07] INFO WEBrick 1.3.1
[2011-03-14 07:16:07] INFO ruby 1.9.2 (2011-02-18) [x86_64-linux]
[2011-03-14 07:16:07] INFO WEBrick::HTTPServer#start: pid=30305 port=4567
What’s going on here? Is it a problem with Passenger? Apache (also tried Nginx)? Rack? Sinatra? Gollum? So many components!
Well, of course it’s my fault. You see, here’s the documentation for `Sinatra::Base.run!` (`Sinatra::Base` is, of course, the superclass of `Precious::App`, Gollum’s default frontend):
Run the Sinatra app as a self-hosted server using Thin, Mongrel or WEBrick (in that order)
So, it turns out `run!` is not just a convenience method for Rack::Builder#run… It actually tries to spin up a self-hosted server. Which is how I ended up in the bizarre situation of Passenger trying to start another server on port 4567.
Duh.
For reference, here’s the working config.ru:
require 'rubygems'
require 'bundler'
Bundler.require(:default, ENV['RACK_ENV'].to_sym)
require 'gollum/frontend/app'
Precious::App.set(:gollum_path, "wiki_data.git")
Precious::App.set(:wiki_options, {})
run Precious::App
I tried this, but got:
Error message: Could not locate Gemfile (Bundler::GemfileNotFound)
on this line:
Bundler.require(:default, ENV['RACK_ENV'].to_sym)
Any ideas?
Hi Marc,
First question would be: do you have a Gemfile in the working directory for your project?
In my case, I had a Gemfile to handle installing Gollum (and some other markup libraries) and Capistrano, so it made sense to use Bundler, but that’s not the only way. You could probably just as easily just require ‘gollum’ if that suits your needs.
False alarm, I didn’t need bundler having installed the gems. So this worked for me:
require 'gollum/frontend/app'
Precious::App.set(:gollum_path, "/var/www/gollum/lib/public")
Precious::App.set(:wiki_options, {})
run Precious::App
Thanks for doing the groundwork, though.