Google Groups Home
Help | Sign in
Thin cluster for Rails
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
stephen.celis@gmail.com  
View profile
(1 user)  More options Jan 5, 5:16 pm
From: "stephen.ce...@gmail.com" <stephen.ce...@gmail.com>
Date: Sat, 5 Jan 2008 14:16:16 -0800 (PST)
Local: Sat, Jan 5 2008 5:16 pm
Subject: Thin cluster for Rails
For those brave enough to start testing thin on a production or
staging server, I've prepared a quick rake task to make a thin
cluster.

To run it:

rake thin:cluster:start
rake thin:cluster:stop

For the start task, you can pass in the RAILS_ENV, the SIZE of the
cluster (default 4), and the starting PORT (default 3000 for
development, 8000 otherwise).

rake thin:cluster:start RAILS_ENV=production SIZE=10 PORT=8000

The code! (add to a .rake file in your lib/tasks directory):

namespace :thin do
  namespace :cluster do

    desc 'Start thin cluster'
    task :start => :environment do
      `cd #{RAILS_ROOT}`
      port_range = RAILS_ENV == 'development' ? 3 : 8
      (ENV['SIZE'] ? ENV['SIZE'].to_i : 4).times do |i|
        Thread.new do
          port = ENV['PORT'] ? ENV['PORT'].to_i + i : ("#{port_range}
%03d" % i)
          str  = "thin start -d -p#{port} -Ptmp/pids/thin-#{port}.pid"
          str += " -e#{RAILS_ENV}"
          puts "Starting server on port #{port}..."
          `#{str}`
        end
      end
    end

    desc 'Stop all thin clusters'
    task :stop => :environment do
      `cd #{RAILS_ROOT}`
      Dir.new("#{RAILS_ROOT}/tmp/pids").each do |file|
        Thread.new do
          if file.starts_with?("thin-")
            str  = "thin stop -Ptmp/pids/#{file}"
            puts "Stopping server on port #{file[/\d+/]}..."
            `#{str}`
          end
        end
      end
    end

  end
end


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
macournoyer  
View profile
 More options Jan 5, 5:28 pm
From: macournoyer <macourno...@gmail.com>
Date: Sat, 5 Jan 2008 14:28:04 -0800 (PST)
Local: Sat, Jan 5 2008 5:28 pm
Subject: Re: Thin cluster for Rails
hey stephen,

that's awesome! nice job!

I'd like to provide this built in the thin script soon. James proposed
that we use the god gem to monitor the servers rather then just start
and stop. I really like the idea :
http://groups.google.com/group/thin-ruby/browse_thread/thread/8812d6a...

Let me know what you think about this!

On Jan 5, 5:16 pm, "stephen.ce...@gmail.com" <stephen.ce...@gmail.com>
wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
stephen.celis@gmail.com  
View profile
 More options Jan 5, 5:42 pm
From: "stephen.ce...@gmail.com" <stephen.ce...@gmail.com>
Date: Sat, 5 Jan 2008 14:42:00 -0800 (PST)
Subject: Re: Thin cluster for Rails
I wasn't familiar with the god gem, but I like the idea. The
dependency shouldn't be a huge issue as long as it remains transparent
to the thin end user (and as long as documentation is plentiful!).

On Jan 5, 4:28 pm, macournoyer <macourno...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Golick  
View profile
 More options Jan 5, 6:15 pm
From: "James Golick" <jamesgol...@gmail.com>
Date: Sat, 5 Jan 2008 18:15:14 -0500
Local: Sat, Jan 5 2008 6:15 pm
Subject: Re: Thin cluster for Rails

Exactly - I mean, if people want to use monit (or even a different god
conf), or anything else, they still can.

On Jan 5, 2008 5:42 PM, stephen.ce...@gmail.com <stephen.ce...@gmail.com>
wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brian Tol  
View profile
(1 user)  More options Jan 6, 2:43 pm
From: Brian Tol <wirem...@gmail.com>
Date: Sun, 6 Jan 2008 11:43:41 -0800 (PST)
Local: Sun, Jan 6 2008 2:43 pm
Subject: Re: Thin cluster for Rails
Here's a 'thin_cluster' script that works a lot like mongrel_rails
cluster::*. It can use existing mongrel_cluster.yml files, in fact.
FWIW, I thought I'd pass it along. Your millage will vary.

#!/usr/bin/env ruby
# <tt>thin_cluter start</tt>: Starts the Rails app in the current
directory with a cluster of thins.
# Run <tt>thin_cluter -h</tt> to get more usage.
require File.dirname(__FILE__) + '/../lib/thin'
require 'optparse'

options = {
  'host'     => '0.0.0.0',
  'port'     => 3000,
  'env'      => ENV['RAILS_ENV'] || "development",
  'root'     => Dir.pwd,
  'log_file' => 'log/thin.log',
  'pid_file' => 'tmp/pids/thin.pid',
  'timeout'  => 60,
  'servers'  => 2

}

cli_options = {}

config_filename = 'config/thin_cluster.yml'

opts = OptionParser.new do |opts|
  opts.banner = "Usage: thin_cluster [options] start|stop|restart"

  opts.separator ""
  opts.separator "Cluster options:"

  opts.on("-C", "--config", "Config file to use",
                                  "(defaults to: #{config_filename})")
{ |user_config_filename|
    config_filename = user_config_filename if File.exist?
(user_config_filename)
  }

  opts.on("-o", "--host HOST", "listen on HOST (default:
0.0.0.0)")      { |host| cli_options['host'] = host }
  opts.on("-p", "--port PORT", "use PORT (default:
3000)")               { |port| cli_options['port'] = port }
  opts.on("-e", "--env ENV", "Rails environment (default:
development)") { |env| cli_options['env'] = env }
  opts.on("-c", "--chdir PATH", "listen on HOST (default: current
dir)") { |dir| cli_options['root'] = dir }
  #opts.on("-d", "--daemonize", "Run daemonized in the
background")       { cli_options['daemonize'] = true }
  opts.on("-l", "--log-file FILE", "File to redirect output",
                                   "(default:
#{options['log_file']})")   { |file| cli_options['log_file'] = file }
  opts.on("-P", "--pid-file FILE", "File to store PID",
                                   "(default:
#{options['pid_file']})")   { |file| cli_options['pid_file'] = file }
  opts.on("-t", "--timeout SEC", "Request or command timeout in
sec",
                                 "(default:
#{options['timeout']})")      { |sec| cli_options['timeout'] = sec }
  opts.on("-u", "--user NAME", "User to run daemon as (use with -
g)")    { |user| cli_options['user'] = user }
  opts.on("-g", "--group NAME", "Group to run daemon as (use with -
u)")  { |group| cli_options['group'] = group }

  opts.separator ""
  opts.separator "Common options:"

  opts.on_tail("-D", "--debug", "Set debbuging on") { $DEBUG = true }

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end

  opts.on_tail('-v', '--version', "Show version") do
    puts Thin::SERVER
    exit
  end

  opts.parse! ARGV
end

if File.exist?(config_filename)
  config_file_options = YAML.load_file(config_filename)
  options.merge! config_file_options if config_file_options
end

options.merge! cli_options

def start_cluster(options)
  port = options["port"].to_i - 1
  pid  = options["pid_file"].split(".")

  puts "Starting #{options["servers"]} Thin servers..."

  1.upto(options['servers'].to_i) do |i|
    argv = [ 'thin' ]
    argv << "start"
    argv << "-d"
    argv << "-e #{options["environment"]}" if options["environment"]
    argv << "-p #{port+i}"
    argv << "-o #{options["host"]}"  if options["host"]
    argv << "-l #{options["log_file"]}" if options["log_file"]
    argv << "-P #{pid[0]}.#{port+i}.#{pid[1]}"
    argv << "-c #{options["root"]}" if options["root"]
    argv << "-t #{options["timeout"]}" if options["timeout"]
    #argv << "-r #{options["root"]}" if options["root"]
    #argv << "-n #{options["num_procs"]}" if options["num_procs"]
    #argv << "-B" if options["debug"]
    argv << "--user #{options["user"]}" if options["user"]
    argv << "--group #{options["group"]}" if options["group"]
    cmd = argv.join " "

    puts cmd if $DEBUG
    output = `#{cmd}`
    unless $?.success?
      puts cmd unless $DEBUG
      puts output
    end
  end

end

def stop_cluster(options)
  port = options["port"].to_i - 1
  pid  = options["pid_file"].split(".")

  puts "Stopping #{options["servers"]} Thin servers..."

  1.upto(options['servers'].to_i) do |i|
    argv = [ "thin" ]
    argv << "stop"
    argv << "-P #{pid[0]}.#{port+i}.#{pid[1]}"
    argv << "-c #{options["root"]}" if options["root"]
    cmd = argv.join " "
    puts cmd if $DEBUG
    output = `#{cmd}`
    unless $?.success?
      puts cmd unless $DEBUG
      puts output
    end
  end

end

case ARGV[0]

when 'start'
  start_cluster(options)

when 'stop'
  stop_cluster(options)

when 'restart'
  stop_cluster(options)
  start_cluster(options)

when nil
  puts "Command required"
  puts opts
  exit 1

else
  abort "Invalid command : #{ARGV[0]}"

end


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jag  
View profile
 More options Feb 29, 5:05 am
From: jag <indieh...@gmail.com>
Date: Fri, 29 Feb 2008 02:05:28 -0800 (PST)
Local: Fri, Feb 29 2008 5:05 am
Subject: Re: Thin cluster for Rails
Excellent work Stephen!

posted the script + instructions on my blog as i'm playing with THIN
for a new project, credited your name.

Let me know if you want it linking to your site.

Sincerely,

John

On Jan 6, 7:43 pm, Brian Tol <wirem...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jag  
View profile
 More options Feb 29, 5:06 am
From: jag <indieh...@gmail.com>
Date: Fri, 29 Feb 2008 02:06:51 -0800 (PST)
Local: Fri, Feb 29 2008 5:06 am
Subject: Re: Thin cluster for Rails
blog by the way, is at...

http://www.red91.com

and your article is at...

http://www.red91.com/articles/2008/02/27/mongrel-on-a-diet-fedora-cle...

Again, good work!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
macournoyer  
View profile
 More options Feb 29, 8:17 am
From: macournoyer <macourno...@gmail.com>
Date: Fri, 29 Feb 2008 05:17:38 -0800 (PST)
Local: Fri, Feb 29 2008 8:17 am
Subject: Re: Thin cluster for Rails
That's very old stuff jag (one month old, aye!) :)

Thin now has builtin cluster support throught the --servers option:

thin start -s3

to start a cluster of 3 servers

On Feb 29, 5:06 am, jag <indieh...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jag  
View profile
 More options Apr 11, 8:43 am
From: jag <indieh...@gmail.com>
Date: Fri, 11 Apr 2008 05:43:34 -0700 (PDT)
Local: Fri, Apr 11 2008 8:43 am
Subject: Re: Thin cluster for Rails
thanks macournoyor, gathered that after i played around with the
options some more.

to update, i've moved all my rails projects over to thin + nginx and
they're handling the load much better; I was getting worried for a
moment with scaling issues whether or not to move over to merb but i
was thankful to find this lighter web server.

all the best,

John.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
wuz  
View profile
 More options Apr 13, 10:40 pm
From: wuz <wzl2...@gmail.com>
Date: Sun, 13 Apr 2008 19:40:37 -0700 (PDT)
Local: Sun, Apr 13 2008 10:40 pm
Subject: Re: Thin cluster for Rails