@@ -32,6 +32,7 @@ resign(Pid) ->
32
32
% %%===================================================================
33
33
34
34
init ([Etcd , LeaderKey , Value ]) ->
35
+ logger :set_primary_config (#{level => info }),
35
36
erlang :process_flag (trap_exit , true ),
36
37
{ok , #{'ID' := LeaseID }} = eetcd_lease :grant (Etcd , 8 ),
37
38
{ok , _ } = eetcd_lease :keep_alive (Etcd , LeaseID ),
@@ -66,9 +67,11 @@ handle_info(Msg, State) ->
66
67
#{campaign := Campaign , observe := Observe } = State ,
67
68
case eetcd_election :campaign_response (Campaign , Msg ) of
68
69
{ok , NewCampaign = #{campaign := Leader }} ->
70
+ % % Only get this response when you win campaign by yourself.
71
+ % % You are leader!
69
72
win_campaign_event (Leader ),
70
73
{noreply , State #{campaign => NewCampaign }};
71
- {error , Reason } -> % % you can just let it crash and restart process
74
+ {error , Reason } -> % % you can just let it crash and restart process or recampaign !!!
72
75
campaign_unexpected_error (Reason ),
73
76
{noreply , State };
74
77
unknown ->
@@ -95,17 +98,22 @@ code_change(_OldVsn, State = #{}, _Extra) ->
95
98
% %%===================================================================
96
99
% %% Internal functions
97
100
% %%===================================================================
98
- win_campaign_event (_Leader ) ->
101
+ win_campaign_event (Leader ) ->
102
+ logger :info (" win campaign event:~p " , [Leader ]),
99
103
" Todo" .
100
104
101
- campaign_unexpected_error (_Reason ) ->
105
+ campaign_unexpected_error (Reason ) ->
106
+ logger :info (" campaign unexpected error:~p " , [Reason ]),
102
107
" Todo: try to recampaign" .
103
108
104
- leader_change_event (_Leader ) ->
109
+ leader_change_event (Leader ) ->
110
+ logger :info (" leader change event:~p " , [Leader ]),
105
111
" Todo" .
106
112
107
- observe_unexpected_error (_Reason ) ->
113
+ observe_unexpected_error (Reason ) ->
114
+ logger :info (" observe unexpect error:~p " , [Reason ]),
108
115
" Todo: try to reobserve after some sleep." .
109
116
110
- handle_info_your_own_msg (_Msg , _State ) ->
117
+ handle_info_your_own_msg (Msg , State ) ->
118
+ logger :info (" hanle info your own msg:~p ~p " , [Msg , State ]),
111
119
" Todo" .
0 commit comments