Bump irc_amqp_listener state version to 3.
authorMatt Mullins <mmullins@mmlx.us>
Sat, 4 May 2013 06:56:59 +0000 (23:56 -0700)
committerMatt Mullins <mmullins@mmlx.us>
Sun, 5 May 2013 07:19:07 +0000 (00:19 -0700)
Convert version 2 state to version 3 by hoping that the listener is only
monitoring one process.  Thankfully, amqp_bot_listener:listen_for_events
creates a monitor so that AMQP will kill IRC.

irc/irc_amqp_listener.erl

index da33e92..f615dc3 100644 (file)
@@ -2,7 +2,7 @@
 %% as PRIVMSG to a particular destination
 -module(irc_amqp_listener).
 -behavior(gen_server).
--vsn(2).
+-vsn(3).
 
 -define(EXCHANGE, <<"irc">>).
 
@@ -15,7 +15,8 @@
 
 -export([
           init/1,
-          handle_info/2
+          handle_info/2,
+          code_change/3
         ]).
 
 -include("irc_util.hrl").
@@ -47,6 +48,17 @@ handle_info(
     irc_conn:send_command(ConnectionPid, Command),
     {noreply, State}.
 
+code_change(2, {ConnectionPid}, _) ->
+    case process_info(self(), monitors) of
+        {monitors, [{process, ListeningChannel}]} ->
+            {ok, {ConnectionPid, ListeningChannel}};
+        _ ->
+            {error, cant_find_listening_channel}
+    end;
+
+code_change({down, 2}, {ConnectionPid, _ListeningChannel}, _) ->
+    {ok, {ConnectionPid}}.
+
 %% @doc Sends a message with the given routing key and body to the proper
 %% exchange
 send_message(RoutingKey, ReplyTo, Body) ->