| 1 | #!/usr/local/bin/ruby |
|---|
| 2 | |
|---|
| 3 | require 'drb' |
|---|
| 4 | |
|---|
| 5 | # This file includes an experimental gateway CGI implementation. It will work |
|---|
| 6 | # only on platforms which support both fork and sockets. |
|---|
| 7 | # |
|---|
| 8 | # To enable it edit public/.htaccess and replace dispatch.cgi with gateway.cgi. |
|---|
| 9 | # |
|---|
| 10 | # Next, create the directory log/drb_gateway and grant the apache user rw access |
|---|
| 11 | # to said directory. |
|---|
| 12 | # |
|---|
| 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. |
|---|
| 16 | # |
|---|
| 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. |
|---|
| 21 | |
|---|
| 22 | TrackerSocket = File.expand_path(File.join(File.dirname(__FILE__), '../log/drb_gateway/tracker.sock')) |
|---|
| 23 | DieAfter = 90 # Seconds |
|---|
| 24 | Listeners = 3 |
|---|
| 25 | |
|---|
| 26 | def message(s) |
|---|
| 27 | $stderr.puts "gateway.cgi: #{s}" if ENV && ENV["DEBUG_GATEWAY"] |
|---|
| 28 | end |
|---|
| 29 | |
|---|
| 30 | def listener_socket(number) |
|---|
| 31 | File.expand_path(File.join(File.dirname(__FILE__), "../log/drb_gateway/listener_#{number}.sock")) |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | unless File.exists? TrackerSocket |
|---|
| 35 | message "Starting tracker and #{Listeners} listeners" |
|---|
| 36 | fork do |
|---|
| 37 | Process.setsid |
|---|
| 38 | STDIN.reopen "/dev/null" |
|---|
| 39 | STDOUT.reopen "/dev/null", "a" |
|---|
| 40 | |
|---|
| 41 | root = File.expand_path(File.dirname(__FILE__) + '/..') |
|---|
| 42 | |
|---|
| 43 | message "starting tracker" |
|---|
| 44 | fork do |
|---|
| 45 | ARGV.clear |
|---|
| 46 | ARGV << TrackerSocket << Listeners.to_s << DieAfter.to_s |
|---|
| 47 | load File.join(root, 'script', 'tracker') |
|---|
| 48 | end |
|---|
| 49 | |
|---|
| 50 | message "starting listeners" |
|---|
| 51 | require File.join(root, 'config/environment.rb') |
|---|
| 52 | Listeners.times do |number| |
|---|
| 53 | fork do |
|---|
| 54 | ARGV.clear |
|---|
| 55 | ARGV << listener_socket(number) << DieAfter.to_s |
|---|
| 56 | load File.join(root, 'script', 'listener') |
|---|
| 57 | end |
|---|
| 58 | end |
|---|
| 59 | end |
|---|
| 60 | |
|---|
| 61 | message "waiting for tracker and listener to arise..." |
|---|
| 62 | ready = false |
|---|
| 63 | 10.times do |
|---|
| 64 | sleep 0.5 |
|---|
| 65 | break if (ready = File.exists?(TrackerSocket) && File.exists?(listener_socket(0))) |
|---|
| 66 | end |
|---|
| 67 | |
|---|
| 68 | if ready |
|---|
| 69 | message "tracker and listener are ready" |
|---|
| 70 | else |
|---|
| 71 | message "Waited 5 seconds, listener and tracker not ready... dropping request" |
|---|
| 72 | Kernel.exit 1 |
|---|
| 73 | end |
|---|
| 74 | end |
|---|
| 75 | |
|---|
| 76 | DRb.start_service |
|---|
| 77 | |
|---|
| 78 | message "connecting to tracker" |
|---|
| 79 | tracker = DRbObject.new_with_uri("drbunix:#{TrackerSocket}") |
|---|
| 80 | |
|---|
| 81 | input = $stdin.read |
|---|
| 82 | $stdin.close |
|---|
| 83 | |
|---|
| 84 | env = ENV.inspect |
|---|
| 85 | |
|---|
| 86 | output = nil |
|---|
| 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" |
|---|
| 93 | end |
|---|
| 94 | |
|---|
| 95 | $stdout.write output |
|---|
| 96 | $stdout.flush |
|---|
| 97 | $stdout.close |
|---|