Class: CDC::Core::ProcessorResult
- Inherits:
-
Object
- Object
- CDC::Core::ProcessorResult
- Defined in:
- lib/cdc/core/processor_result.rb
Overview
Result returned by processors and pipelines.
ProcessorResult standardizes processor outcomes so callers can distinguish successful processing, skipped events, and failures without relying on processor-specific return values.
Constant Summary collapse
- VALID_STATUSES =
Allowed result statuses.
Ractor.make_shareable(%i[success failure skipped].freeze)
Instance Attribute Summary collapse
- #error ⇒ Symbol, ... readonly
- #event ⇒ Symbol, ... readonly
- #metadata ⇒ Symbol, ... readonly
- #status ⇒ Symbol, ... readonly
- #value ⇒ Symbol, ... readonly
Class Method Summary collapse
-
.failure(error, event: nil, reason: nil, retryable: nil, processor: nil, failed_at: nil, metadata: nil) ⇒ ProcessorResult
Build a failure result.
-
.skipped(event = nil, metadata: {}) ⇒ ProcessorResult
Build a skipped result.
-
.success(event = nil, metadata: {}, value: event) ⇒ ProcessorResult
Build a successful result.
Instance Method Summary collapse
-
#error_backtrace ⇒ Array<String>
Error backtrace, when present.
-
#error_class ⇒ String?
Error class name, when present.
-
#error_message ⇒ String?
Error message, when present.
-
#failed_at ⇒ String?
Timestamp for when the failure occurred, when present.
-
#failure? ⇒ Boolean
True when status is :failure.
-
#failure_reason ⇒ String?
Human-readable failure reason, when present.
-
#initialize(status, event: nil, error: nil, metadata: {}, value: event) ⇒ ProcessorResult
constructor
Build a processor result with an explicit status.
-
#processor_name ⇒ String?
Name of the processor associated with the failure, when present.
-
#retryable? ⇒ Boolean
Whether the failure is retryable.
-
#skipped? ⇒ Boolean
True when status is :skipped.
-
#success? ⇒ Boolean
True when status is :success.
-
#to_h ⇒ Hash{String=>Object,nil}
Convert the result into a shareable hash.
Constructor Details
#initialize(status, event: nil, error: nil, metadata: {}, value: event) ⇒ ProcessorResult
Build a processor result with an explicit status.
66 67 68 69 70 71 72 73 |
# File 'lib/cdc/core/processor_result.rb', line 66 def initialize(status, event: nil, error: nil, metadata: {}, value: event) @status = normalize_status(status) @event = event @value = value @error = error @metadata = .is_a?(EventMetadata) ? : EventMetadata.new() make_shareable_when_possible end |
Instance Attribute Details
#error ⇒ Symbol, ... (readonly)
19 20 21 |
# File 'lib/cdc/core/processor_result.rb', line 19 def error @error end |
#event ⇒ Symbol, ... (readonly)
19 20 21 |
# File 'lib/cdc/core/processor_result.rb', line 19 def event @event end |
#metadata ⇒ Symbol, ... (readonly)
19 20 21 |
# File 'lib/cdc/core/processor_result.rb', line 19 def @metadata end |
#status ⇒ Symbol, ... (readonly)
19 20 21 |
# File 'lib/cdc/core/processor_result.rb', line 19 def status @status end |
#value ⇒ Symbol, ... (readonly)
19 20 21 |
# File 'lib/cdc/core/processor_result.rb', line 19 def value @value end |
Class Method Details
.failure(error, event: nil, reason: nil, retryable: nil, processor: nil, failed_at: nil, metadata: nil) ⇒ ProcessorResult
Build a failure result.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cdc/core/processor_result.rb', line 39 def self.failure(error, event: nil, reason: nil, retryable: nil, processor: nil, failed_at: nil, metadata: nil) = .nil? ? EventMetadata.new.to_h : .to_h = .merge( reason: reason || error., retryable: retryable, processor: processor || error.class.name, failed_at: failed_at || Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S.%6NZ') ).compact new(:failure, event:, error:, metadata: ) end |
.skipped(event = nil, metadata: {}) ⇒ ProcessorResult
Build a skipped result.
57 |
# File 'lib/cdc/core/processor_result.rb', line 57 def self.skipped(event = nil, metadata: {}) = new(:skipped, event:, metadata:) |
.success(event = nil, metadata: {}, value: event) ⇒ ProcessorResult
Build a successful result.
27 |
# File 'lib/cdc/core/processor_result.rb', line 27 def self.success(event = nil, metadata: {}, value: event) = new(:success, event:, metadata:, value:) |
Instance Method Details
#error_backtrace ⇒ Array<String>
Error backtrace, when present.
129 130 131 |
# File 'lib/cdc/core/processor_result.rb', line 129 def error_backtrace Array(error&.backtrace) end |
#error_class ⇒ String?
Error class name, when present.
115 116 117 |
# File 'lib/cdc/core/processor_result.rb', line 115 def error_class error&.class&.name end |
#error_message ⇒ String?
Error message, when present.
122 123 124 |
# File 'lib/cdc/core/processor_result.rb', line 122 def error&. end |
#failed_at ⇒ String?
Timestamp for when the failure occurred, when present.
108 109 110 |
# File 'lib/cdc/core/processor_result.rb', line 108 def failed_at [:failed_at] end |
#failure? ⇒ Boolean
Returns true when status is :failure.
79 |
# File 'lib/cdc/core/processor_result.rb', line 79 def failure? = status == :failure |
#failure_reason ⇒ String?
Human-readable failure reason, when present.
87 88 89 |
# File 'lib/cdc/core/processor_result.rb', line 87 def failure_reason [:reason] end |
#processor_name ⇒ String?
Name of the processor associated with the failure, when present.
101 102 103 |
# File 'lib/cdc/core/processor_result.rb', line 101 def processor_name [:processor] end |
#retryable? ⇒ Boolean
Whether the failure is retryable.
94 95 96 |
# File 'lib/cdc/core/processor_result.rb', line 94 def retryable? [:retryable] == true end |
#skipped? ⇒ Boolean
Returns true when status is :skipped.
82 |
# File 'lib/cdc/core/processor_result.rb', line 82 def skipped? = status == :skipped |
#success? ⇒ Boolean
Returns true when status is :success.
76 |
# File 'lib/cdc/core/processor_result.rb', line 76 def success? = status == :success |
#to_h ⇒ Hash{String=>Object,nil}
Convert the result into a shareable hash.
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/cdc/core/processor_result.rb', line 136 def to_h payload = { 'status' => status, 'event' => event.respond_to?(:to_h) ? event.to_h : event, 'value' => value.respond_to?(:to_h) ? value.to_h : value, 'error_class' => error_class, 'error_message' => , 'error_backtrace' => error_backtrace, 'metadata' => .to_h } Ractor.make_shareable(payload.freeze) end |