Class: Yes::Core::CommandHandling::CommandHandler
- Inherits:
-
Object
- Object
- Yes::Core::CommandHandling::CommandHandler
- Includes:
- OpenTelemetry::Trackable
- Defined in:
- lib/yes/core/command_handling/command_handler.rb
Overview
Handles the complete command execution flow for aggregates This class orchestrates command preparation, execution, and read model updates
Constant Summary collapse
- COMMAND_ATTRIBUTE =
Span attribute naming the command that ran. The span keeps a fixed name so its latency histogram stays one series per service; this is how the command is identified.
Not the bare
command: CommandCerbosAuthorizer already puts the serialised command on an attribute of that name, and a metrics pipeline that exports span attributes by name cannot tell the two apart. It would export the payload, one series per payload. 'command.name'
Instance Method Summary collapse
-
#call(command_name, payload, guards: true, metadata: nil) ⇒ Yes::Core::Commands::Response
Executes a command and updates the aggregate state.
-
#initialize(aggregate) ⇒ CommandHandler
constructor
Initializes a new CommandHandler.
Constructor Details
#initialize(aggregate) ⇒ CommandHandler
Initializes a new CommandHandler
27 28 29 30 31 |
# File 'lib/yes/core/command_handling/command_handler.rb', line 27 def initialize(aggregate) @aggregate = aggregate @command_utilities = aggregate.send(:command_utilities) @read_model = aggregate.read_model if aggregate.class.read_model_enabled? end |
Instance Method Details
#call(command_name, payload, guards: true, metadata: nil) ⇒ Yes::Core::Commands::Response
Executes a command and updates the aggregate state
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/yes/core/command_handling/command_handler.rb', line 40 def call(command_name, payload, guards: true, metadata: nil) prepared_payload = prepare_payload(command_name, payload, ) cmd = command_utilities.build_command(command_name, prepared_payload) otl_record_command(cmd) guard_evaluator_class = command_utilities.fetch_guard_evaluator_class(command_name) ReadModelRecoveryService.check_and_recover_with_retries(read_model, aggregate:) if aggregate.class.read_model_enabled? response = CommandExecutor.new(aggregate). call(cmd, command_name, guard_evaluator_class, skip_guards: !guards) ReadModelUpdater.new(aggregate).call(response.event, prepared_payload, command_name) if aggregate.class.read_model_enabled? && response.success? response end |