refactor(listeners): use emqx_ctl to print cli messages
It's better to keep cli print behaviour consistent. It also makes tests easier as they can meck emqx_ctl:print to validate the output.
This commit is contained in:
parent
2d79c870c1
commit
dfa9bbc0c2
|
@ -554,12 +554,17 @@ listeners(_) ->
|
||||||
]).
|
]).
|
||||||
|
|
||||||
stop_listener(false, Input) ->
|
stop_listener(false, Input) ->
|
||||||
emqx_ctl:print("No such listener ~p~n", [Input]);
|
ok = emqx_ctl:print("No such listener ~p~n", [Input]);
|
||||||
stop_listener(#{} = Listener, _Input) ->
|
stop_listener(#{listen_on := ListenOn} = Listener, _Input) ->
|
||||||
%% Discard reason here, reasons are io:format logged to group leader
|
ID = emqx_listeners:identifier(Listener),
|
||||||
%% in case of emqx_ctl RPC call, it's logged to the remore node.
|
ListenOnStr = emqx_listeners:format_listen_on(ListenOn),
|
||||||
_ = emqx_listeners:stop_listener(Listener),
|
case emqx_listeners:stop_listener(Listener) of
|
||||||
ok.
|
ok ->
|
||||||
|
ok = emqx_ctl:print("Stop ~s listener on ~s successfully.~n", [ID, ListenOnStr]);
|
||||||
|
{error, Reason} ->
|
||||||
|
ok = emqx_ctl:print("Failed to stop ~s listener on ~s - ~p~n.",
|
||||||
|
[ID, ListenOnStr, Reason])
|
||||||
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @doc data Command
|
%% @doc data Command
|
||||||
|
|
|
@ -28,7 +28,6 @@
|
||||||
-export([ start_listener/1
|
-export([ start_listener/1
|
||||||
, start_listener/3
|
, start_listener/3
|
||||||
, stop_listener/1
|
, stop_listener/1
|
||||||
, stop_listener/3
|
|
||||||
, restart_listener/1
|
, restart_listener/1
|
||||||
, restart_listener/3
|
, restart_listener/3
|
||||||
]).
|
]).
|
||||||
|
@ -37,6 +36,7 @@
|
||||||
, find_by_listen_on/1
|
, find_by_listen_on/1
|
||||||
, find_by_id/1
|
, find_by_id/1
|
||||||
, identifier/1
|
, identifier/1
|
||||||
|
, format_listen_on/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-type(listener() :: #{ name := binary()
|
-type(listener() :: #{ name := binary()
|
||||||
|
@ -76,6 +76,10 @@ identifier(#{proto := Proto, name := Name}) ->
|
||||||
start() ->
|
start() ->
|
||||||
lists:foreach(fun start_listener/1, emqx:get_env(listeners, [])).
|
lists:foreach(fun start_listener/1, emqx:get_env(listeners, [])).
|
||||||
|
|
||||||
|
%% @doc Format address:port for logging.
|
||||||
|
-spec(format_listen_on(esockd:listen_on()) -> binary()).
|
||||||
|
format_listen_on(ListenOn) -> format(ListenOn).
|
||||||
|
|
||||||
-spec(start_listener(listener()) -> ok).
|
-spec(start_listener(listener()) -> ok).
|
||||||
start_listener(#{proto := Proto, name := Name, listen_on := ListenOn, opts := Options}) ->
|
start_listener(#{proto := Proto, name := Name, listen_on := ListenOn, opts := Options}) ->
|
||||||
ID = identifier(Proto, Name),
|
ID = identifier(Proto, Name),
|
||||||
|
@ -171,16 +175,8 @@ stop() ->
|
||||||
lists:foreach(fun stop_listener/1, emqx:get_env(listeners, [])).
|
lists:foreach(fun stop_listener/1, emqx:get_env(listeners, [])).
|
||||||
|
|
||||||
-spec(stop_listener(listener()) -> ok | {error, term()}).
|
-spec(stop_listener(listener()) -> ok | {error, term()}).
|
||||||
stop_listener(#{proto := Proto, name := Name, listen_on := ListenOn, opts := Opts}) ->
|
stop_listener(#{proto := Proto, listen_on := ListenOn, opts := Opts}) ->
|
||||||
ID = identifier(Proto, Name),
|
stop_listener(Proto, ListenOn, Opts).
|
||||||
StopRet = stop_listener(Proto, ListenOn, Opts),
|
|
||||||
case StopRet of
|
|
||||||
ok -> io:format("Stop ~s listener on ~s successfully.~n", [ID, format(ListenOn)]);
|
|
||||||
{error, Reason} ->
|
|
||||||
io:format(standard_error, "Failed to stop mqtt:~s listener on ~s - ~p~n.",
|
|
||||||
[ID, format(ListenOn), Reason])
|
|
||||||
end,
|
|
||||||
StopRet.
|
|
||||||
|
|
||||||
-spec(stop_listener(esockd:proto(), esockd:listen_on(), [esockd:option()])
|
-spec(stop_listener(esockd:proto(), esockd:listen_on(), [esockd:option()])
|
||||||
-> ok | {error, term()}).
|
-> ok | {error, term()}).
|
||||||
|
|
Loading…
Reference in New Issue