Permalink
Browse files

Fix pid file cleanup when running on ruby 2.0

- only spawn a server thread in the foreground mode
- at_exit handlers called from a thread in a forked
  process in ruby 2.0 appear to not be called.
- Closes #15, Closes #16
  • Loading branch information...
1 parent 6e4ce7e commit 7692508e5316645dc46f2b29c8175cc8e834749d @copiousfreetime committed Nov 26, 2013
Showing with 24 additions and 14 deletions.
  1. +24 −14 lib/heel/server.rb
View
@@ -237,21 +237,31 @@ def heel_app
return stack.to_app
end
+ # If we are daemonizing the fork and wait for the child to launch the server
+ # If we are not daemonizing, throw the Rack::Server in a background thread
def start_server
- server_thread = Thread.new do
- if options.daemonize then
- if cpid = fork then
- Process.waitpid( cpid )
- else
- server = Rack::Server.new( server_options )
- server.start
- end
- else
- server = Rack::Server.new( server_options )
- server.start
- end
+ if options.daemonize then
+ start_background_server
+ return nil
+ else
+ return start_foreground_server
end
- return server_thread
+ end
+
+ def start_background_server
+ if cpid = fork then
+ Process.waitpid( cpid )
+ else
+ server = Rack::Server.new( server_options )
+ server.start
+ end
+ end
+
+ def start_foreground_server
+ Thread.new {
+ server = Rack::Server.new( server_options )
+ server.start
+ }
end
def server_options
@@ -279,7 +289,7 @@ def run
if options.launch_browser then
launch_browser.join
end
- server_thread.join
+ server_thread.join if server_thread
end
end
end

0 comments on commit 7692508

Please sign in to comment.