fix: return 503 if dashboard's router not ready

This commit is contained in:
Zhongwen Deng 2022-08-31 15:42:39 +08:00
parent 7041378c80
commit 1cb281fbc4
2 changed files with 24 additions and 16 deletions

View File

@ -23,7 +23,7 @@
execute(Req, Env) -> execute(Req, Env) ->
case check_dispatch_ready(Env) of case check_dispatch_ready(Env) of
true -> add_cors_flag(Req, Env); true -> add_cors_flag(Req, Env);
false -> {stop, cowboy_req:reply(503, Req)} false -> {stop, cowboy_req:reply(503, #{<<"retry-after">> => <<"15">>}, Req)}
end. end.
add_cors_flag(Req, Env) -> add_cors_flag(Req, Env) ->

View File

@ -33,18 +33,26 @@ init(Req0, State) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
running_status() -> running_status() ->
BrokerStatus = case emqx_dashboard_listener:is_ready(timer:seconds(20)) of
true ->
BrokerStatus = broker_status(),
AppStatus = application_status(),
Body = io_lib:format("Node ~ts is ~ts~nemqx is ~ts", [node(), BrokerStatus, AppStatus]),
{200, #{<<"content-type">> => <<"text/plain">>}, list_to_binary(Body)};
false ->
{503, #{<<"retry-after">> => <<"15">>}, <<>>}
end.
broker_status() ->
case emqx:is_running() of case emqx:is_running() of
true -> true ->
started; started;
false -> false ->
stopped stopped
end, end.
AppStatus =
application_status() ->
case lists:keysearch(emqx, 1, application:which_applications()) of case lists:keysearch(emqx, 1, application:which_applications()) of
false -> not_running; false -> not_running;
{value, _Val} -> running {value, _Val} -> running
end, end.
Status = io_lib:format("Node ~ts is ~ts~nemqx is ~ts", [node(), BrokerStatus, AppStatus]),
Body = list_to_binary(Status),
{200, #{<<"content-type">> => <<"text/plain">>}, Body}.