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:
Clean stale PID files when starting as daemon, fixes #53 [Chu Yeow]
macournoyer (author)
Mon Mar 03 16:49:32 -0800 2008
commit  d44352acc6888df131bf0980c1d4c3324ba7a535
tree    18ae75c03f5d4b7a850b7588c2f6fa238b0628bf
parent  530729a8818ac68fa0d221f45aedf1ec4d341717
...
1
 
2
3
4
...
1
2
3
4
5
0
@@ -1,4 +1,5 @@
0
 == 0.7.1 Fancy Pants release
0
+ * Clean stale PID files when starting as daemon, fixes #53 [Chu Yeow]
0
  * Require EventMachine 0.11. Until it's released, install from:
0
    gem install eventmachine --source http://code.macournoyer.com
0
  * Ruby 1.8.5 compatibility, closes #49 [Wincent Colaiuta]
...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
 
 
51
52
53
...
150
151
152
 
 
 
 
 
 
 
 
 
 
 
 
 
153
154
155
...
37
38
39
 
 
 
 
 
 
 
 
 
 
 
40
41
42
43
44
...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
0
@@ -37,17 +37,8 @@
0
       raise PlatformNotSupported, 'Daemonizing is not supported on Windows' if Thin.win?
0
       raise ArgumentError, 'You must specify a pid_file to daemonize' unless @pid_file
0
 
0
- # If PID file is stale, remove it.
0
- if File.exist?(@pid_file)
0
- if pid && Process.running?(pid)
0
- raise PidFileExist, "#{@pid_file} already exists, seems like it's already running (process ID: #{pid}). " +
0
- "Stop the process or delete #{@pid_file}."
0
- else
0
- log ">> Deleting stale PID file #{@pid_file}"
0
- remove_pid_file
0
- end
0
- end
0
-
0
+ remove_stale_pid_file
0
+
0
       pwd = Dir.pwd # Current directory is changed during daemonization, so store it
0
       
0
       Daemonize.daemonize(File.expand_path(@log_file), name)
0
@@ -150,6 +141,19 @@
0
         FileUtils.mkdir_p File.dirname(@pid_file)
0
         open(@pid_file,"w") { |f| f.write(Process.pid) }
0
         File.chmod(0644, @pid_file)
0
+ end
0
+
0
+ # If PID file is stale, remove it.
0
+ def remove_stale_pid_file
0
+ if File.exist?(@pid_file)
0
+ if pid && Process.running?(pid)
0
+ raise PidFileExist, "#{@pid_file} already exists, seems like it's already running (process ID: #{pid}). " +
0
+ "Stop the process or delete #{@pid_file}."
0
+ else
0
+ log ">> Deleting stale PID file #{@pid_file}"
0
+ remove_pid_file
0
+ end
0
+ end
0
       end
0
   end
0
 end
...
15
16
17
 
18
 
19
20
21
22
23
24
 
25
26
27
...
131
132
133
134
 
135
136
137
...
143
144
145
 
 
 
 
 
 
 
 
 
146
147
148
...
15
16
17
18
19
20
21
22
23
24
25
 
26
27
28
29
...
133
134
135
 
136
137
138
139
...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
0
@@ -15,13 +15,15 @@
0
 describe 'Daemonizing' do
0
   before :all do
0
     @logfile = File.dirname(__FILE__) + '/../log/daemonizing_test.log'
0
+ @pidfile = 'test.pid'
0
     File.delete(@logfile) if File.exist?(@logfile)
0
+ File.delete(@pidfile) if File.exist?(@pidfile)
0
   end
0
   
0
   before :each do
0
     @server = TestServer.new
0
     @server.log_file = @logfile
0
- @server.pid_file = 'test.pid'
0
+ @server.pid_file = @pidfile
0
     @pid = nil
0
   end
0
   
0
@@ -131,7 +133,7 @@
0
     proc { sleep 0.1 while File.exist?(@server.pid_file) }.should take_less_then(10)
0
   end
0
   
0
- it "should exit if pid file already exist" do
0
+ it "should exit and raise if pid file already exist" do
0
     @pid = fork do
0
       @server.daemonize
0
       sleep 5
0
@@ -143,6 +145,15 @@
0
     proc { @server.daemonize }.should raise_error(PidFileExist)
0
     
0
     File.exist?(@server.pid_file).should be_true
0
+ end
0
+
0
+ it "should should delete pid file if stale" do
0
+ # Create a file w/ a PID that does not exist
0
+ File.open(@server.pid_file, 'w') { |f| f << 999999999 }
0
+
0
+ @server.send(:remove_stale_pid_file)
0
+
0
+ File.exist?(@server.pid_file).should be_false
0
   end
0
   
0
   after do

Comments

    No one has commented yet.