fix mneisa

This commit is contained in:
anlicheng 2026-05-11 21:25:50 +08:00
parent eaddd964ea
commit b687541d27
2 changed files with 30 additions and 72 deletions

View File

@ -13,14 +13,8 @@ start(_StartType, _StartArgs) ->
io:setopts([{encoding, unicode}]),
%%
erlang:system_flag(fullsweep_after, 16),
try
%% mnesia
ok = iot_mnesia:start()
catch
Class:Reason:Stack ->
logger:warning("[iot_app] start failed, class: ~p, reason: ~p, stack: ~p", [Class, Reason, Stack]),
ok = iot_mnesia:init()
end,
iot_mnesia:safe_start(),
%% http服务 supervisor simulator API
start_http_server(),

View File

@ -9,56 +9,48 @@
-include("iot_tables.hrl").
-export([init/0, start/0, stop/0]).
-export([safe_start/0, stop/0]).
-define(TABLES, [efka_client]).
-define(WAIT_TIMEOUT, 5000).
-spec init() -> ok | {error, term()}.
init() ->
with_result(fun() ->
_ = mnesia:stop(),
ok = mnesia:delete_schema([node()]),
ok = mnesia:create_schema([node()]),
ok = ensure_persistent_dir(),
ok = mnesia:start(),
ok = create_tables(),
ok = wait_for_tables(),
ok
end).
safe_start() ->
case ensure_schema() of
create_and_start ->
ok = mnesia:start(),
ok = create_tables(),
ok = wait_for_tables();
only_start ->
ok = mnesia:start(),
ok = wait_for_tables()
end,
log_runtime_info().
-spec start() -> ok | {error, term()}.
start() ->
with_result(fun() ->
ok = mnesia:start(),
ok = ensure_persistent_dir(),
ok = wait_for_tables(),
ok = verify_tables(),
log_runtime_info(),
ok
end).
ensure_schema() ->
stopped = mnesia:stop(),
case mnesia:create_schema([node()]) of
ok ->
create_and_start;
{error, {_, {already_exists, _}}} ->
only_start;
{error, {_, already_exists}} ->
only_start;
{error, _Reason} ->
only_start
end.
-spec stop() -> ok.
stop() ->
_ = mnesia:stop(),
ok.
-spec ensure_persistent_dir() -> ok.
ensure_persistent_dir() ->
case mnesia:system_info(use_dir) of
true ->
ok;
false ->
throw({mnesia_dir_required, node()})
end.
-spec create_tables() -> ok.
create_tables() ->
case efka_client_store:ensure_table() of
ok ->
ok;
{error, Reason} ->
throw({create_table_failed, {efka_client, Reason}})
{error, {create_table_failed, {efka_client, Reason}}}
end.
-spec wait_for_tables() -> ok.
@ -67,24 +59,9 @@ wait_for_tables() ->
ok ->
ok;
{timeout, Tables} ->
throw({mnesia_tables_timeout, Tables});
{error, {mnesia_tables_timeout, Tables}};
{error, Reason} ->
throw({mnesia_tables_error, Reason})
end.
-spec verify_tables() -> ok.
verify_tables() ->
lists:foreach(fun verify_table/1, ?TABLES),
ok.
-spec verify_table(atom()) -> ok.
verify_table(efka_client) ->
Expected = record_info(fields, efka_client),
case mnesia:table_info(efka_client, attributes) of
Expected ->
ok;
Actual ->
throw({table_attributes_mismatch, efka_client, Expected, Actual})
{error, {mnesia_tables_error, Reason}}
end.
-spec log_runtime_info() -> ok.
@ -94,17 +71,4 @@ log_runtime_info() ->
mnesia:system_info(directory),
mnesia:system_info(tables)
]),
ok.
-spec with_result(fun(() -> ok)) -> ok | {error, term()}.
with_result(Fun) ->
try Fun() of
ok ->
ok
catch
throw:Reason ->
{error, Reason};
Class:Reason:Stacktrace ->
logger:warning("[iot_mnesia] failed, class: ~p, reason: ~p, stack: ~p", [Class, Reason, Stacktrace]),
{error, Reason}
end.
ok.