Улучшения в кластере. Кластер формируется динамически, без единой точки отказа (каждая нода подключается ко всем трём через connect_nodes/0)

This commit is contained in:
2026-04-24 20:32:37 +03:00
parent 48d8ba7a2c
commit 6bb29174a5
4 changed files with 78 additions and 134 deletions

View File

@@ -1,49 +0,0 @@
-module(admin_app).
-behaviour(application).
-export([start/2, stop/1]).
start(_StartType, _StartArgs) ->
application:ensure_all_started(cowboy),
start_admin_http(),
{ok, self()}.
stop(_State) ->
ok.
start_admin_http() ->
Port = application:get_env(eventhub, admin_http_port, 8445),
Dispatch = cowboy_router:compile([
{'_', [
{"/admin/health", admin_handler_health, []},
{"/admin/stats", admin_handler_stats, []},
{"/admin/users", admin_handler_users, []},
{"/admin/users/:id", admin_handler_user_by_id, []},
{"/admin/calendars", admin_handler_calendars, []},
{"/admin/calendars/:id", admin_handler_calendar_by_id, []},
{"/admin/events", admin_handler_events, []},
{"/admin/events/:id", admin_handler_event_by_id, []},
{"/admin/reports", handler_reports, []},
{"/admin/reports/:id", handler_report_by_id, []},
{"/admin/tickets", handler_tickets, []},
{"/admin/tickets/:id", handler_ticket_by_id, []},
{"/admin/tickets/stats", handler_ticket_stats, []},
{"/admin/subscriptions", handler_admin_subscriptions, []},
{"/admin/banned-words", handler_banned_words, []}
]}
]),
Middlewares = [
cowboy_router,
cowboy_handler
],
Env = #{dispatch => Dispatch},
cowboy:start_clear(admin_http, [{port, Port}], #{
env => Env,
middlewares => Middlewares
}),
io:format("Admin HTTP server started on port ~p~n", [Port]).

View File

@@ -1,3 +1,3 @@
-sname ${NODE_NAME}
-name ${NODE_NAME}
-setcookie ${RELEASE_COOKIE}
-kernel inet_dist_use_interface {0,0,0,0}

View File

@@ -12,6 +12,7 @@ start(_StartType, _StartArgs) ->
{ok, Pid} ->
ok = infra_mnesia:init_tables(),
ok = infra_mnesia:wait_for_tables(),
connect_nodes(),
start_http(),
start_admin_http(),
% Запускаем сборщик метрик Prometheus
@@ -126,4 +127,18 @@ start_admin_http() ->
env => #{dispatch => AdminWsDispatch}
}),
io:format("WebSocket started on ports 8081 (user) and 8446 (admin)~n").
io:format("WebSocket started on ports 8081 (user) and 8446 (admin)~n").
connect_nodes() ->
case os:getenv("JOIN_NODES") of
false -> ok;
NodesStr ->
Nodes = [list_to_atom(string:trim(N)) || N <- string:tokens(NodesStr, ",")],
lists:foreach(fun(Node) ->
case net_kernel:connect_node(Node) of
true -> io:format("Connected to ~s~n", [Node]);
false -> io:format("ERROR: Failed to connect to ~s~n", [Node]);
ignored -> ok
end
end, Nodes)
end.