Module: Easyop::Plugins::Recording::RunWrapper

Defined in:
lib/easyop/plugins/recording.rb

Instance Method Summary collapse

Instance Method Details

#_easyop_run(ctx, raise_on_failure:) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/easyop/plugins/recording.rb', line 144

def _easyop_run(ctx, raise_on_failure:)
  return super unless self.class._recording_enabled?
  return super unless (model = self.class._recording_model)
  return super unless self.class.name # skip anonymous classes

  # -- Flow tracing --
  # Each operation gets its own reference_id. The root_reference_id is
  # shared across the entire execution tree via ctx (set once, inherited).
  reference_id      = SecureRandom.uuid
  root_reference_id = ctx[:__recording_root_reference_id] ||= SecureRandom.uuid

  # Read current parent context — these become THIS operation's parent fields.
  parent_operation_name = ctx[:__recording_parent_operation_name]
  parent_reference_id   = ctx[:__recording_parent_reference_id]

  # Set THIS operation as the parent for any children that run inside super.
  # Save the previous values so we can restore them after (for siblings).
  prev_parent_name = parent_operation_name
  prev_parent_id   = parent_reference_id
  ctx[:__recording_parent_operation_name] = self.class.name
  ctx[:__recording_parent_reference_id]   = reference_id

  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  super
ensure
  # Always record — including when raise_on_failure: true raises Ctx::Failure
  # (e.g. when this operation is a step inside a Flow). Without the ensure
  # branch the tap block would be skipped and failures inside flows would
  # never be persisted.
  if start
    # Restore parent context so sibling steps see the correct parent.
    ctx[:__recording_parent_operation_name] = prev_parent_name
    ctx[:__recording_parent_reference_id]   = prev_parent_id

    ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000).round(2)
    _recording_persist!(ctx, model, ms,
      root_reference_id:     root_reference_id,
      reference_id:          reference_id,
      parent_operation_name: parent_operation_name,
      parent_reference_id:   parent_reference_id)
  end
end