5 # This file includes an experimental gateway CGI implementation. It will work
6 # only on platforms which support both fork and sockets.
8 # To enable it edit public/.htaccess and replace dispatch.cgi with gateway.cgi.
10 # Next, create the directory log/drb_gateway and grant the apache user rw access
13 # On the next request to your server, the gateway tracker should start up, along
14 # with a few listener processes. This setup should provide you with much better
15 # speeds than dispatch.cgi.
17 # Keep in mind that the first request made to the server will be slow, as the
18 # tracker and listeners will have to load. Also, the tracker and listeners will
19 # shutdown after a period if inactivity. You can set this value below -- the
20 # default is 90 seconds.
22 TrackerSocket
= File
.expand_path(File
.join(File
.dirname(__FILE__
), '../log/drb_gateway/tracker.sock'))
23 DieAfter
= 90 # Seconds
27 $stderr.puts
"gateway.cgi: #{s}" if ENV && ENV["DEBUG_GATEWAY"]
30 def listener_socket(number
)
31 File
.expand_path(File
.join(File
.dirname(__FILE__
), "../log/drb_gateway/listener_#{number}.sock"))
34 unless File
.exist
? TrackerSocket
35 message
"Starting tracker and #{Listeners} listeners"
38 STDIN.reopen
"/dev/null"
39 STDOUT.reopen
"/dev/null", "a"
41 root
= File
.expand_path(File
.dirname(__FILE__
) + '/..')
43 message
"starting tracker"
46 ARGV << TrackerSocket
<< Listeners
.to_s
<< DieAfter
.to_s
47 load File
.join(root
, 'script', 'tracker')
50 message
"starting listeners"
51 require File
.join(root
, 'config/environment.rb')
52 Listeners
.times
do |number
|
55 ARGV << listener_socket(number
) << DieAfter
.to_s
56 load File
.join(root
, 'script', 'listener')
61 message
"waiting for tracker and listener to arise..."
65 break if (ready
= File
.exist
?(TrackerSocket
) && File
.exist
?(listener_socket(0)))
69 message
"tracker and listener are ready"
71 message
"Waited 5 seconds, listener and tracker not ready... dropping request"
78 message
"connecting to tracker"
79 tracker
= DRbObject
.new_with_uri("drbunix:#{TrackerSocket}")
87 tracker
.with_listener
do |number
|
88 message
"connecting to listener #{number}"
89 socket
= listener_socket(number
)
90 listener
= DRbObject
.new_with_uri("drbunix:#{socket}")
91 output
= listener
.process(env, input
)
92 message
"listener #{number} has finished, writing output"