Class: CMDx::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdx/runtime.rb

Overview

Note:

Always used via the class method; never new Runtime manually.

Orchestrates a task’s full lifecycle: chain acquisition, middlewares, telemetry, deprecation, callbacks, input resolution, ‘work` (wrapped in retry), output verification, rollback on failure, result finalization, and teardown (freeze + chain clear).

Signal propagation: ‘work` and the surrounding middleware/callback chain both run inside `catch(Signal::TAG)`, so `success!` / `skip!` / `fail!` / `throw!` halt cleanly from anywhere. Raised Faults become echoed signals (with the upstream result as `:origin`); other `StandardError`s become failed signals (with the exception as `:cause`). In strict mode, `execute!` re-raises from `ensure` using a Fault built from the deepest originating result, so `fault.task` points at the leaf that failed.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task, strict: false) ⇒ Runtime

Returns a new instance of Runtime.

Parameters:

  • task (Task)
  • strict (Boolean) (defaults to: false)


37
38
39
40
# File 'lib/cmdx/runtime.rb', line 37

def initialize(task, strict: false)
  @task   = task
  @strict = strict
end

Class Method Details

.execute(task, strict: false) ⇒ Result

Returns the finalized, frozen result.

Parameters:

  • task (Task)
  • strict (Boolean) (defaults to: false)

    when true, re-raise on failure (‘execute!` semantics)

Returns:

  • (Result)

    the finalized, frozen result

Raises:

  • (Fault, StandardError)

    only when ‘strict: true` and the task failed



29
30
31
# File 'lib/cmdx/runtime.rb', line 29

def execute(task, strict: false)
  new(task, strict:).execute
end

Instance Method Details

#executeResult

Runs the full lifecycle. Teardown runs in ‘ensure`, guaranteeing the task’s context/errors get frozen and the fiber chain is cleared even when strict mode re-raises.

Returns:

Raises:

  • (Fault, StandardError)

    under strict mode on failure



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cmdx/runtime.rb', line 48

def execute
  acquire_chain

  run_middlewares do
    emit_telemetry(:task_started)
    run_deprecation
    run_lifecycle
  end

  finalize_result
ensure
  run_teardown
  raise_signal!
end