aboutsummaryrefslogtreecommitdiffstats
path: root/railties/bin/tracker
blob: 17fce67e5448c4734bd8fc46465ea23d24d0ac7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require "drb"
require "rbconfig"

VERBOSE = false

class Tracker
  include DRbUndumped
  
  def initialize(timeout = 90, uri = nil)
    @timeout = timeout
    @waiting = []
    @working = []
    
    @waiting_mutex = Mutex.new
    
    DRb.start_service(uri, self)
    @uri = DRb.uri
  end
  def run
    start_listener 3
    sleep 3
    
    background
  end
  
  def register_listener(listener)
    @waiting.push listener
    nil
  end
  def remove_listener(listener)
    @waiting.delete listener
    @working.delete listener
    nil
  end
  
  def with_listener
    listener = @waiting.shift
    unless listener
      start_listener(2) unless @waiting.length + @working.length > 6
      @waiting_mutex.synchronize do
	10.times do
          sleep 0.5
          listener = @waiting.shift
          break if listener
        end
        unless listener
          ($stderr.puts "Dropping request due to lack of listeners!!!" unless listener) if VERBOSE
          return
        end
      end
    end
    
    @working << listener
    yield listener
  ensure
    if listener
      @working.delete listener
      @waiting << listener
    end
  end
  
  def background
    loop do
      @timeout ? sleep(@timeout) : sleep
      unless @processed
        $stderr.puts "Idle for #{@timeout} -- shutting down tracker." if VERBOSE
        Kernel.exit 0 
      end
      @processed = false
    end
  end
    
  def process(input)
    output = nil
    $stderr.puts "tracker: received request.. obtaining listener" if VERBOSE
    with_listener do |listener|
      $stderr.puts "tracker: obtained -- forwarding request to listener.." if VERBOSE
      @processed = true
      output = listener.process(input)
      $stderr.puts "tracker: listener released control." if VERBOSE
    end
    return output
  end
  
  def start_listener(n = 1)
    tracker_uri = @uri
    listener_path = File.join(File.dirname(__FILE__), 'listener')
    fork do
      exec(
        File.join(Config::CONFIG['bin_dir'], Config::CONFIG['RUBY_SO_NAME']), 
        listener_path, 'start-listeners', tracker_uri, n.to_s
      )
    end
  end
  
  def ping
    true
  end
end

if ARGV.first == "start"
  tracker = Tracker.new(90, ARGV[1])
  socket = (/druby:([^?]*)\?/ =~ ARGV[1]) ? $1 : nil
  require 'fileutils' if socket

  begin tracker.run
  ensure 
    FileUtils.rm_f(socket) if socket
  end
end