TaskBunny v0.1.0-rc.1 TaskBunny.Connection
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.
Summary
Functions
Returns the RabbitMQ connection for the given host. When host argument is not passed it returns the connection for the default host
Similar to get_connection/1 but raises an exception when connection is not ready
Initialises GenServer. Send a request to establish a connection
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
Similar to subscribe_connection/2 but raises an exception when process is not ready.
Examples
Types
Functions
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
Similar to get_connection/1 but raises an exception when connection is not ready.
Examples
iex> conn = get_connection!()
%AMQP.Connection{}
Initialises GenServer. Send a request to establish a connection.
subscribe_connection(atom, pid) :: :ok | {:error, atom}
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