Class: UnionStationHooks::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/union_station_hooks_core/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, txn_id) ⇒ Transaction

Returns a new instance of Transaction.



34
35
36
37
38
39
40
41
# File '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

Instance Attribute Details

#txn_idObject (readonly)

Returns the value of attribute txn_id.



32
33
34
# File 'lib/union_station_hooks_core/transaction.rb', line 32

def txn_id
  @txn_id
end

Instance Method Details

#close(should_flush_to_disk = false) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File '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?Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
# File '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) ⇒ Object



119
120
121
122
123
# File '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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File '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) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File '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) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File '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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File '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?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/union_station_hooks_core/transaction.rb', line 43

def null?
  !@connection || !@connection.connected?
end