Module: CMDx::Middlewares::Runtime

Extended by:
Runtime
Included in:
Runtime
Defined in:
lib/cmdx/middlewares/runtime.rb

Overview

Middleware for measuring task execution runtime.

The Runtime middleware provides performance monitoring by measuring the execution time of tasks using monotonic clock for accuracy. It stores runtime measurements in task result metadata for analysis.

Instance Method Summary collapse

Instance Method Details

#call(task, **options) { ... } ⇒ Object

Middleware entry point that measures task execution runtime and task execution start and end times.

Evaluates the condition from options and measures execution time if enabled. Uses monotonic clock for precise timing measurements and stores the result in task metadata.

Examples:

Basic usage with automatic runtime measurement

Runtime.call(task, &block)

Conditional runtime measurement

Runtime.call(task, if: :enable_profiling, &block)

Disable runtime measurement

Runtime.call(task, unless: :skip_profiling, &block)

Parameters:

  • task (Task)

    The task being executed

  • options (Hash)

    Configuration options for runtime measurement

Options Hash (**options):

  • :if (Symbol, Proc, Object, nil)

    Condition to enable runtime measurement

  • :unless (Symbol, Proc, Object, nil)

    Condition to disable runtime measurement

Yields:

  • The task execution block

Returns:

  • (Object)

    The result of task execution

Rbs:

  • (Task task, **untyped options) { () -> untyped } -> untyped



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cmdx/middlewares/runtime.rb', line 38

def call(task, **options)
  unow = utc_time
  mnow = monotonic_time
  return yield unless Utils::Condition.evaluate(task, options)

  result = yield
  task.result..merge!(
    runtime: monotonic_time - mnow,
    ended_at: utc_time,
    started_at: unow
  )
  result
end