Gnat.ConsumerSupervisor (gnat v1.3.0) View Source

A process that can supervise consumers for you

If you want to subscribe to a few topics and have that subscription last across restarts for you, then this worker can be of help. It also spawns a supervised Task for each message it receives. This way errors in message processing don't crash the consumers, but you will still get SASL reports that you can send to services like honeybadger.

To use this just add an entry to your supervision tree like this:

consumer_supervisor_settings = %{
  connection_name: :name_of_supervised_connection,
  module: MyApp.Server, # a module that implements the Gnat.Server behaviour
  subscription_topics: [
    %{topic: "", queue_group: ""},
    %{topic: "rpc.MyApp.create", queue_group: "rpc.MyApp.create"},
worker(Gnat.ConsumerSupervisor, [consumer_supervisor_settings, [name: :rpc_consumer]], shutdown: 30_000)

The second argument is a keyword list that gets used as the GenServer options so you can pass a name that you want to register for the consumer process if you like. The :consuming_function specifies which module and function to call when messages arrive. The function will be called with a single argument which is a Gnat.message/0 just like you get when you call Gnat.sub/4 directly.

You can have a single consumer that subscribes to multiple topics or multiple consumers that subscribe to different topics and call different consuming functions. It is recommended that your ConsumerSupervisors are present later in your supervision tree than your ConnectionSupervisor. That way during a shutdown the ConsumerSupervisor can attempt a graceful shutdown of the consumer before shutting down the connection.

It's also possible to pass a %{consuming_function: {YourModule, :your_function}} rather than a :module in your settings. In that case no error handling or replying is taking care of for you, it will be up to your function to take whatever action you want with each message.

Link to this section Summary


Returns a specification to start this module under a supervisor.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

start_link(settings, options \\ [])

View Source


start_link(map(), keyword()) :: GenServer.on_start()