Created
May 14, 2017 11:21
-
-
Save gottfrois/698ec91f2f1414f871bedb1f5b5b0af0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Rabbit.ConnectionWorker do | |
use Connection | |
def start_link(opts) do | |
Connection.start_link(__MODULE__, opts) | |
end | |
def init(opts) do | |
state = %{opts: opts, conn: nil} | |
{:connect, nil, state} | |
end | |
def disconnect(_info, state) do | |
{:connect, :reconnect, %{state | conn: nil}} | |
end | |
def connect(_info, state = %{conn: nil, opts: %{host: host, port: port}}) do | |
case AMQP.Connection.open( | |
host: host, | |
port: port, | |
connection_timeout: 5000, | |
heartbeat: 60 | |
) do | |
{:ok, conn = %AMQP.Connection{pid: pid}} -> | |
Process.link(pid) | |
{:ok, %{state | conn: conn}} | |
{:error, _reason} -> | |
{:backoff, 1000, state} | |
end | |
end | |
def handle_call(_, _, state = %{conn: nil}) do | |
{:reply, {:error, :closed}, state} | |
end | |
def handle_call(:close, from, state) do | |
{:disconnect, {:close, from}, state} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment