Parent

Class/Module Index [+]

Quicksearch

PhusionPassenger::UnionStation::Core

Attributes

max_connect_tries[RW]
reconnect_timeout[RW]

Public Class Methods

new(ust_router_address, username, password, node_name) click to toggle source
# File lib/phusion_passenger/union_station/core.rb, line 56
def initialize(ust_router_address, username, password, node_name)
  @server_address = ust_router_address
  @username = username
  @password = password
  if node_name && !node_name.empty?
    @node_name = node_name
  else
    @node_name = `hostname`.strip
  end
  @random_dev = File.open("/dev/urandom")

  # This mutex protects the following instance variables, but
  # not the contents of @connection.
  @mutex = Mutex.new

  @connection = Connection.new(nil)
  if @server_address && local_socket_address?(@server_address)
    @max_connect_tries = 10
  else
    @max_connect_tries = 1
  end
  @reconnect_timeout = 1
  @next_reconnect_time = Time.utc(1980, 1, 1)
end
new_from_options(options) click to toggle source
# File lib/phusion_passenger/union_station/core.rb, line 42
def self.new_from_options(options)
  if options["analytics"] && options["ust_router_address"]
    return new(options["ust_router_address"],
      options["ust_router_username"],
      options["ust_router_password"],
      options["node_name"])
  else
    return nil
  end
end

Public Instance Methods

clear_connection() click to toggle source
# File lib/phusion_passenger/union_station/core.rb, line 81
def clear_connection
  @mutex.synchronize do
    @connection.synchronize do
      @random_dev = File.open("/dev/urandom") if @random_dev.closed?
      @connection.unref
      @connection = Connection.new(nil)
    end
  end
end
close() click to toggle source
# File lib/phusion_passenger/union_station/core.rb, line 91
def close
  @mutex.synchronize do
    @connection.synchronize do
      @random_dev.close
      @connection.unref
      @connection = nil
    end
  end
end
continue_transaction(txn_id, group_name, category = :requests, union_station_key = "-") click to toggle source
# File lib/phusion_passenger/union_station/core.rb, line 160
def continue_transaction(txn_id, group_name, category = :requests, union_station_key = "-")
  if !@server_address
    return Transaction.new
  elsif !txn_id || txn_id.empty?
    raise ArgumentError, "Transaction ID may not be empty"
  end

  Utils::Lock.new(@mutex).synchronize do |lock|
    if current_time < @next_reconnect_time
      return Transaction.new
    end

    Utils::Lock.new(@connection.mutex).synchronize do |connection_lock|
      if !@connection.connected?
        begin
          connect
          connection_lock.reset(@connection.mutex)
        rescue SystemCallError, IOError
          @connection.disconnect
          warn("Cannot connect to the UstRouter at #{@server_address}; " +
            "retrying in #{@reconnect_timeout} second(s).")
          @next_reconnect_time = current_time + @reconnect_timeout
          return Transaction.new
        rescue Exception => e
          @connection.disconnect
          raise e
        end
      end

      begin
        @connection.channel.write("openTransaction",
          txn_id, group_name, "", category,
          Core.timestamp_string,
          union_station_key,
          true)
        return Transaction.new(@connection, txn_id)
      rescue SystemCallError, IOError
        @connection.disconnect
        warn("The UstRouter at #{@server_address}" <<
          " closed the connection; will reconnect in " <<
          "#{@reconnect_timeout} second(s).")
        @next_reconnect_time = current_time + @reconnect_timeout
        return Transaction.new
      rescue Exception => e
        @connection.disconnect
        raise e
      end
    end
  end
end
new_transaction(group_name, category = :requests, union_station_key = "-") click to toggle source
# File lib/phusion_passenger/union_station/core.rb, line 101
def new_transaction(group_name, category = :requests, union_station_key = "-")
  if !@server_address
    return Transaction.new
  elsif !group_name || group_name.empty?
    raise ArgumentError, "Group name may not be empty"
  end

  txn_id = (Core.current_time.to_i / 60).to_s(36)
  txn_id << "-#{random_token(11)}"

  Utils::Lock.new(@mutex).synchronize do |lock|
    if current_time < @next_reconnect_time
      return Transaction.new
    end

    Utils::Lock.new(@connection.mutex).synchronize do |connection_lock|
      if !@connection.connected?
        begin
          connect
          connection_lock.reset(@connection.mutex)
        rescue SystemCallError, IOError
          @connection.disconnect
          warn("Cannot connect to the UstRouter at #{@server_address}; " +
            "retrying in #{@reconnect_timeout} second(s).")
          @next_reconnect_time = current_time + @reconnect_timeout
          return Transaction.new
        rescue Exception => e
          @connection.disconnect
          raise e
        end
      end

      begin
        @connection.channel.write("openTransaction",
          txn_id, group_name, "", category,
          Core.timestamp_string,
          union_station_key,
          true,
          true)
        result = @connection.channel.read
        if result != ["ok"]
          raise "Expected the UstRouter to respond with 'ok', but got #{result.inspect} instead"
        end
        return Transaction.new(@connection, txn_id)
      rescue SystemCallError, IOError
        @connection.disconnect
        warn("The UstRouter at #{@server_address}" <<
          " closed the connection; will reconnect in " <<
          "#{@reconnect_timeout} second(s).")
        @next_reconnect_time = current_time + @reconnect_timeout
        return Transaction.new
      rescue Exception => e
        @connection.disconnect
        raise e
      end
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.