View Source Gnat.ConsumerSupervisor (gnat v1.9.1)
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: "rpc.MyApp.search", queue_group: "rpc.MyApp.search"},
%{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 ConsumerSupervisor
s 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.
If you want this consumer supervisor to host a NATS service, then you can specify a module that
implements the Gnat.Services.Server
behavior. You'll need to specify the service_definition
field in the consumer
supervisor settings and conforms to the Gnat.Services.Server.service_configuration
type. Here is an example of configuring
the consumer supervisor to manage a service:
consumer_supervisor_settings = %{
connection_name: :name_of_supervised_connection,
module: MyApp.Service, # a module that implements the Gnat.Services.Server behaviour
service_definition: %{
name: "exampleservice",
description: "This is an example service",
version: "0.1.0",
endpoints: [
%{
name: "add",
group_name: "calc",
},
%{
name: "sub",
group_name: "calc"
}
]
}
}
worker(Gnat.ConsumerSupervisor, [consumer_supervisor_settings, [name: :myservice_consumer]], shutdown: 30_000)
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, microservices cannot be used, and it will be up to your function to take whatever action you want with each message.
Summary
Functions
Returns a specification to start this module under a supervisor.
Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
@spec start_link( map(), keyword() ) :: GenServer.on_start()