Class: Yes::Core::Commands::Processor

Inherits:
ActiveJob::Base
  • Object
show all
Includes:
OpenTelemetry::Trackable
Defined in:
lib/yes/core/commands/processor.rb

Overview

Processes commands asynchronously through ActiveJob

Since:

  • 0.1.0

Constant Summary collapse

UnregisteredCommand =

Error raised when a command is not registered with a handler

Since:

  • 0.1.0

Class.new(Error)

Instance Method Summary collapse

Instance Method Details

#perform(origin, command_or_commands, notifier_options, custom_batch_id = nil, inline = false) ⇒ Array<Response>

Processes the given commands by running them through their respective handlers.

Parameters:

  • origin (String)

    a string identifying the origin of the commands

  • command_or_commands (Command, Array<Command>)

    the command or commands to process

  • notifier_options (Hash)

    options to pass to the command notifier

  • custom_batch_id (String) (defaults to: nil)

    Custom batch ID

Returns:

  • (Array<Response>)

    the responses from the performed commands

Raises:

Since:

  • 0.1.0



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yes/core/commands/processor.rb', line 27

def perform(origin, command_or_commands, notifier_options, custom_batch_id = nil, inline = false)
  setup(notifier_options, custom_batch_id, inline)
  singleton_class.current_span&.add_event('Command Processor Setup Done')

  commands = [*command_or_commands]
  ensure_guard_evaluators_exist?(commands)
  singleton_class.current_span&.add_event('Ensured Guard Evaluators Exist')

  commands.map! { |cmd| cmd.class.new(cmd.to_h.merge(origin:, batch_id:)) }
  singleton_class.current_span&.add_event('Commands Mapped')

  if command_notifiers.any?
    singleton_class.with_otl_span 'Run Commands With Notifiers' do
      Notifier.with_batch_notification(command_notifiers, batch_id, commands) do
        singleton_class.with_otl_span 'Run Commands' do
          run_commands(commands)
        end
      end
    end
  else
    singleton_class.with_otl_span 'Run Commands' do
      run_commands(commands)
    end
  end
end