add mnesia

This commit is contained in:
anlicheng 2025-04-30 11:34:45 +08:00
parent ed7aff2759
commit fb4d7eeb98
4 changed files with 98 additions and 33 deletions

View File

@ -0,0 +1,20 @@
%%%-------------------------------------------------------------------
%%% @author anlicheng
%%% @copyright (C) 2025, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 30. 4 2025 11:16
%%%-------------------------------------------------------------------
-author("anlicheng").
%%
-record(micro_service, {
service_id :: binary(),
service_name :: binary(),
from :: binary(),
params :: binary(),
metrics :: binary(),
%% 0: , 1:
status = 0
}).

View File

@ -12,13 +12,9 @@
start(_StartType, _StartArgs) -> start(_StartType, _StartArgs) ->
io:setopts([{encoding, unicode}]), io:setopts([{encoding, unicode}]),
%% mnesia数据库 %% mnesia数据库
% mnesia:start(),
%% %%
erlang:system_flag(fullsweep_after, 16), erlang:system_flag(fullsweep_after, 16),
%% tcp的服务
% start_tcp_server(),
%% url %% url
application:set_env(efka, repository_url, "http://118.178.229.213:3000/anlicheng/ekfa/"), application:set_env(efka, repository_url, "http://118.178.229.213:3000/anlicheng/ekfa/"),
@ -29,21 +25,11 @@ stop(_State) ->
%% internal functions %% internal functions
%% tcp服务 %%
start_tcp_server() -> start_mnesia() ->
Port = 1883, %%
TransOpts = [ ok = mnesia:start(),
{tcp_options, [ Tables = mnesia:system_info(tables),
binary, %%
{reuseaddr, true}, not lists:member(build_data, Tables) andalso mnesia_build_data:create_table(),
{active, false}, not lists:member(counter, Tables) andalso mnesia_counter:create_table().
{packet, 2},
{nodelay, false},
{backlog, 1024}
]},
{acceptors, 10},
{max_connections, 1024}
],
{ok, _} = esockd:open('efka/tcp_server', Port, TransOpts, {tcp_channel, start_link, []}),
efka_loggeefka_logger:debug("[efka_app] the tcp server start at: ~p", [Port]).

View File

@ -8,12 +8,13 @@
%%%------------------------------------------------------------------- %%%-------------------------------------------------------------------
-module(efka_micro_service_sup). -module(efka_micro_service_sup).
-author("anlicheng"). -author("anlicheng").
-include("efka_tables.hrl").
-behaviour(supervisor). -behaviour(supervisor).
%% API %% API
-export([start_link/0]). -export([start_link/0]).
-export([register_server/1, delete_server/1]). -export([register_service/1, delete_service/1]).
%% Supervisor callbacks %% Supervisor callbacks
-export([init/1]). -export([init/1]).
@ -46,15 +47,18 @@ start_link() ->
init([]) -> init([]) ->
SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600}, SupFlags = #{strategy => one_for_one, intensity => 1000, period => 3600},
{ok, {SupFlags, []}}. MicroServices = micro_service_model:get_running_services(),
Specs = lists:map(fun(Service) -> child_spec(Service) end, MicroServices),
{ok, {SupFlags, Specs}}.
%%%=================================================================== %%%===================================================================
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================
-spec register_server(ServerId :: binary()) -> {ok, Pid :: pid()} | {error, Reason :: any()}. -spec register_service(ServiceId :: binary()) -> {ok, Pid :: pid()} | {error, Reason :: any()}.
register_server(ServerId) when is_binary(ServerId) -> register_service(ServiceId) when is_binary(ServiceId) ->
case supervisor:start_child(?MODULE, child_spec(ServerId)) of case supervisor:start_child(?MODULE, child_spec(ServiceId)) of
{ok, Pid} when is_pid(Pid) -> {ok, Pid} when is_pid(Pid) ->
{ok, Pid}; {ok, Pid};
{error, {'already_started', Pid}} when is_pid(Pid) -> {error, {'already_started', Pid}} when is_pid(Pid) ->
@ -63,17 +67,17 @@ register_server(ServerId) when is_binary(ServerId) ->
{error, Error} {error, Error}
end. end.
-spec delete_server(ServerId :: binary()) -> ok. -spec delete_service(ServiceId :: binary()) -> ok.
delete_server(ServerId) when is_binary(ServerId) -> delete_service(ServiceId) when is_binary(ServiceId) ->
ChildId = efka_server:get_name(ServerId), ChildId = efka_micro_service:get_name(ServiceId),
ok = supervisor:terminate_child(?MODULE, ChildId), ok = supervisor:terminate_child(?MODULE, ChildId),
supervisor:delete_child(?MODULE, ChildId). supervisor:delete_child(?MODULE, ChildId).
child_spec(ServerId) when is_binary(ServerId) -> child_spec(S = #micro_service{service_id = ServiceId}) when is_binary(ServiceId) ->
Name = efka_server:get_name(ServerId), Name = efka_micro_service:get_name(ServiceId),
#{ #{
id => Name, id => Name,
start => {efka_micro_service, start_link, [Name, ServerId]}, start => {efka_micro_service, start_link, [Name, S]},
restart => permanent, restart => permanent,
shutdown => 2000, shutdown => 2000,
type => worker, type => worker,

View File

@ -0,0 +1,55 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 04. 7 2023 12:31
%%%-------------------------------------------------------------------
-module(micro_service_model).
-author("aresei").
-include("efka_tables.hrl").
-include_lib("stdlib/include/qlc.hrl").
-define(TAB, micro_service).
%% API
-export([create_table/0]).
-export([insert/1, start_service/1, stop_service/1, get_running_services/0]).
create_table() ->
%% id生成器
mnesia:create_table(micro_service, [
{attributes, record_info(fields, micro_service)},
{record_name, micro_service},
{disc_copies, [node()]},
{type, ordered_set}
]).
insert(MicroService = #micro_service{}) ->
case mnesia:transaction(fun() -> mnesia:write(micro_service, MicroService, write) end) of
{'atomic', Res} ->
Res;
{'aborted', Reason} ->
{error, Reason}
end.
stop_service(ServiceId) when is_binary(ServiceId) ->
ok.
start_service(ServiceId) when is_binary(ServiceId) ->
ok.
-spec get_running_services() -> [#micro_service{}].
get_running_services() ->
Fun = fun() ->
Q = qlc:q([E || E <- mnesia:table(?TAB), E#micro_service.status =:= 1]),
qlc:e(Q)
end,
case mnesia:transaction(Fun) of
{'atomic', Res} ->
Res;
{'aborted', _} ->
[]
end.