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:

  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.

Link to this section Summary

Types

Represents the state of a connection GenServer

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

Link to this type state() View Source
state() :: {atom(), %AMQP.Connection{pid: term()} | nil, [pid()]}

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.

Link to this function get_connection!(host \\ :default) View Source
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{}
Link to this function get_connection(host \\ :default) View Source
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
Link to this function init(state) View Source
init(tuple()) :: {:ok, any()}

Initialises GenServer. Send a request to establish a connection.

Link to this function subscribe_connection!(host \\ :default, listener_pid) View Source
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
Link to this function subscribe_connection(host \\ :default, listener_pid) View Source
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