Class: Karafka::Web::Pro::Commanding::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/web/pro/commanding/dispatcher.rb

Overview

Dispatcher for sending commanding related messages. Those can be:

  • command (do something)

  • result (you wanted me to get you some info, here it is)

Dispatcher requires Web UI to have a producer

Constant Summary collapse

SCHEMA_VERSION =

What schema do we have in current Karafka version for commands dispatches

"1.2.0"

Class Method Summary collapse

Class Method Details

.acceptance(command_name, process_id, params = {}) ⇒ Object

Dispatches the acceptance info. Indicates that a command was received and appropriate action will be taken but async. Useful for commands that may not take immediate actions upon receiving a command.

Parameters:

  • command_name (String, Symbol)

    command that triggered this result

  • process_id (String)

    related process id

  • params (Hash) (defaults to: {})

    input command params (or empty hash if none)



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/karafka/web/pro/commanding/dispatcher.rb', line 74

def acceptance(command_name, process_id, params = {})
  produce_reply(
    process_id,
    "acceptance",
    {
      schema_version: SCHEMA_VERSION,
      type: "acceptance",
      id: SecureRandom.uuid,
      command: params.merge(name: command_name),
      dispatched_at: Time.now.to_f,
      process: {
        id: process_id
      }
    }
  )
end

.request(command_name, params = {}, matchers: {}) ⇒ Object

Dispatches the command request

Parameters:

  • command_name (String, Symbol)

    name of the command we want to deal with in the process

  • params (Hash) (defaults to: {})

    hash with extra command params that some commands may use.

  • matchers (Hash) (defaults to: {})

    hash with matching criteria for filtering which processes should handle this command.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/karafka/web/pro/commanding/dispatcher.rb', line 52

def request(command_name, params = {}, matchers: {})
  produce_request(
    {
      schema_version: SCHEMA_VERSION,
      type: "request",
      # UUID to uniquely identify this command instance
      id: SecureRandom.uuid,
      # Name is auto-generated and required. Should not be changed
      command: params.merge(name: command_name),
      dispatched_at: Time.now.to_f,
      matchers: matchers
    }
  )
end

.result(command_name, process_id, result) ⇒ Object

Dispatches the result request

Parameters:

  • command_name (String, Symbol)

    command that triggered this result

  • process_id (String)

    related process id

  • result (Object)

    anything that can be the result of the command execution



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/karafka/web/pro/commanding/dispatcher.rb', line 96

def result(command_name, process_id, result)
  produce_reply(
    process_id,
    "result",
    {
      schema_version: SCHEMA_VERSION,
      type: "result",
      id: SecureRandom.uuid,
      command: {
        name: command_name
      },
      result: result,
      dispatched_at: Time.now.to_f,
      process: {
        id: process_id
      }
    }
  )
end