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
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: {}) ⇒ 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: {}) ⇒ 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: {}) ⇒ ProcessorResult
Build a processor result with an explicit status.
63 64 65 66 67 68 69 |
# File 'lib/cdc/core/processor_result.rb', line 63 def initialize(status, event: nil, error: nil, metadata: {}) @status = normalize_status(status) @event = event @error = error @metadata = .is_a?(EventMetadata) ? : EventMetadata.new() Ractor.make_shareable(self) unless error end |
Instance Attribute Details
#error ⇒ Symbol, ... (readonly)
18 19 20 |
# File 'lib/cdc/core/processor_result.rb', line 18 def error @error end |
#event ⇒ Symbol, ... (readonly)
18 19 20 |
# File 'lib/cdc/core/processor_result.rb', line 18 def event @event end |
#metadata ⇒ Symbol, ... (readonly)
18 19 20 |
# File 'lib/cdc/core/processor_result.rb', line 18 def @metadata end |
#status ⇒ Symbol, ... (readonly)
18 19 20 |
# File 'lib/cdc/core/processor_result.rb', line 18 def status @status end |
Class Method Details
.failure(error, event: nil, reason: nil, retryable: nil, processor: nil, failed_at: nil, metadata: nil) ⇒ ProcessorResult
Build a failure result.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/cdc/core/processor_result.rb', line 37 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.
55 |
# File 'lib/cdc/core/processor_result.rb', line 55 def self.skipped(event = nil, metadata: {}) = new(:skipped, event:, metadata:) |
.success(event = nil, metadata: {}) ⇒ ProcessorResult
Build a successful result.
25 |
# File 'lib/cdc/core/processor_result.rb', line 25 def self.success(event = nil, metadata: {}) = new(:success, event:, metadata:) |
Instance Method Details
#error_backtrace ⇒ Array<String>
Error backtrace, when present.
125 126 127 |
# File 'lib/cdc/core/processor_result.rb', line 125 def error_backtrace Array(error&.backtrace) end |
#error_class ⇒ String?
Error class name, when present.
111 112 113 |
# File 'lib/cdc/core/processor_result.rb', line 111 def error_class error&.class&.name end |
#error_message ⇒ String?
Error message, when present.
118 119 120 |
# File 'lib/cdc/core/processor_result.rb', line 118 def error&. end |
#failed_at ⇒ String?
Timestamp for when the failure occurred, when present.
104 105 106 |
# File 'lib/cdc/core/processor_result.rb', line 104 def failed_at [:failed_at] end |
#failure? ⇒ Boolean
Returns true when status is :failure.
75 |
# File 'lib/cdc/core/processor_result.rb', line 75 def failure? = status == :failure |
#failure_reason ⇒ String?
Human-readable failure reason, when present.
83 84 85 |
# File 'lib/cdc/core/processor_result.rb', line 83 def failure_reason [:reason] end |
#processor_name ⇒ String?
Name of the processor associated with the failure, when present.
97 98 99 |
# File 'lib/cdc/core/processor_result.rb', line 97 def processor_name [:processor] end |
#retryable? ⇒ Boolean
Whether the failure is retryable.
90 91 92 |
# File 'lib/cdc/core/processor_result.rb', line 90 def retryable? [:retryable] == true end |
#skipped? ⇒ Boolean
Returns true when status is :skipped.
78 |
# File 'lib/cdc/core/processor_result.rb', line 78 def skipped? = status == :skipped |
#success? ⇒ Boolean
Returns true when status is :success.
72 |
# File 'lib/cdc/core/processor_result.rb', line 72 def success? = status == :success |
#to_h ⇒ Hash{String=>Object,nil}
Convert the result into a shareable hash.
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/cdc/core/processor_result.rb', line 132 def to_h payload = { 'status' => status, 'event' => event&.to_h, 'error_class' => error_class, 'error_message' => , 'error_backtrace' => error_backtrace, 'metadata' => .to_h } Ractor.make_shareable(payload.freeze) end |