Class: Yes::Command::Api::Commands::Notifiers::MessageBus

Inherits:
Yes::Core::Commands::Notifier
  • Object
show all
Defined in:
lib/yes/command/api/commands/notifiers/message_bus.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ MessageBus

Returns a new instance of MessageBus.

Parameters:

  • options (Hash)

    the options to create a notifier with

Options Hash (options):

  • :channel (String)

    the channel name to publish notifications to



14
15
16
17
18
# File 'lib/yes/command/api/commands/notifiers/message_bus.rb', line 14

def initialize(options)
  super()

  @channel = options[:channel]
end

Instance Method Details

#notify_batch_finished(batch_id, transaction = nil, responses = nil) ⇒ Object

Parameters:

  • batch_id (String)

    the id of the batch that has finished processing

  • transaction (TransactionDetails) (defaults to: nil)

    the transaction details of the current transaction

  • responses (Array<Response>) (defaults to: nil)

    the responses of the commands that were processed



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yes/command/api/commands/notifiers/message_bus.rb', line 41

def notify_batch_finished(batch_id, transaction = nil, responses = nil)
  user_ids = [transaction&.caller_id].compact

  data = {
    type: 'batch_finished',
    batch_id:,
    published_at:
  }.merge(
    transaction: transaction.to_h
  ).merge(failed_commands_data(responses))

  ::MessageBus.publish(channel, data, user_ids: user_ids.empty? ? nil : user_ids)
end

#notify_batch_started(batch_id, transaction = nil, commands = nil) ⇒ Object

Parameters:

  • batch_id (String)

    the id of the batch that has started processing

  • transaction (TransactionDetails) (defaults to: nil)

    the transaction details of the current transaction

  • commands (Array<Command>) (defaults to: nil)

    the commands that are being processed



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/yes/command/api/commands/notifiers/message_bus.rb', line 24

def notify_batch_started(batch_id, transaction = nil, commands = nil)
  user_ids = [transaction&.caller_id].compact

  data =
    {
      batch_id:, published_at:, type: 'batch_started'
    }.merge(
      transaction: transaction.to_h
    ).merge(commands_data(commands))

  ::MessageBus.publish(channel, data, user_ids: user_ids.empty? ? nil : user_ids)
end

#notify_command_response(cmd_response) ⇒ Object

Parameters:

  • cmd_response (Yes::Core::Commands::Response)

    the command response to notify



57
58
59
60
61
62
63
64
65
# File 'lib/yes/command/api/commands/notifiers/message_bus.rb', line 57

def notify_command_response(cmd_response)
  user_ids = [cmd_response.transaction&.caller_id].compact

  ::MessageBus.publish(
    channel,
    cmd_response.to_notification.merge(published_at:),
    user_ids: user_ids.empty? ? nil : user_ids
  )
end