class UnionStationHooks::Transaction

@private

Attributes

txn_id[R]

Public Class Methods

new(connection, txn_id) click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 34
def initialize(connection, txn_id)
  @connection = connection
  @txn_id = txn_id
  if connection
    raise ArgumentError, 'Transaction ID required' if txn_id.nil?
    connection.ref
  end
end

Public Instance Methods

close(should_flush_to_disk = false) click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 125
def close(should_flush_to_disk = false)
  return if !@connection

  @connection.synchronize do
    return if !@connection.connected?

    begin
      io_operation do
        # We need an ACK here so that we the UstRouter doesn't end up
        # processing the Core's openTransaction and closeTransaction pair
        # before it has received this process's openTransaction command.
        @connection.channel.write('closeTransaction', @txn_id,
          Utils.encoded_timestamp, true)
        Utils.process_ust_router_reply(@connection.channel,
          "Error handling reply for 'closeTransaction' message")
        if should_flush_to_disk
          flush_to_disk
        end
      end
    ensure
      @connection.unref
      @connection = nil
    end
  end
end
closed?() click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 151
def closed?
  return nil if !@connection
  @connection.synchronize do
    !@connection.connected?
  end
end
log_activity(name, begin_time, end_time, extra_info = nil, has_error = false) click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 119
def log_activity(name, begin_time, end_time, extra_info = nil,
                 has_error = false)
  log_activity_begin(name, begin_time, extra_info)
  log_activity_end(name, end_time, has_error)
end
log_activity_begin(name, time = UnionStationHooks.now, extra_info = nil) click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 85
def log_activity_begin(name, time = UnionStationHooks.now, extra_info = nil)
  if extra_info
    extra_info_base64 = Utils.base64(extra_info)
  else
    extra_info_base64 = nil
  end
  if time.is_a?(TimePoint)
    message "BEGIN: #{name} (#{Utils.encoded_timestamp(time)},"            "#{time.utime.to_s(36)},#{time.stime.to_s(36)}) "            "#{extra_info_base64}"
  else
    message "BEGIN: #{name} (#{Utils.encoded_timestamp(time)})"            " #{extra_info_base64}"
  end
end
log_activity_block(name, extra_info = nil) { || ... } click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 69
def log_activity_block(name, extra_info = nil)
  has_error = false
  log_activity_begin(name, UnionStationHooks.now, extra_info)
  begin
    yield
  rescue Exception
    has_error = true
    is_closed = closed?
    raise
  ensure
    if !is_closed
      log_activity_end(name, UnionStationHooks.now, has_error)
    end
  end
end
log_activity_end(name, time = UnionStationHooks.now, has_error = false) click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 101
def log_activity_end(name, time = UnionStationHooks.now, has_error = false)
  if time.is_a?(TimePoint)
    if has_error
      message "FAIL: #{name} (#{Utils.encoded_timestamp(time)},"              "#{time.utime.to_s(36)},#{time.stime.to_s(36)})"
    else
      message "END: #{name} (#{Utils.encoded_timestamp(time)},"              "#{time.utime.to_s(36)},#{time.stime.to_s(36)})"
    end
  else
    if has_error
      message "FAIL: #{name} (#{Utils.encoded_timestamp(time)})"
    else
      message "END: #{name} (#{Utils.encoded_timestamp(time)})"
    end
  end
end
message(text) click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 47
def message(text)
  if !@connection
    log_message_to_null(text)
    return
  end

  @connection.synchronize do
    if !@connection.connected?
      log_message_to_null(text)
      return
    end

    UnionStationHooks::Log.debug('[Union Station log] '            "#{@txn_id} #{Utils.encoded_timestamp} #{text}")

    io_operation do
      @connection.channel.write('log', @txn_id, Utils.encoded_timestamp)
      @connection.channel.write_scalar(text)
    end
  end
end
null?() click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 43
def null?
  !@connection || !@connection.connected?
end

Private Instance Methods

flush_to_disk() click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 176
def flush_to_disk
  @connection.channel.write('flush')
  Utils.process_ust_router_reply(@connection.channel,
    "Error handling reply for 'flush' message")
end
io_operation() { || ... } click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 165
def io_operation
  yield
rescue SystemCallError, IOError => e
  @connection.disconnect
  UnionStationHooks::Log.warn(
    "Error communicating with the UstRouter: #{e.message}")
rescue Exception => e
  @connection.disconnect
  raise e
end
log_message_to_null(text) click to toggle source
# File src/ruby_supportlib/phusion_passenger/vendor/union_station_hooks_core/lib/union_station_hooks_core/transaction.rb, line 160
def log_message_to_null(text)
  UnionStationHooks::Log.debug('[Union Station log to null] '          "#{@txn_id} #{Utils.encoded_timestamp} #{text}")
end