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



115
116
117
118
119
120
121
122
123
124
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
150
151
152
153
154
155
156
# File 'lib/easyop/plugins/recording.rb', line 115

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