Add src/hk_health.erl
Browse files- src/hk_health.erl +72 -0
src/hk_health.erl
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
%%%-------------------------------------------------------------------
|
| 2 |
+
%%% @doc HK-CORE :: system health aggregation, backing `/health' and
|
| 3 |
+
%%% the dashboard view.
|
| 4 |
+
%%%
|
| 5 |
+
%%% Deliberately stateless per call: every field is computed fresh
|
| 6 |
+
%%% from the registries and supervisors at query time rather than
|
| 7 |
+
%%% tracked incrementally, so health can never drift from reality
|
| 8 |
+
%%% (there's no separate counter to fall out of sync).
|
| 9 |
+
%%% @end
|
| 10 |
+
%%%-------------------------------------------------------------------
|
| 11 |
+
-module(hk_health).
|
| 12 |
+
-behaviour(gen_server).
|
| 13 |
+
|
| 14 |
+
-export([start_link/0, report/0]).
|
| 15 |
+
-export([init/1, handle_call/3, handle_cast/2]).
|
| 16 |
+
|
| 17 |
+
start_link() ->
|
| 18 |
+
hk_sup_util:start_link_retry(
|
| 19 |
+
fun() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []) end).
|
| 20 |
+
|
| 21 |
+
-spec report() -> map().
|
| 22 |
+
report() ->
|
| 23 |
+
gen_server:call(?MODULE, report).
|
| 24 |
+
|
| 25 |
+
init([]) ->
|
| 26 |
+
{ok, #{started_at_ms => hk_id:timestamp_ms()}}.
|
| 27 |
+
|
| 28 |
+
handle_call(report, _From, State) ->
|
| 29 |
+
Now = hk_id:timestamp_ms(),
|
| 30 |
+
Report = #{
|
| 31 |
+
status => overall_status(),
|
| 32 |
+
uptime_ms => Now - maps:get(started_at_ms, State),
|
| 33 |
+
subsystems => #{
|
| 34 |
+
browser => subsystem_status(hk_browser_sup),
|
| 35 |
+
agent => subsystem_status(hk_agent_sup),
|
| 36 |
+
search => subsystem_status(hk_search_sup),
|
| 37 |
+
messaging => subsystem_status(hk_messaging_sup),
|
| 38 |
+
event => subsystem_status(hk_event_sup),
|
| 39 |
+
api => subsystem_status(hk_api_sup),
|
| 40 |
+
frontend => subsystem_status(hk_frontend_sup)
|
| 41 |
+
},
|
| 42 |
+
counts => #{
|
| 43 |
+
active_agents => safe_len(fun hk_agent_registry:list_agents/0),
|
| 44 |
+
active_browser_sessions => safe_len(fun hk_browser_registry:list_sessions/0),
|
| 45 |
+
active_search_jobs => safe_len(fun hk_search_registry:list_jobs/0),
|
| 46 |
+
recent_events => length(hk_event_bus:list_recent(1))
|
| 47 |
+
},
|
| 48 |
+
checked_at_ms => Now
|
| 49 |
+
},
|
| 50 |
+
{reply, Report, State}.
|
| 51 |
+
|
| 52 |
+
handle_cast(_Msg, State) ->
|
| 53 |
+
{noreply, State}.
|
| 54 |
+
|
| 55 |
+
%%% internal
|
| 56 |
+
|
| 57 |
+
overall_status() ->
|
| 58 |
+
Sups = [hk_browser_sup, hk_agent_sup, hk_search_sup, hk_messaging_sup,
|
| 59 |
+
hk_event_sup, hk_api_sup, hk_frontend_sup],
|
| 60 |
+
case lists:all(fun(S) -> whereis(S) =/= undefined end, Sups) of
|
| 61 |
+
true -> healthy;
|
| 62 |
+
false -> degraded
|
| 63 |
+
end.
|
| 64 |
+
|
| 65 |
+
subsystem_status(Name) ->
|
| 66 |
+
case whereis(Name) of
|
| 67 |
+
undefined -> down;
|
| 68 |
+
Pid when is_pid(Pid) -> up
|
| 69 |
+
end.
|
| 70 |
+
|
| 71 |
+
safe_len(Fun) ->
|
| 72 |
+
try length(Fun()) catch _:_ -> 0 end.
|