Is there a way Rails 3.0.x can default to using Thin?
Asked Answered
D

4

13

I run the Thin webserver for basically every app in my dev/test environments. When I used Mongrel with Rails 2.x, all I had to type was script/server to get it to run the webserver I choose. But with Rails 3, I have to specify Thin every time. Is there a way to get Thin running on my Rails apps by just typing rails s instead of rails s thin?

Deltoro answered 31/1, 2011 at 16:33 Comment(0)
H
10

As of Rails 3.2rc2, thin is now run by default on invoking rails server when gem 'thin' is in your Gemfile! Thanks to this pull request: https://github.com/rack/rack/commit/b487f02b13f42c5933aa42193ed4e1c0b90382d7

Works great for me.

Hymenopterous answered 2/3, 2012 at 18:8 Comment(0)
C
21

Yeah it's possible to do this.

The way the rails s command works at the end of the day is by falling through to Rack and letting it pick the server. By default the Rack handler will try to use mongrel and if it can't find mongrel it will go with webrick. All we have to do is patch the handler slightly. We'll need to insert our patch into the rails script itself. Here is what you do, crack open your script/rails file. By default it should look like this:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rails/commands'

We insert our patch right before the require 'rails/commands' line. Our new file should look like this:

#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rack/handler'
Rack::Handler.class_eval do
  def self.default(options = {})
    # Guess.
    if ENV.include?("PHP_FCGI_CHILDREN")
      # We already speak FastCGI
      options.delete :File
      options.delete :Port

      Rack::Handler::FastCGI
    elsif ENV.include?("REQUEST_METHOD")
      Rack::Handler::CGI
    else
      begin
        Rack::Handler::Mongrel
      rescue LoadError
        begin
          Rack::Handler::Thin
        rescue LoadError
          Rack::Handler::WEBrick
        end
      end
    end
  end
end
require 'rails/commands'

Notice that it will now try Mongrel and if there is an error try for Thin and only then go with Webrick. Now when you type rails s we get the behaviour we're after.

Carboloy answered 13/8, 2011 at 8:52 Comment(0)
H
10

As of Rails 3.2rc2, thin is now run by default on invoking rails server when gem 'thin' is in your Gemfile! Thanks to this pull request: https://github.com/rack/rack/commit/b487f02b13f42c5933aa42193ed4e1c0b90382d7

Works great for me.

Hymenopterous answered 2/3, 2012 at 18:8 Comment(0)
G
1

In script/rails the following works as well:

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)

require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler::Thin

require 'rails/commands'
Guild answered 13/2, 2012 at 15:15 Comment(0)
S
0

Simply install thin, cd to the directory that your app is in and run thin start. Works perfectly here. :)

You can use http://www.softiesonrails.com/2008/4/27/using-thin-instead-of-mongrel to change as needed. (Its the one I used)

Starling answered 31/1, 2011 at 18:29 Comment(2)
As an update, thin -V start works to mimic the behaviour you usually see when you start the rails server, aka where you see the output in the terminal from each connection.Starling
that's cool. but there's nothing that makes rails s run thin start?Deltoro

© 2022 - 2024 — McMap. All rights reserved.