Mississippi.Consumer.DataUpdater.Handler behaviour (Mississippi v1.0.0)

View Source

A behaviour module to for implementing a Processor of Mississippi messages. Messages sharing the same sharding key are processed in order.

Summary

Callbacks

Invoked when handle_message/5 returns a {:continue, continue_arg} tuple. Used to update the state of a stateful handler.

Invoked when a message is received. Possible return values are

Invoked when an information that is not a message is received. Used to update the state of a stateful handler. There is no guarantee of ordering.

Invoked when the handler is initialized. This can be used to make the handler stateful. The sharding key for this Handler is passed as an argument to the callback. An {:error, reason} return value will make handler initialization fail. In that case, the handler will be restarted and messages which have been prefetched will be requed in the original order.

Invoked when the handler is being terminated. It can be used to perform cleanup tasks before closing.

Types

handler_state()

@type handler_state() :: term()

Callbacks

handle_continue(continue_arg, state)

@callback handle_continue(continue_arg :: term(), state :: handler_state()) ::
  {:ok, new_state :: handler_state()}

Invoked when handle_message/5 returns a {:continue, continue_arg} tuple. Used to update the state of a stateful handler.

handle_message(payload, headers, message_id, timestamp, state)

@callback handle_message(
  payload :: term(),
  headers :: map(),
  message_id :: binary(),
  timestamp :: term(),
  state :: handler_state()
) ::
  {:ack, result :: term(), new_state :: handler_state()}
  | {:ack, result :: term(), new_state :: handler_state(),
     {:continue, continue_arg :: term()}}
  | {:discard, reason :: term(), new_state :: handler_state()}
  | {:discard, reason :: term(), new_state :: handler_state(),
     {:continue, continue_arg :: term()}}
  | {:stop, reason :: term(), action :: :ack | :discard,
     new_state :: handler_state()}

Invoked when a message is received. Possible return values are:

  • {:ack, result, state}: Mississippi acks the message
  • {:ack, result, state, {:continue, continue_arg}}: Mississippi acks the message and the DataUpdater process continues as specified by the handle_continue/2 callback
  • {:discard, reason, state}: Mississippi discards the message
  • {:discard, result, state, {:continue, continue_arg}}: Mississippi discards the message and the DataUpdater process continues as specified by the handle_continue/2 callback
  • {:stop, reason, action, state}: Mississippi acks or discards the message according to action and then the DataUpdater process terminates normally. This makes the related MessageTracker terminate, too.

handle_signal(signal, state)

@callback handle_signal(signal :: term(), state :: handler_state()) ::
  {result :: term(), new_state :: handler_state()}

Invoked when an information that is not a message is received. Used to update the state of a stateful handler. There is no guarantee of ordering.

init(sharding_key)

@callback init(sharding_key :: term()) ::
  {:ok, state :: handler_state()} | {:error, reason :: term()}

Invoked when the handler is initialized. This can be used to make the handler stateful. The sharding key for this Handler is passed as an argument to the callback. An {:error, reason} return value will make handler initialization fail. In that case, the handler will be restarted and messages which have been prefetched will be requed in the original order.

terminate(reason, state)

@callback terminate(reason :: term(), state :: handler_state()) :: :ok

Invoked when the handler is being terminated. It can be used to perform cleanup tasks before closing.