Class: CMDx::Chain
Overview
Ordered collection of Results produced by a top-level task and any nested tasks it triggers. A Chain is stored per-fiber so concurrent workflows (see Pipeline parallel strategy) each get their own. The root Runtime clears the chain on teardown.
Constant Summary collapse
- STORAGE_KEY =
:cmdx_chain
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
-
#xid ⇒ Object
readonly
Returns the value of attribute xid.
Class Method Summary collapse
-
.clear ⇒ nil
Clears the fiber-local chain reference.
-
.current ⇒ Chain?
The chain active on the current fiber, or nil outside execution.
-
.current=(chain) ⇒ Chain?
Installs ‘chain` as the active chain on the current fiber.
Instance Method Summary collapse
- #each {|Result| ... } ⇒ Enumerator, Chain
- #empty? ⇒ Boolean
-
#freeze ⇒ Chain
Freezes the chain and its results.
-
#index(result) ⇒ Integer?
Zero-based position of ‘result`, or nil when absent.
-
#initialize(xid = nil) ⇒ Chain
constructor
A new instance of Chain.
-
#last ⇒ Result?
The most recently appended result.
-
#push(result) ⇒ Chain
(also: #<<)
Appends ‘result` to the chain.
-
#root ⇒ Result?
The root result, or nil when absent.
- #size ⇒ Integer
-
#state ⇒ String?
The state of the root result, or nil when absent.
-
#status ⇒ String?
The status of the root result, or nil when absent.
-
#unshift(result) ⇒ Chain
Prepends ‘result` to the chain.
Constructor Details
#initialize(xid = nil) ⇒ Chain
Returns a new instance of Chain.
42 43 44 45 46 47 |
# File 'lib/cmdx/chain.rb', line 42 def initialize(xid = nil) @xid = xid @id = SecureRandom.uuid_v7 @mutex = Mutex.new @results = [] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
37 38 39 |
# File 'lib/cmdx/chain.rb', line 37 def id @id end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
37 38 39 |
# File 'lib/cmdx/chain.rb', line 37 def results @results end |
#xid ⇒ Object (readonly)
Returns the value of attribute xid.
37 38 39 |
# File 'lib/cmdx/chain.rb', line 37 def xid @xid end |
Class Method Details
.clear ⇒ nil
Clears the fiber-local chain reference.
31 32 33 |
# File 'lib/cmdx/chain.rb', line 31 def clear Fiber[STORAGE_KEY] = nil end |
.current ⇒ Chain?
Returns the chain active on the current fiber, or nil outside execution.
18 19 20 |
# File 'lib/cmdx/chain.rb', line 18 def current Fiber[STORAGE_KEY] end |
.current=(chain) ⇒ Chain?
Installs ‘chain` as the active chain on the current fiber.
25 26 27 |
# File 'lib/cmdx/chain.rb', line 25 def current=(chain) Fiber[STORAGE_KEY] = chain end |
Instance Method Details
#each {|Result| ... } ⇒ Enumerator, Chain
106 107 108 |
# File 'lib/cmdx/chain.rb', line 106 def each(&) @results.each(&) end |
#empty? ⇒ Boolean
95 96 97 |
# File 'lib/cmdx/chain.rb', line 95 def empty? @results.empty? end |
#freeze ⇒ Chain
Freezes the chain and its results. Called by Runtime teardown.
113 114 115 116 |
# File 'lib/cmdx/chain.rb', line 113 def freeze @mutex.synchronize { @results.freeze } super end |
#index(result) ⇒ Integer?
Returns zero-based position of ‘result`, or nil when absent.
70 71 72 |
# File 'lib/cmdx/chain.rb', line 70 def index(result) @results.index(result) end |
#last ⇒ Result?
Returns the most recently appended result.
75 76 77 |
# File 'lib/cmdx/chain.rb', line 75 def last @results.last end |
#push(result) ⇒ Chain Also known as: <<
Appends ‘result` to the chain. Thread-safe to support parallel pipelines.
53 54 55 56 |
# File 'lib/cmdx/chain.rb', line 53 def push(result) @mutex.synchronize { @results << result } self end |
#root ⇒ Result?
Returns the root result, or nil when absent.
80 81 82 |
# File 'lib/cmdx/chain.rb', line 80 def root @results.find(&:root?) end |
#size ⇒ Integer
100 101 102 |
# File 'lib/cmdx/chain.rb', line 100 def size @results.size end |
#state ⇒ String?
Returns the state of the root result, or nil when absent.
85 86 87 |
# File 'lib/cmdx/chain.rb', line 85 def state root&.state end |
#status ⇒ String?
Returns the status of the root result, or nil when absent.
90 91 92 |
# File 'lib/cmdx/chain.rb', line 90 def status root&.status end |
#unshift(result) ⇒ Chain
Prepends ‘result` to the chain. Thread-safe to support parallel pipelines.
63 64 65 66 |
# File 'lib/cmdx/chain.rb', line 63 def unshift(result) @mutex.synchronize { @results.unshift(result) } self end |