Skip to content

Commit

Permalink
Adding starting_worker_process/stopping_worker_process event calls wh…
Browse files Browse the repository at this point in the history
…en spawning rack applications
  • Loading branch information
Saimon Moore committed Apr 7, 2009
1 parent d9fc577 commit 71da6cc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/phusion_passenger/rack/application_spawner.rb
Expand Up @@ -19,6 +19,7 @@

require 'socket'
require 'phusion_passenger/application'
require 'phusion_passenger/events'
require 'phusion_passenger/message_channel'
require 'phusion_passenger/abstract_request_handler'
require 'phusion_passenger/utils'
Expand Down Expand Up @@ -96,11 +97,14 @@ def run(channel, app_root, options)
channel.send_io(writer)
writer.close
channel.close

PhusionPassenger.call_event(:starting_worker_process)
handler.main_loop
ensure
channel.close rescue nil
writer.close rescue nil
handler.cleanup rescue nil
PhusionPassenger.call_event(:stopping_worker_process)
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions test/ruby/rack/application_spawner_spec.rb
Expand Up @@ -34,6 +34,46 @@
config_ru_owner.should == touch_txt_owner
end if Process.euid == 0

it "calls the starting_worker_process event after config.ru has been loaded" do
File.append("#{@stub.app_root}/config.ru", %q{
PhusionPassenger.on_event(:starting_worker_process) do
File.append("rackresult.txt", "worker_process_started\n")
end
File.append("rackresult.txt", "end of config.ru\n");
})
spawn(@stub.app_root).close
spawn(@stub.app_root).close

# Give some time for the starting_worker_process hook to be executed.
sleep 0.2

contents = File.read("#{@stub.app_root}/rackresult.txt")
contents.should == "end of config.ru\n" +
"worker_process_started\n" +
"end of config.ru\n" +
"worker_process_started\n"
end

it "calls the stopping_worker_process event" do
File.append("#{@stub.app_root}/config.ru", %q{
PhusionPassenger.on_event(:stopping_worker_process) do
File.append("rackresult.txt", "worker_process_stopped\n")
end
File.append("rackresult.txt", "end of config.ru\n");
})
spawn(@stub.app_root).close
spawn(@stub.app_root).close

# Give some time for the starting_worker_process hook to be executed.
sleep 0.2

contents = File.read("#{@stub.app_root}/rackresult.txt")
contents.should == "end of config.ru\n" +
"worker_process_stopped\n" +
"end of config.ru\n" +
"worker_process_stopped\n"
end

def spawn(app_root)
PhusionPassenger::Rack::ApplicationSpawner.spawn_application(app_root,
"lowest_user" => CONFIG['lowest_user'])
Expand Down

0 comments on commit 71da6cc

Please sign in to comment.