View Source Polyn.PullConsumer behaviour (Polyn v0.1.0)

Use Polyn.PullConsumer to connect and process messages from an existing NATS Consumer that was setup with Polyn CLI. This module is a wrapper around Jetstream.PullConsumer that does schema validation with the received messages. This type of Consumer is meant for simple use cases that don't involve concurrency or batching.

Link to this section Summary

Callbacks

Invoked to synchronously process a message pulled by the consumer. Depending on the value it returns, the acknowledgement is or is not sent. Polyn will deserialize the message body into a Polyn.Event struct and use that as the first argument, followed by the original message, follwed by the state.

Invoked when the server is started. start_link/3 or start/3 will block until it returns.

Functions

Returns a specification to start this module under a supervisor.

Closes the pull consumer and stops underlying process.

Starts a Jetstream.PullConsumer process without links (outside of a supervision tree).

Starts a pull consumer linked to the current process with the given function.

Link to this section Callbacks

Link to this callback

handle_message(event, message, state)

View Source
@callback handle_message(
  event :: Polyn.Event.t(),
  message :: Jetstream.message(),
  state :: term()
) :: {ack_action, new_state}
when ack_action: :ack | :nack | :term | :noreply, new_state: term()

Invoked to synchronously process a message pulled by the consumer. Depending on the value it returns, the acknowledgement is or is not sent. Polyn will deserialize the message body into a Polyn.Event struct and use that as the first argument, followed by the original message, follwed by the state.

ack-actions

ACK actions

See Jetstream.PullConsumer.handle_message/2 for available options

example

Example

def handle_message(event, _message, state) do
  IO.inspect(event)
  {:ack, state}
end
@callback init(init_arg :: term()) ::
  {:ok, state :: term(), Jetstream.PullConsumer.connection_options()}
  | :ignore
  | {:stop, reason :: any()}

Invoked when the server is started. start_link/3 or start/3 will block until it returns.

init_arg is the argument term (second argument) passed to start_link/3.

See Connection.init/1 for more details.

Link to this section Functions

@spec child_spec(arg :: GenServer.options()) :: Supervisor.child_spec()

Returns a specification to start this module under a supervisor.

See the "Child specification" section in the Supervisor module for more detailed information.

@spec close(consumer :: Jetstream.PullConsumer.consumer()) :: :ok

Closes the pull consumer and stops underlying process.

example

Example

{:ok, consumer} =
  PullConsumer.start_link(ExamplePullConsumer,
    connection_name: :gnat,
    stream_name: "TEST_STREAM",
    consumer_name: "TEST_CONSUMER"
  )

:ok = PullConsumer.close(consumer)
Link to this function

start(module, init_arg, options \\ [])

View Source
@spec start(module(), init_arg :: term(), options :: GenServer.options()) ::
  GenServer.on_start()

Starts a Jetstream.PullConsumer process without links (outside of a supervision tree).

See start_link/3 for more information.

Link to this function

start_link(module, init_arg, options \\ [])

View Source
@spec start_link(module(), init_arg :: term(), options :: GenServer.options()) ::
  GenServer.on_start()

Starts a pull consumer linked to the current process with the given function.

This is often used to start the pull consumer as part of a supervision tree.

Once the server is started, the init/1 function of the given module is called with init_arg as its argument to initialize the server. To ensure a synchronized start-up procedure, this function does not return until init/1 has returned.

See GenServer.start_link/3 for more details.