root/lang/ruby/pritter/vendor/rails/railties/dispatches/gateway.cgi @ 5644

Revision 5644, 2.7 kB (checked in by nakamud, 5 years ago)

lang/ruby/pritter: freezed gems to ease installation

Line 
1#!/usr/local/bin/ruby
2
3require '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
22TrackerSocket = File.expand_path(File.join(File.dirname(__FILE__), '../log/drb_gateway/tracker.sock'))
23DieAfter = 90 # Seconds
24Listeners = 3
25
26def message(s)
27  $stderr.puts "gateway.cgi: #{s}" if ENV && ENV["DEBUG_GATEWAY"]
28end
29
30def listener_socket(number)
31  File.expand_path(File.join(File.dirname(__FILE__), "../log/drb_gateway/listener_#{number}.sock"))
32end
33
34unless 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
74end
75
76DRb.start_service
77
78message "connecting to tracker"
79tracker = DRbObject.new_with_uri("drbunix:#{TrackerSocket}")
80
81input = $stdin.read
82$stdin.close
83
84env = ENV.inspect
85
86output = nil
87tracker.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"
93end
94
95$stdout.write output
96$stdout.flush
97$stdout.close
Note: See TracBrowser for help on using the browser.