Commits

Andy Gross  committed 4b28dd3

some better handling of connection errors

  • Participants
  • Parent commits 08e4688

Comments (0)

Files changed (2)

File src/example_gb.erl

          handle_info/2,
          terminate/2]).
 
+-export([get_messages/1]).
+
 -include_lib("gen_bunny.hrl").
 
 -record(state, {messages=[]}).
 
 start_link(Type) ->
     setup(Type),
-    gen_bunny:start_link(?MODULE, {direct, "guest", "guest2"}, <<"bunny.test">>, []).
+    gen_bunny:start_link(?MODULE, {direct, "guest", "guest"}, 
+                         <<"bunny.test">>, []).
 
 init([]) -> 
     {ok, #state{}}.
 
+get_messages(Pid) ->
+    gen_bunny:call(Pid, get_messages).
+
 handle_message(Message, State=#state{messages=Messages}) -> 
     NewMessages = [Message|Messages],
     {noreply, State#state{messages=NewMessages}}.
 
-handle_call(_Request, _From, State) -> {reply, ok, State}.
+handle_call(get_messages, _From, State=#state{messages=Messages}) -> 
+    {reply, Messages, State}.
 
 handle_cast(_Msg, State) -> {noreply, State}.
 
 handle_info(_Info, State) -> {noreply, State}.
 
-terminate(_Reason, _State) -> ok.
+terminate(Reason, _State) -> 
+    io:format("~p terminating with reason ~p~n", [?MODULE, Reason]),
+    ok.
 
 
 %%

File src/gen_bunny.erl

          terminate/2,
          code_change/3]).
 -export([behaviour_info/1]).
+
+-export([call/2,
+         call/3,
+         cast/2]).
+
 -export([get_connection/1,
          get_channel/1,
          get_consumer_tag/1,
       [Module,ConnectionInfo, QueueName, InitArgs], 
       []).
 
+call(Name, Request) ->
+    gen_server:call(Name, Request).
+
+call(Name, Request, Timeout) ->
+    gen_server:call(Name, Request, Timeout).
+
+cast(Dest, Request) ->
+    gen_server:cast(Dest, Request).
+
+
 init([Module, ConnectionInfo, QueueName, InitArgs]) ->
     case Module:init(InitArgs) of
         {ok, ModState} ->
                                 channel=ChannelPid,
                                 connection=ConnectionPid,
                                 queue=QueueName}};
-                Err ->
-                    Module:terminate(Err, ModState),
-                    Err
+                {_ErrClass, {error, Reason}} ->
+                    Module:terminate(Reason, ModState),
+                    {stop, Reason}
             end;
         Error ->
             Error
     
 
 terminate(Reason, #state{mod=Mod, modstate=ModState}) ->
+    io:format("gen_bunny terminating with reason ~p~n", [Reason]),
     Mod:terminate(Reason, ModState),
     ok.
 
 %% TODO: better error handling here.
 connect_and_subscribe({direct, Username, Password}, QueueName) ->
     %% TODO: link? 
-    ConnectionPid = amqp_connection:start_direct(Username, Password),
-    ChannelPid = amqp_connection:open_channel(ConnectionPid),
-    lib_amqp:subscribe(ChannelPid, QueueName, self()),
-    {ok, ChannelPid, ConnectionPid};
+    case catch amqp_connection:start_direct(Username, Password) of
+        {'EXIT', {Reason, _Stack}} ->
+            Reason;
+        ConnectionPid when is_pid(ConnectionPid) ->
+            ChannelPid = amqp_connection:open_channel(ConnectionPid),
+            lib_amqp:subscribe(ChannelPid, QueueName, self()),
+            {ok, ChannelPid, ConnectionPid}
+    end;
 connect_and_subscribe({network, Host, Port, Username, Password, VHost}, 
                       QueueName) ->
-    ConnectionPid = amqp_connection:start_network(Username, Password, Host,
-                                                  Port, VHost),
-    ChannelPid = amqp_connection:open_channel(ConnectionPid),
-    lib_amqp:subscribe(ChannelPid, QueueName, self()),
-    {ok, ChannelPid, ConnectionPid}.
+
+    case catch amqp_connection:start_direct(Username, Password, Host, 
+                                            Port, VHost) of
+        {'EXIT', {Reason, _Stack}} ->
+            Reason;
+        ConnectionPid when is_pid(ConnectionPid) ->
+            ChannelPid = amqp_connection:open_channel(ConnectionPid),
+            lib_amqp:subscribe(ChannelPid, QueueName, self()),
+            {ok, ChannelPid, ConnectionPid}
+    end.