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:
* thin restart now sends HUP signals rather then stopping & starting.
* HUP signal now launches a new process with the same options.
macournoyer (author)
Mon Jan 28 21:22:59 -0800 2008
commit  546e6a9e461b4f1017e3d01ea7809529f08402f8
tree    df1f68089bbbe4901947a2963452ec2dfae5e1cd
parent  f3417196f32db106f44feecf6bf2b77621c5b994
...
1
 
 
2
3
4
...
1
2
3
4
5
6
0
@@ -1,4 +1,6 @@
0
 == 0.6.2 Rambo release
0
+ * thin restart now sends HUP signals rather then stopping & starting.
0
+ * HUP signal now launches a new process with the same options.
0
  * Add PID and more info from the last request to the Stats adapter
0
    mostly taken from Rack::ShowException.
0
  * pid and log files in cluster are no longer required to be relative to the
...
112
113
114
 
 
 
115
116
117
...
122
123
124
125
 
126
127
128
...
132
133
134
135
136
137
138
139
 
140
141
142
...
112
113
114
115
116
117
118
119
120
...
125
126
127
 
128
129
130
131
...
135
136
137
 
 
 
 
 
138
139
140
141
0
@@ -112,6 +112,9 @@ def start(options)
0
     # If a stats are required, wrap in Stats adapter
0
     server.app = Thin::Stats::Adapter.new(server.app, options[:stats]) if options[:stats]
0
     
0
+ # Register restart procedure
0
+ server.on_restart { Thin::Command.run(:start, options) }
0
+
0
     server.start!
0
   end
0
 end
0
@@ -122,7 +125,7 @@ def stop(options)
0
   if cluster?(options)
0
     Thin::Cluster.new(options).stop
0
   else
0
- Thin::Server.kill options[:pid], options[:timeout]
0
+ Thin::Server.kill(options[:pid], options[:timeout])
0
   end
0
 end
0
 
0
@@ -132,11 +135,7 @@ def restart(options)
0
   if cluster?(options)
0
     Thin::Cluster.new(options).restart
0
   else
0
- # Restart only make sense when running as a daemon
0
- options.update :daemonize => true
0
-
0
- stop(options)
0
- start(options)
0
+ Thin::Server.restart(options[:pid])
0
   end
0
 end
0
 
...
21
22
23
24
 
25
26
27
...
21
22
23
 
24
25
26
27
0
@@ -21,7 +21,7 @@ module Thin
0
       @options = options.merge(:daemonize => true)
0
       @size = @options.delete(:servers)
0
       @only = @options.delete(:only)
0
- @script = 'thin'
0
+ @script = $PROGRAM_NAME
0
       
0
       if socket
0
         @options.delete(:address)
...
10
11
12
13
 
14
15
16
...
10
11
12
 
13
14
15
16
0
@@ -10,7 +10,7 @@ module Thin
0
     def initialize(name, options={})
0
       @name = name
0
       @options = options
0
- @script = 'thin'
0
+ @script = $PROGRAM_NAME
0
     end
0
     
0
     def self.run(*args)
...
40
41
42
 
 
43
44
45
...
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
...
40
41
42
43
44
45
46
47
...
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
120
121
122
 
 
123
124
125
126
 
127
128
129
130
 
131
132
133
134
0
@@ -40,6 +40,8 @@ module Thin
0
       
0
       Dir.chdir(pwd)
0
       
0
+ trap('HUP') { restart }
0
+
0
       write_pid_file
0
       at_exit do
0
         log ">> Exiting!"
0
@@ -66,37 +68,67 @@ module Thin
0
       log "Couldn't change user and group to #{user}:#{group}: #{e}"
0
     end
0
     
0
+ # Registerer a proc to be called to restart the server.
0
+ def on_restart(&block)
0
+ @on_restart = block
0
+ end
0
+
0
+ # Restart the server
0
+ def restart
0
+ raise ArgumentError, "Can't restart, no on_restart block specified" unless @on_restart
0
+ log '>> Restarting ...'
0
+ stop
0
+ remove_pid_file
0
+ @on_restart.call
0
+ exit!
0
+ end
0
+
0
     module ClassMethods
0
- # Kill the process which PID is stored in +pid_file+.
0
+ # Send a INT signal the process which PID is stored in +pid_file+.
0
+ # If the process is still running after +timeout+, KILL signal is
0
+ # sent.
0
       def kill(pid_file, timeout=60)
0
- if pid = open(pid_file).read
0
- pid = pid.to_i
0
- print "Sending INT signal to process #{pid} ... "
0
+ if pid = send_signal('INT', pid_file)
0
           begin
0
- Process.kill('INT', pid)
0
             Timeout.timeout(timeout) do
0
               sleep 0.1 while Process.running?(pid)
0
             end
0
           rescue Timeout::Error
0
- print "timeout, Sending KILL signal ... "
0
- Process.kill('KILL', pid)
0
+ print "timeout! "
0
+ send_signal('KILL', pid_file)
0
           end
0
- puts "stopped!"
0
+ end
0
+ File.delete(pid_file) if File.exist?(pid_file)
0
+ end
0
+
0
+ # Restart the server by sending HUP signal
0
+ def restart(pid_file)
0
+ send_signal('HUP', pid_file)
0
+ end
0
+
0
+ # Send a +signal+ to the process which PID is stored in +pid_file+.
0
+ def send_signal(signal, pid_file)
0
+ if File.exist?(pid_file) && pid = open(pid_file).read
0
+ pid = pid.to_i
0
+ print "Sending #{signal} signal to process #{pid} ... "
0
+ Process.kill(signal, pid)
0
+ puts
0
+ pid
0
         else
0
- puts "Can't stop process, no PID found in #{@pid_file}"
0
+ puts "Can't stop process, no PID found in #{pid_file}"
0
+ nil
0
         end
0
       rescue Errno::ESRCH # No such process
0
         puts "process not found!"
0
- ensure
0
- File.delete(pid_file) rescue nil
0
+ nil
0
       end
0
     end
0
     
0
- private
0
+ protected
0
       def remove_pid_file
0
         File.delete(@pid_file) if @pid_file && File.exists?(@pid_file)
0
       end
0
-
0
+
0
       def write_pid_file
0
         log ">> Writing PID to #{@pid_file}"
0
         FileUtils.mkdir_p File.dirname(@pid_file)
...
93
94
95
96
 
97
98
99
...
93
94
95
 
96
97
98
99
0
@@ -93,7 +93,7 @@ module Thin
0
     # Stops the server by raising an error.
0
     def stop!
0
       raise StopServer
0
- end
0
+ end
0
     
0
     protected
0
       def start_server
...
88
89
90
 
 
91
92
93
...
88
89
90
91
92
93
94
95
0
@@ -88,6 +88,8 @@ describe 'Daemonizing' do
0
     Process.running?(@pid).should be_false
0
   end
0
   
0
+ it "should restart"
0
+
0
   after do
0
     Process.kill(9, @pid.to_i) if @pid && Process.running?(@pid.to_i)
0
     Process.kill(9, @server.pid) if @server.pid && Process.running?(@server.pid)

Comments

    No one has commented yet.