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.1.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)

[View source]

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

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

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

Dispatches the command request

Parameters:

  • command_name (String, Symbol)

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

  • process_id (String)

    id of the process. We use name instead of id only because in the web ui we work with the full name and it is easier. Since

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

    hash with extra command params that some commands may use.

[View source]

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/karafka/web/pro/commanding/dispatcher.rb', line 26

def request(command_name, process_id, params = {})
  produce(
    process_id,
    'request',
    {
      schema_version: SCHEMA_VERSION,
      type: 'request',
      # Name is auto-generated and required. Should not be changed
      command: params.merge(name: command_name),
      dispatched_at: Time.now.to_f,
      process: {
        id: process_id
      }
    }
  )
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

[View source]

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

def result(command_name, process_id, result)
  produce(
    process_id,
    'result',
    {
      schema_version: SCHEMA_VERSION,
      type: 'result',
      command: {
        name: command_name
      },
      result: result,
      dispatched_at: Time.now.to_f,
      process: {
        id: process_id
      }
    }
  )
end