public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Search Repo:
Fix IOError when writing to logger when starting server as a daemon.
macournoyer (author)
Wed Jan 09 21:46:54 -0800 2008
commit  1a2268550c691256a40f8ab1f80a052069fdce9c
tree    edc47b0a1a44e7969edfb658d30b75b916f289cc
parent  e6ff51a05d896b34bd5cc3b2de315f22ab524635
...
1
 
 
 
2
3
4
...
1
2
3
4
5
6
7
0
@@ -1,4 +1,7 @@
0
 == 0.5.2 Cheezburger release
0
+ * Fix IOError when writing to logger when starting server as a daemon.
0
+ * Really change directory when the -c option is specified.
0
+ * Add restart command to thin script.
0
  * Fix typo in thin script usage message and expand chdir path.
0
  * Rename thin script options to be the same as mongrel_rails script:
0
    -o --host => -a --address
...
4
5
6
 
 
7
8
9
...
15
16
17
18
 
19
20
21
22
...
52
53
54
55
 
 
 
 
 
56
57
58
59
60
61
62
63
64
65
66
67
68
69
...
66
67
68
 
 
69
70
 
71
72
 
73
 
74
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
77
78
79
 
80
81
82
 
83
...
4
5
6
7
8
9
10
11
...
17
18
19
 
20
21
22
23
24
...
54
55
56
 
57
58
59
60
61
62
 
 
 
 
63
64
65
66
67
68
69
70
71
...
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
0
@@ -4,6 +4,8 @@
0
 require File.dirname(__FILE__) + '/../lib/thin'
0
 require 'optparse'
0
 
0
+COMMANDS = %w(start stop restart)
0
+
0
 options = {
0
   :root => Dir.pwd,
0
   :env => 'development',
0
@@ -15,7 +17,7 @@
0
 }
0
 
0
 opts = OptionParser.new do |opts|
0
- opts.banner = "Usage: thin [options] start|stop"
0
+ opts.banner = "Usage: thin [options] #{COMMANDS.join('|')}"
0
 
0
   opts.separator ""
0
   opts.separator "Server options:"
0
0
@@ -52,12 +54,12 @@
0
   opts.parse! ARGV
0
 end
0
 
0
-case ARGV[0]
0
+
0
+# Commands definitions
0
+
0
+def start(options)
0
+ server = Thin::Server.new(options[:host], options[:port])
0
   
0
-when 'start'
0
- app = Rack::Adapter::Rails.new(options)
0
- server = Thin::Server.new(options[:host], options[:port], app)
0
-
0
   server.pid_file = options[:pid_file]
0
   server.log_file = options[:log_file]
0
   server.timeout = options[:timeout]
0
0
0
0
0
0
0
@@ -66,19 +68,37 @@
0
     server.change_privilege options[:user], options[:group] if options[:user] && options[:group]
0
     server.daemonize
0
   end
0
+
0
+ server.app = Rack::Adapter::Rails.new(options)
0
 
0
   server.start!
0
+end
0
 
0
-when 'stop'
0
+def stop(options)
0
   Thin::Server.kill options[:pid_file], options[:timeout]
0
+end
0
 
0
-when nil
0
+def restart(options)
0
+ # Restart only make sense when running as a daemon
0
+ options.update :daemonize => true
0
+
0
+ stop(options)
0
+ start(options)
0
+end
0
+
0
+
0
+# Runs the command
0
+
0
+Dir.chdir(options[:root])
0
+command = ARGV[0]
0
+
0
+if COMMANDS.include?(command)
0
+ send(command, options)
0
+elsif command.nil?
0
   puts "Command required"
0
   puts opts
0
- exit 1
0
-
0
+ exit 1
0
 else
0
- abort "Invalid command : #{ARGV[0]}"
0
-
0
+ abort "Invalid command : #{command}"
0
 end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,73 @@
0
+# WARNING: this config file has not been tested yet.
0
+# If you know God better then I, feel free to tweak it and send it to me.
0
+# Thanks!
0
+# -- Marc
0
+
0
+RAILS_ROOT = "/Users/marc/projects/refactormycode"
0
+
0
+God.watch do |w|
0
+ w.name = "thin-3000"
0
+ w.group = 'thins'
0
+ w.interval = 5.seconds # default
0
+ w.start = "thin start -c #{RAILS_ROOT} -P #{RAILS_ROOT}/tmp/pids/thin.3000.pid -p 3000 -d"
0
+ w.stop = "thin stop -P #{RAILS_ROOT}/tmp/pids/thin.3000.pid"
0
+ w.restart = "thin restart -P #{RAILS_ROOT}/tmp/pids/thin.3000.pid -p 3000"
0
+ w.pid_file = File.join(RAILS_ROOT, "tmp/pids/thin.3000.pid")
0
+
0
+ # clean pid files before start if necessary
0
+ w.behavior(:clean_pid_file)
0
+
0
+ # determine the state on startup
0
+ w.transition(:init, { true => :up, false => :start }) do |on|
0
+ on.condition(:process_running) do |c|
0
+ c.running = true
0
+ end
0
+ end
0
+
0
+ # determine when process has finished starting
0
+ w.transition([:start, :restart], :up) do |on|
0
+ on.condition(:process_running) do |c|
0
+ c.running = true
0
+ end
0
+
0
+ # failsafe
0
+ on.condition(:tries) do |c|
0
+ c.times = 5
0
+ c.transition = :start
0
+ end
0
+ end
0
+
0
+ # start if process is not running
0
+ w.transition(:up, :start) do |on|
0
+ on.condition(:process_exits)
0
+ end
0
+
0
+ # restart if memory or cpu is too high
0
+ w.transition(:up, :restart) do |on|
0
+ on.condition(:memory_usage) do |c|
0
+ c.interval = 20
0
+ c.above = 50.megabytes
0
+ c.times = [3, 5]
0
+ end
0
+
0
+ on.condition(:cpu_usage) do |c|
0
+ c.interval = 10
0
+ c.above = 10.percent
0
+ c.times = [3, 5]
0
+ end
0
+ end
0
+
0
+ # lifecycle
0
+ w.lifecycle do |on|
0
+ on.condition(:flapping) do |c|
0
+ c.to_state = [:start, :restart]
0
+ c.times = 5
0
+ c.within = 5.minute
0
+ c.transition = :unmonitored
0
+ c.retry_in = 10.minutes
0
+ c.retry_times = 5
0
+ c.retry_within = 2.hours
0
+ end
0
+ end
0
+end
...
23
24
25
26
 
27
28
29
...
32
33
34
 
 
35
36
37
...
23
24
25
 
26
27
28
29
...
32
33
34
35
36
37
38
39
0
@@ -23,7 +23,7 @@
0
     
0
     # Creates a new server binded to <tt>host:port</tt>
0
     # that will pass request to +app+.
0
- def initialize(host, port, app)
0
+ def initialize(host, port, app=nil)
0
       @host = host
0
       @port = port.to_i
0
       @app = app
0
@@ -32,6 +32,8 @@
0
     
0
     # Starts the handlers.
0
     def start
0
+ raise ArgumentError, "app required" unless @app
0
+
0
       log ">> Thin web server (v#{VERSION::STRING} codename #{VERSION::CODENAME})"
0
       trace ">> Tracing ON"
0
     end

Comments

    No one has commented yet.