Skip to content

Commit

Permalink
Fix pid file cleanup when running on ruby 2.0
Browse files Browse the repository at this point in the history
- 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
copiousfreetime committed Nov 26, 2013
1 parent 6e4ce7e commit 7692508
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions lib/heel/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.