Handle up/down-grade for the IRC application -- add/remove AMQP processes
authorMatt Mullins <mmullins@mmlx.us>
Sat, 3 Sep 2011 03:39:53 +0000 (22:39 -0500)
committerMatt Mullins <mmullins@mmlx.us>
Sat, 3 Sep 2011 03:39:53 +0000 (22:39 -0500)
irc/irc.appup
irc/irc_conn.erl
irc/irc_net_sup.erl

index b6f474a..9af27b3 100644 (file)
@@ -1,4 +1,15 @@
-{"3",
- [{"2", [{load_module, irc_sup}]}],
- [{"2", [{load_module, irc_sup}]}]
+{"4",
+ [{"3", [{add_module, irc_object_sup},
+         {add_module, irc_amqp_listener},
+         {update, irc_sup, supervisor},
+         {update, irc_net_sup, supervisor},
+         {update, irc_conn, {advanced, none}}
+        ]}],
+
+ [{"3", [{update, irc_conn, {advanced, none}},
+         {delete_module, irc_object_sup},
+         {delete_module, irc_amqp_listener},
+         {update, irc_sup, supervisor},
+         {update, irc_net_sup, supervisor}
+        ]}]
 }.
index 6d79256..e3865c1 100644 (file)
@@ -128,8 +128,50 @@ do_privmsg(_Command = #irc_command{middles = Middles, trailing = Text}) ->
 terminate(_Reason, _State) ->
     ok.
 
-code_change(_OldVsn, State, _Extra) ->
-    {ok, State}.
+code_change(1, {irc_state, Instance, Config, Joined, Socket, Buffer} = State, _Extra) ->
+    error_logger:info_msg("Converting from version 1 to version 2.~nOld state is~p~n",
+                          [State]),
+
+    [Supervisor|_] = get('$ancestors'),
+    [TableId|_] = [Tab || Tab <- ets:all(), Supervisor == ets:info(Tab, owner)],
+    {ok, ObjectSupPid} = irc_net_sup:create_object_sup(Instance, Supervisor, TableId),
+
+    NewState = {irc_state, Instance,
+                           Config,
+                           Joined,
+                           Socket,
+                           Buffer,
+                           Supervisor,
+                           TableId,
+                           ObjectSupPid},
+
+    error_logger:info_msg("State transition is done.~nNew state is ~p~n",
+                          [NewState]),
+
+    {_, _, _, _, Channels} = Config,
+    lists:foreach(fun (Channel) -> add_amqp_listener(Channel, NewState) end, Channels),
+
+    error_logger:info_msg("All done starting new version"),
+
+    {ok, NewState};
+
+code_change({down, 1}, State, _Extra) ->
+    error_logger:info_msg("Converting version 2 to version 1.~nOld state is ~p~n",
+                          [State]),
+
+    #irc_state{instance = Instance,
+               config = Config,
+               joined = Joined,
+               socket = Socket,
+               buffer = Buffer,
+               supervisor = Supervisor} = State,
+
+    irc_net_sup:remove_object_sup(Supervisor),
+
+    NewState = {irc_state, Instance, Config, Joined, Socket, Buffer},
+    error_logger:info_msg("New state is ~p~n", [NewState]),
+
+    {ok, NewState}.
 
 send_command(Command) ->
     gen_server:cast(self(), {send_command, Command}).
@@ -137,6 +179,9 @@ send_command(Command) ->
 join_channel(Channel, State) ->
     Command = #irc_command{command = "JOIN", middles = [Channel]},
     send_command(Command),
+    add_amqp_listener(Channel, State).
+
+add_amqp_listener(Channel, State) ->
     #irc_state{object_sup = ObjectSup,
                instance = Instance
               } = State,
index 4938c41..e779fdc 100644 (file)
@@ -8,7 +8,8 @@
 
 -export([
           start_link/1,
-          create_object_sup/3
+          create_object_sup/3,
+          remove_object_sup/1
                ]).
 -export([init/1]).
 
@@ -41,3 +42,8 @@ create_object_sup(Instance, Supervisor, TableId) ->
                 },
     {ok, ObjectSupPid} = supervisor:start_child(Supervisor, ObjectSup),
     {ok, ObjectSupPid}.
+
+%% @doc Kills the irc_object_sup running under this supervisor
+remove_object_sup(Supervisor) ->
+    supervisor:terminate_child(Supervisor, object_sup),
+    supervisor:delete_child(Supervisor, object_sup).