Sha256: aaf93042be2eecb1309ec13e932a2caef55524cd823acb094948b8ba0babc1c1

Contents?: true

Size: 1.49 KB

Versions: 119

Compression:

Stored size: 1.49 KB

Contents

#!/usr/local/bin/ruby

require 'drb'
require 'thread'

def message(s)
  $stderr.puts "tracker: #{s}" if ENV && ENV["DEBUG_GATEWAY"]
end

class Tracker
  include DRbUndumped

  def initialize(instances, socket_path)
    @instances = instances
    @socket = File.expand_path(socket_path)
    @active = false

    @listeners = []
    @instances.times { @listeners << Mutex.new }

    message "using #{@listeners.length} listeners"
    message "opening socket at #{@socket}"

    @service = DRb.start_service("drbunix://#{@socket}", self)
  end

  def with_listener
    message "listener requested"

    mutex = has_lock = index = nil
    3.times do 
      @listeners.each_with_index do |mutex, index|
        has_lock = mutex.try_lock
        break if has_lock
      end
      break if has_lock
      sleep 0.05
    end

    if has_lock
      message "obtained listener #{index}"
      @active = true
      begin yield index
      ensure
        mutex.unlock
        message "released listener #{index}"
      end
    else
      message "dropping request because no listeners are available!"
    end
  end

  def background(check_interval = nil)
    if check_interval
      loop do
        sleep check_interval
        message "Idle for #{check_interval}, shutting down" unless @active
        @active = false
        Kernel.exit 0
      end
    else DRb.thread.join
    end
  end
end

socket_path = ARGV.shift
instances = ARGV.shift.to_i
t = Tracker.new(instances, socket_path)
t.background(ARGV.first ? ARGV.shift.to_i : 90)

Version data entries

119 entries across 119 versions & 11 rubygems

Version Path
radiant-0.7.2 vendor/rails/railties/lib/commands/ncgi/tracker
vibes-bj-1.2.2 spec/rails_root/vendor/rails/railties/lib/commands/ncgi/tracker
vibes-bj-1.2.1 spec/rails_root/vendor/rails/railties/lib/commands/ncgi/tracker
jstorimer-deep-test-2.0.0 sample_rails_project/vendor/rails/railties/lib/commands/ncgi/tracker
jstorimer-deep-test-1.4.0 sample_rails_project/vendor/rails/railties/lib/commands/ncgi/tracker
jstorimer-deep-test-1.3.0 sample_rails_project/vendor/rails/railties/lib/commands/ncgi/tracker
jstorimer-deep-test-1.2.0 sample_rails_project/vendor/rails/railties/lib/commands/ncgi/tracker
jstorimer-deep-test-1.1.0 sample_rails_project/vendor/rails/railties/lib/commands/ncgi/tracker
jstorimer-deep-test-1.0.0 sample_rails_project/vendor/rails/railties/lib/commands/ncgi/tracker
jstorimer-deep-test-0.2.0 sample_rails_project/vendor/rails/railties/lib/commands/ncgi/tracker
jstorimer-deep-test-0.1.0 sample_rails_project/vendor/rails/railties/lib/commands/ncgi/tracker
antfarm-0.3.0 rails/vendor/rails/railties/lib/commands/ncgi/tracker
antfarm-0.4.0 rails/vendor/rails/railties/lib/commands/ncgi/tracker
authorails-1.0.0 lib/commands/ncgi/tracker
backlog-0.0.0 vendor/rails/railties/lib/commands/ncgi/tracker
backlog-0.0.1 vendor/rails/railties/lib/commands/ncgi/tracker
backlog-0.0.2 vendor/rails/railties/lib/commands/ncgi/tracker
backlog-0.0.5 vendor/rails/railties/lib/commands/ncgi/tracker
backlog-0.0.4 vendor/rails/railties/lib/commands/ncgi/tracker
backlog-0.1.1 vendor/rails/railties/lib/commands/ncgi/tracker