Class: RailsAiBridge::Introspector::TimedRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/introspector/timed_runner.rb

Overview

Executes a single introspector class and measures how long it takes.

This is a pure value-object service: it has no state of its own and is not responsible for thread management, error aggregation, or configuration. It does one thing — run an introspector and tell you how long it took.

The result hash uses two keys:

  • +:result+ — the raw return value of the introspector, or +{ error: message }+ when the introspector raises.
  • +:duration_ms+ — wall-clock time of the introspector call in milliseconds, as a +Float+. Available even on error so callers can diagnose slow-then-failing introspectors.

Examples:

timed = TimedRunner.call(SchemaIntrospector, Rails.application)
timed[:result]      # => { tables: { ... }, ... }
timed[:duration_ms] # => 42.7

Class Method Summary collapse

Class Method Details

.call(klass, app) ⇒ Hash{Symbol => Object}

Instantiates +klass+ with +app+ and calls it, measuring elapsed time.

Parameters:

  • klass (Class)

    an introspector class responding to +.new(app)+ and +#call+

  • app (Rails::Application)

    the Rails application instance

Returns:

  • (Hash{Symbol => Object})

    a hash with +:result+ and +:duration_ms+



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_ai_bridge/introspector/timed_runner.rb', line 30

def self.call(klass, app)
  result     = nil
  caught     = nil
  started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  begin
    result = klass.new(app).call
  rescue StandardError => error
    caught = error
  end

  duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started_at) * 1000.0).round(2)
  payload     = caught ? { error: caught.message } : result
  { result: payload, duration_ms: duration_ms }
end