Class: Yes::Core::Commands::Stateless::Handler

Inherits:
Object
  • Object
show all
Includes:
HandlerHelpers, OpenTelemetry::Trackable
Defined in:
lib/yes/core/commands/stateless/handler.rb

Overview

Handles stateless commands by publishing events to the event store without maintaining aggregate state in memory.

Examples:

class MyHandler < Yes::Core::Commands::Stateless::Handler
  self.event_name = 'Created'
end

Defined Under Namespace

Modules: RevisionsLoader Classes: InvalidTransition, NoChangeTransition, TransitionError

Constant Summary collapse

MISSING_CMDS_MSG =
'Commands missing'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HandlerHelpers

#activated?, #added?, #deactivated?, #disabled?, #enabled?, #event_exists_with_payload?, #event_in_stream?, #item_added?, #item_assigned?, #item_removed?, #item_unassigned?, #no_change?, #partial_payload_field_changed?, #published?, #removed?, #skip?, #unpublished?

Constructor Details

#initialize(cmd, events_cache: {}, revision_check: true, publish_events: true) ⇒ Stateless::Handler

Parameters:

  • cmd (Yes::Core::Command)
  • events_cache (Hash) (defaults to: {})

    already cached events { stream => { event_name => event_data } }

  • revision_check (Boolean) (defaults to: true)

    whether to check stream revisions before publishing

  • publish_events (Boolean) (defaults to: true)

    whether to actually publish events



86
87
88
89
90
91
92
# File 'lib/yes/core/commands/stateless/handler.rb', line 86

def initialize(cmd, events_cache: {}, revision_check: true, publish_events: true)
  @cmd = cmd
  @cmd_helper = Commands::Helper.new(cmd)
  @revision_check = revision_check
  @publish_events = publish_events
  @events_cache = events_cache
end

Class Attribute Details

.event_nameObject

Returns the value of attribute event_name.



66
67
68
# File 'lib/yes/core/commands/stateless/handler.rb', line 66

def event_name
  @event_name
end

.streamsObject

Returns the value of attribute streams.



66
67
68
# File 'lib/yes/core/commands/stateless/handler.rb', line 66

def streams
  @streams
end

Instance Attribute Details

#publish_eventsObject (readonly)

Returns the value of attribute publish_events.



74
75
76
# File 'lib/yes/core/commands/stateless/handler.rb', line 74

def publish_events
  @publish_events
end

#revision_checkObject (readonly)

Returns the value of attribute revision_check.



74
75
76
# File 'lib/yes/core/commands/stateless/handler.rb', line 74

def revision_check
  @revision_check
end

Class Method Details

.inherited(base) ⇒ Object



59
60
61
62
63
# File 'lib/yes/core/commands/stateless/handler.rb', line 59

def self.inherited(base)
  super

  base.prepend(RevisionsLoader)
end

.stateless?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/yes/core/commands/stateless/handler.rb', line 69

def stateless?
  true
end

Instance Method Details

#callvoid

This method returns an undefined value.



95
96
97
98
99
# File 'lib/yes/core/commands/stateless/handler.rb', line 95

def call
  return unless publish_events

  publish_event(self.class.event_name)
end

#publish_event(event_name) ⇒ PgEventstore::Event

Publishes a single event to the event store

Parameters:

  • event_name (String)

    the name of the event to publish

Returns:

  • (PgEventstore::Event)

    the published event



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/yes/core/commands/stateless/handler.rb', line 105

def publish_event(event_name)
  transaction.otl_contexts.publisher = self.class.propagate_context(service_name: true) if transaction&.otl_contexts

  type = event_type(event_name)
  event_class = events_module.const_get(event_name)
  event = event_class.new(
    type:,
    data: event_payload,
    metadata: 
  )
  otl_record_event_data(event)
  verify_revisions! if revision_check

  PgEventstore.client.append_to_stream(
    stream,
    event,
    options: { expected_revision: subject_stream_revision }
  ).tap { otl_record_response(_1) }
end