Class: SagaForge::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/saga_forge/base.rb

Overview

The saga DSL. Macros only record; Definition.compile (lazy, memoized) builds and validates the machine. The file IS the state machine.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.correlatorObject (readonly)

Returns the value of attribute correlator.



6
7
8
# File 'lib/saga_forge/base.rb', line 6

def correlator
  @correlator
end

.default_retry_policyObject (readonly)

Returns the value of attribute default_retry_policy.



6
7
8
# File 'lib/saga_forge/base.rb', line 6

def default_retry_policy
  @default_retry_policy
end

Class Method Details

.compensation(name, &block) ⇒ Object



29
30
31
# File 'lib/saga_forge/base.rb', line 29

def compensation(name, &block)
  declarations << {kind: :compensation, name: name.to_sym, block:}
end

.correlate_by(key = nil, &block) ⇒ Object



13
14
15
# File 'lib/saga_forge/base.rb', line 13

def correlate_by(key = nil, &block)
  @correlator = block || ->(payload, _event = nil) { payload[key] }
end

.declarationsObject



45
# File 'lib/saga_forge/base.rb', line 45

def declarations = @declarations ||= []

.definitionObject



47
# File 'lib/saga_forge/base.rb', line 47

def definition = @definition ||= Definition.compile(self)

.during(state, on:, compensate: nil, timeout: nil, on_timeout: nil, retry_policy: nil, &block) ⇒ Object



21
22
23
# File 'lib/saga_forge/base.rb', line 21

def during(state, on:, compensate: nil, timeout: nil, on_timeout: nil, retry_policy: nil, &block)
  declarations << {kind: :during, state: state.to_sym, event: on.to_sym, compensate:, timeout:, on_timeout:, retry_policy:, block:}
end

.find_by_correlation(correlation_id) ⇒ Object

Introspection & recovery (class-level; instance ops land in Task 11).



50
# File 'lib/saga_forge/base.rb', line 50

def find_by_correlation(correlation_id) = State.for_saga(self).find_by(correlation_id: correlation_id.to_s)

.finish_with(state) ⇒ Object



25
26
27
# File 'lib/saga_forge/base.rb', line 25

def finish_with(state)
  declarations << {kind: :finish, state: state.to_sym}
end

.in_state(state) ⇒ Object



52
# File 'lib/saga_forge/base.rb', line 52

def in_state(state) = State.for_saga(self).in_state(state)

.inherited(subclass) ⇒ Object



8
9
10
11
# File 'lib/saga_forge/base.rb', line 8

def inherited(subclass)
  super
  Router.register(subclass)
end

.retry_policy(*policies, **kwargs) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/saga_forge/base.rb', line 33

def retry_policy(*policies, **kwargs)
  if policies.any? && kwargs.any?
    raise ArgumentError, "pass either policy objects or kwargs, not both"
  end
  @default_retry_policy =
    if policies.any?
      (policies.size == 1 && kwargs.empty?) ? policies.first : CompositeRetryPolicy.new(policies)
    else
      RetryPolicy.new(**kwargs)
    end
end

.stalledObject



54
# File 'lib/saga_forge/base.rb', line 54

def stalled = State.for_saga(self).stalled

.start_with(event, compensate: nil, timeout: nil, on_timeout: nil, retry_policy: nil, &block) ⇒ Object



17
18
19
# File 'lib/saga_forge/base.rb', line 17

def start_with(event, compensate: nil, timeout: nil, on_timeout: nil, retry_policy: nil, &block)
  declarations << {kind: :start, event: event.to_sym, compensate:, timeout:, on_timeout:, retry_policy:, block:}
end

.suspendedObject



56
# File 'lib/saga_forge/base.rb', line 56

def suspended = State.for_saga(self).suspended

.to_mermaidObject



58
# File 'lib/saga_forge/base.rb', line 58

def to_mermaid = definition.to_mermaid