X3m.System.Message (X3m System v0.9.0)

Copy Markdown View Source

System Message.

This module defines a X3m.System.Message struct and the main functions for working with it.

Fields:

  • service_name - the name of the service that should handle this message. Example: :create_job.
  • id - unique id of the message.
  • correlation_id - id of the message that "started" conversation.
  • causation_id - id of the message that "caused" this message.
  • logger_metadata - In each new process Logger.metadata should be set to this value.
  • invoked_at - utc time when message was generated.
  • dry_run - specifies dry run option. It can be either false, true or :verbose.
  • request - request structure converted to Ecto.Changeset (or anything else useful).
  • raw_request - request as it is received before converting to Message (i.e. params from controller action).
  • assigns - shared Data as a map.
  • response - the response for invoker.
  • events - list of generated events.
  • aggregate_meta - metadata for aggregate.
  • valid? - when set to true it means that raw_request was successfully validated and structered request is set to request field
  • origin_node - Node.self() of invoker
  • reply_to - Pid of process that is waiting for response.
  • halted? - when set to true it means that response should be returned to the invoker without further processing of Message.

Summary

Functions

Adds event in message.events list. If event is nil it behaves as noop.

Assigns a value to a key in the message. The "assigns" storage is meant to be used to store values in the message so that others in pipeline can use them when needed. The assigns storage is a map.

Returns message it received with Response.created(id) result set.

Creates new message with given service_name and provided opts

Creates new message with given service_name that is caused by other msg.

Puts value under key in message.raw_request map.

Returns sys_msg with provided response and as halted? = true.

Types

dry_run()

@type dry_run() :: boolean() | :verbose

error()

@type error() :: {String.t(), Keyword.t()}

errors()

@type errors() :: [{atom(), error()}]

t()

@type t() :: %X3m.System.Message{
  aggregate_meta: map(),
  assigns: assigns(),
  causation_id: String.t(),
  correlation_id: String.t(),
  dry_run: dry_run(),
  events: [map()],
  halted?: boolean(),
  id: String.t(),
  invoked_at: DateTime.t(),
  logger_metadata: Keyword.t(),
  origin_node: Node.t(),
  raw_request: map(),
  reply_to: pid(),
  request: nil | request(),
  response: nil | X3m.System.Response.t(),
  service_name: atom(),
  valid?: boolean()
}

Functions

add_event(message, event)

@spec add_event(message :: t(), event :: nil | any()) :: t()

Adds event in message.events list. If event is nil it behaves as noop.

After return/2 (and friends) order of msg.events will be the same as they've been added.

assign(sys_msg, key, val)

@spec assign(t(), atom(), any()) :: t()

Assigns a value to a key in the message. The "assigns" storage is meant to be used to store values in the message so that others in pipeline can use them when needed. The assigns storage is a map.

Examples

iex> sys_msg.assigns[:user_id]
nil
iex> sys_msg = assign(sys_msg, :user_id, 123)
iex> sys_msg.assigns[:user_id]
123

created(message, id)

@spec created(t(), any()) :: t()

Returns message it received with Response.created(id) result set.

error(message, any)

@spec error(t(), any()) :: t()

gen_msg_id()

@spec gen_msg_id() :: String.t()

new(service_name, opts \\ [])

@spec new(atom(), Keyword.t()) :: t()

Creates new message with given service_name and provided opts:

  • id - id of the message. If not provided it generates random one.
  • correlation_id - id of "conversation". If not provided it is set to id.
  • causation_id - id of message that "caused" this message. If not provided it is set to id.
  • reply_to - sets pid of process that expects response. If not provided it is set to self().
  • raw_request - sets raw request as it is received (i.e. params from controller action).
  • logger_metadata - if not provided Logger.metadata is used by default.

new_caused_by(service_name, msg, opts \\ [])

@spec new_caused_by(atom(), t(), Keyword.t()) :: t()

Creates new message with given service_name that is caused by other msg.

ok(message)

@spec ok(t()) :: t()

ok(message, any)

@spec ok(t(), any()) :: t()

prepare_aggregate_id(message, id_field, opts \\ [])

put_in_raw_request(message, key, value)

Puts value under key in message.raw_request map.

put_request(request, message)

return(sys_msg, response)

@spec return(t(), X3m.System.Response.t()) :: t()

Returns sys_msg with provided response and as halted? = true.

to_service(sys_msg, service_name)

@spec to_service(t(), atom()) :: t()