TaskBunny v0.3.4 TaskBunny.Connection View Source
A GenServer that handles RabbitMQ connection. It provides convenience functions to access RabbitMQ through the GenServer.
GenServer
TaskBunny loads the configurations and automatically starts a GenServer for each host definition. They are supervised by TaskBunny so you don’t have to look after them.
Disconnect/Reconnect
TaskBunny handles disconnection and reconnection. Once the GenServer retrieves the RabbitMQ connection the GenServer monitors it. When it disconnects or dies the GenServer terminates itself.
The supervisor restarts the GenServer and it tries to reconnect to the host. If it fails to connect, it retries every five seconds.
Access to RabbitMQ connections
The module provides two ways to retrieve a RabbitMQ connection:
Use
get_connection/1
and it returns the connection synchronously. This will succeed in most cases since TaskBunny tries to establish a connection as soon as the application starts.Use
subscribe_connection/1
and it sends the connection back asynchronously once the connection is ready. This can be useful when you can’t ensure the caller might start before the connectin is established.
Check out the function documentation for more details.
Link to this section Summary
Functions
Returns a specification to start this module under a supervisor
Similar to get_connection/1 but raises an exception when connection is not ready
Returns the RabbitMQ connection for the given host. When host argument is not passed it returns the connection for the default host
Initialises GenServer. Send a request to establish a connection
Similar to subscribe_connection/2 but raises an exception when process is not ready.
Examples
Requests the GenServer to send the connection back asynchronously. Once connection has been established, it will send a message with {:connected, connection} to the given process
Link to this section Types
Represents the state of a connection GenServer.
It’s a tuple containing {host, connection, subscribers}
.
Link to this section Functions
Returns a specification to start this module under a supervisor.
See Supervisor
.
get_connection!(atom()) :: AMQP.Connection.t()
Similar to get_connection/1 but raises an exception when connection is not ready.
Examples
iex> conn = get_connection!()
%AMQP.Connection{}
get_connection(atom()) :: {:ok, AMQP.Connection.t()} | {:error, atom()}
Returns the RabbitMQ connection for the given host. When host argument is not passed it returns the connection for the default host.
Examples
case get_connection() do
{:ok, conn} -> do_something(conn)
{:error, _} -> cry()
end
Initialises GenServer. Send a request to establish a connection.
Similar to subscribe_connection/2 but raises an exception when process is not ready.
Examples
subscribe_connection!(self())
receive do
{:connected, conn = %AMQP.Connection{}} -> do_something(conn)
end
Requests the GenServer to send the connection back asynchronously. Once connection has been established, it will send a message with {:connected, connection} to the given process.
Examples
:ok = subscribe_connection(self())
receive do
{:connected, conn = %AMQP.Connection{}} -> do_something(conn)
end