From 1cb281fbc4aa0832fb24051c23ecece949e0d51b Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Wed, 31 Aug 2022 15:42:39 +0800 Subject: [PATCH 1/2] fix: return 503 if dashboard's router not ready --- .../src/emqx_dashboard_middleware.erl | 2 +- .../src/emqx_mgmt_api_status.erl | 38 +++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/apps/emqx_dashboard/src/emqx_dashboard_middleware.erl b/apps/emqx_dashboard/src/emqx_dashboard_middleware.erl index 67f907bbb..4188950a6 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard_middleware.erl +++ b/apps/emqx_dashboard/src/emqx_dashboard_middleware.erl @@ -23,7 +23,7 @@ execute(Req, Env) -> case check_dispatch_ready(Env) of true -> add_cors_flag(Req, Env); - false -> {stop, cowboy_req:reply(503, Req)} + false -> {stop, cowboy_req:reply(503, #{<<"retry-after">> => <<"15">>}, Req)} end. add_cors_flag(Req, Env) -> diff --git a/apps/emqx_management/src/emqx_mgmt_api_status.erl b/apps/emqx_management/src/emqx_mgmt_api_status.erl index 70ff98988..e38ccfd69 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_status.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_status.erl @@ -33,18 +33,26 @@ init(Req0, State) -> %%-------------------------------------------------------------------- running_status() -> - BrokerStatus = - case emqx:is_running() of - true -> - started; - false -> - stopped - end, - AppStatus = - case lists:keysearch(emqx, 1, application:which_applications()) of - false -> not_running; - {value, _Val} -> running - 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}. + 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 + true -> + started; + false -> + stopped + end. + +application_status() -> + case lists:keysearch(emqx, 1, application:which_applications()) of + false -> not_running; + {value, _Val} -> running + end. From 13c0c7956781a96542b4c4100b8056564a8b9f2f Mon Sep 17 00:00:00 2001 From: Zhongwen Deng Date: Thu, 1 Sep 2022 09:39:15 +0800 Subject: [PATCH 2/2] chore: bump up emqx_dashboard --- apps/emqx_dashboard/src/emqx_dashboard.app.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/emqx_dashboard/src/emqx_dashboard.app.src b/apps/emqx_dashboard/src/emqx_dashboard.app.src index 4e1a3518f..adf974243 100644 --- a/apps/emqx_dashboard/src/emqx_dashboard.app.src +++ b/apps/emqx_dashboard/src/emqx_dashboard.app.src @@ -2,7 +2,7 @@ {application, emqx_dashboard, [ {description, "EMQX Web Dashboard"}, % strict semver, bump manually! - {vsn, "5.0.4"}, + {vsn, "5.0.5"}, {modules, []}, {registered, [emqx_dashboard_sup]}, {applications, [kernel, stdlib, mnesia, minirest, emqx]},