2424 write_cluster_status /1 , read_cluster_status /0 ,
2525 update_cluster_status /0 , reset_cluster_status /0 ]).
2626-export ([notify_node_up /0 , notify_joined_cluster /0 , notify_left_cluster /1 ]).
27- -export ([partitions /0 ]).
27+ -export ([partitions /0 , subscribe / 1 ]).
2828
2929% % gen_server callbacks
3030-export ([init /1 , handle_call /3 , handle_cast /2 , handle_info /2 , terminate /2 ,
3333-define (SERVER , ? MODULE ).
3434-define (RABBIT_UP_RPC_TIMEOUT , 2000 ).
3535
36- -record (state , {monitors , partitions }).
36+ -record (state , {monitors , partitions , subscribers }).
3737
3838% %----------------------------------------------------------------------------
3939
5454-spec (notify_left_cluster / 1 :: (node ()) -> 'ok' ).
5555
5656-spec (partitions / 0 :: () -> {node (), [{atom (), node ()}]}).
57+ -spec (subscribe / 1 :: (pid ()) -> 'ok' ).
5758
5859-endif .
5960
@@ -179,6 +180,9 @@ notify_left_cluster(Node) ->
179180partitions () ->
180181 gen_server :call (? SERVER , partitions , infinity ).
181182
183+ subscribe (Pid ) ->
184+ gen_server :cast (? SERVER , {subscribe , Pid }).
185+
182186% %----------------------------------------------------------------------------
183187% % gen_server callbacks
184188% %----------------------------------------------------------------------------
@@ -190,8 +194,9 @@ init([]) ->
190194 % % happen.
191195 process_flag (trap_exit , true ),
192196 {ok , _ } = mnesia :subscribe (system ),
193- {ok , # state {monitors = pmon :new (),
194- partitions = []}}.
197+ {ok , # state {monitors = pmon :new (),
198+ subscribers = pmon :new (),
199+ partitions = []}}.
195200
196201handle_call (partitions , _From , State = # state {partitions = Partitions }) ->
197202 {reply , {node (), Partitions }, State };
@@ -232,17 +237,24 @@ handle_cast({left_cluster, Node}, State) ->
232237 write_cluster_status ({del_node (Node , AllNodes ), del_node (Node , DiscNodes ),
233238 del_node (Node , RunningNodes )}),
234239 {noreply , State };
240+ handle_cast ({subscribe , Pid }, State = # state {subscribers = Subscribers }) ->
241+ {noreply , State # state {subscribers = pmon :monitor (Pid , Subscribers )}};
235242handle_cast (_Msg , State ) ->
236243 {noreply , State }.
237244
238245handle_info ({'DOWN' , _MRef , process , {rabbit , Node }, _Reason },
239- State = # state {monitors = Monitors }) ->
246+ State = # state {monitors = Monitors , subscribers = Subscribers }) ->
240247 rabbit_log :info (" rabbit on node ~p down~n " , [Node ]),
241248 {AllNodes , DiscNodes , RunningNodes } = read_cluster_status (),
242249 write_cluster_status ({AllNodes , DiscNodes , del_node (Node , RunningNodes )}),
243250 ok = handle_dead_rabbit (Node ),
251+ [P ! {node_down , Node } || P <- pmon :monitored (Subscribers )],
244252 {noreply , State # state {monitors = pmon :erase ({rabbit , Node }, Monitors )}};
245253
254+ handle_info ({'DOWN' , _MRef , process , Pid , _Reason },
255+ State = # state {subscribers = Subscribers }) ->
256+ {noreply , State # state {subscribers = pmon :erase (Pid , Subscribers )}};
257+
246258handle_info ({mnesia_system_event ,
247259 {inconsistent_database , running_partitioned_network , Node }},
248260 State = # state {partitions = Partitions }) ->
0 commit comments