TaskBunny v0.1.0-rc.2 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:

  1. 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.

  2. 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

Types

Represents the state of a connection GenServer

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

state()
state() :: {atom, %AMQP.Connection{pid: term} | nil, [pid]}

Represents the state of a connection GenServer.

It’s a tuple containing {host, connection, subscribers}.

Functions

get_connection(host \\ :default)
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
get_connection!(host \\ :default)
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{}
init(args)
init(tuple) :: {:ok, any}

Initialises GenServer. Send a request to establish a connection.

subscribe_connection(host \\ :default, listener_pid)
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
subscribe_connection!(host \\ :default, listener_pid)
subscribe_connection!(atom, pid) :: :ok

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