public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
macournoyer (author)
Sat Jan 12 09:13:11 -0800 2008
commit  2b60ea8c4c40f8626847d3a67279ec3c55e0b75a
tree    567e47a5ee1faed079f1b12a4ab4350ba9330435
parent  d39a4de1448b9a7de3e311789311158f6d22252c
thin / bin / thin
100755 119 lines (96 sloc) 4.037 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env ruby
# <tt>thin start</tt>: Starts the Rails app in the current directory.
# Run <tt>thin -h</tt> to get more usage.
require File.dirname(__FILE__) + '/../lib/thin'
require 'optparse'
 
COMMANDS = %w(start stop restart)
 
# Default options values
options = {
  :chdir => Dir.pwd,
  :environment => 'development',
  :address => '0.0.0.0',
  :port => 3000,
  :timeout => 60,
  :log => 'log/thin.log',
  :pid => 'tmp/pids/thin.pid',
  :servers => 1 # no cluster
}
 
# NOTE: If you add an option here make sure the key in the +options+ hash is the
# same as the name of the command line option.
# +option+ keys are use to build the command line to launch a cluster,
# see <tt>lib/thin/cluster.rb</tt>.
opts = OptionParser.new do |opts|
  opts.banner = "Usage: thin [options] #{COMMANDS.join('|')}"
 
  opts.separator ""
  opts.separator "Server options:"
 
  opts.on("-a", "--address HOST", "bind to HOST address (default: 0.0.0.0)") { |host| options[:address] = host }
  opts.on("-p", "--port PORT", "use PORT (default: 3000)") { |port| options[:port] = port.to_i }
  opts.on("-e", "--environment ENV", "Rails environment (default: development)") { |env| options[:environment] = env }
  opts.on("-c", "--chdir PATH", "Change to dir before starting") { |dir| options[:chdir] = File.expand_path(dir) }
  opts.on("-s", "--servers NUM", "Number of servers to start",
                                 "set a value >1 to start a cluster") { |num| options[:servers] = num.to_i }
  opts.on("-d", "--daemonize", "Run daemonized in the background") { options[:daemonize] = true }
  opts.on("-l", "--log FILE", "File to redirect output",
                              "(default: #{options[:log]})") { |file| options[:log] = file }
  opts.on("-P", "--pid FILE", "File to store PID",
                              "(default: #{options[:pid]})") { |file| options[:pid] = file }
  opts.on("-t", "--timeout SEC", "Request or command timeout in sec",
                                 "(default: #{options[:timeout]})") { |sec| options[:timeout] = sec.to_i }
  opts.on("-u", "--user NAME", "User to run daemon as (use with -g)") { |user| options[:user] = user }
  opts.on("-g", "--group NAME", "Group to run daemon as (use with -u)") { |group| 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") { puts opts; exit }
  opts.on_tail('-v', '--version', "Show version") { puts Thin::SERVER; exit }
 
  opts.parse! ARGV
end
 
 
# == Commands definitions
 
def cluster?(options)
  options[:servers] && options[:servers] > 1
end
 
def start(options)
  if cluster?(options)
    Thin::Cluster.new(options).start
  else
    server = Thin::Server.new(options[:address], options[:port])
  
    server.pid_file = options[:pid]
    server.log_file = options[:log]
    server.timeout = options[:timeout]
  
    if options[:daemonize]
      server.change_privilege options[:user], options[:group] if options[:user] && options[:group]
      server.daemonize
    end
  
    server.app = Rack::Adapter::Rails.new(options.merge(:root => options[:chdir]))
    server.start!
  end
end
 
def stop(options)
  if cluster?(options)
    Thin::Cluster.new(options).stop
  else
    Thin::Server.kill options[:pid], options[:timeout]
  end
end
 
def restart(options)
  if cluster?(options)
    Thin::Cluster.new(options).restart
  else
    # Restart only make sense when running as a daemon
    options.update :daemonize => true
  
    stop(options)
    start(options)
  end
end
 
 
# == Runs the command
 
Dir.chdir(options[:chdir])
command = ARGV[0]
 
if COMMANDS.include?(command)
  send(command, options)
elsif command.nil?
  puts "Command required"
  puts opts
  exit 1
else
  abort "Invalid command : #{command}"
end