fix mnesia

This commit is contained in:
anlicheng 2026-05-10 22:37:25 +08:00
parent 634a3ece75
commit e4934eb43b
2 changed files with 16 additions and 46 deletions

View File

@ -15,7 +15,11 @@ start(_StartType, _StartArgs) ->
erlang:system_flag(fullsweep_after, 16), erlang:system_flag(fullsweep_after, 16),
try try
%% mnesia %% mnesia
ok = iot_mnesia:start(), ok = iot_mnesia:start()
catch
Class:Reason:Stack ->
logger:warning("[iot_app] start failed, class: ~p, reason: ~p, stack: ~p", [Class, Reason, Stack])
end,
%% http服务 supervisor simulator API %% http服务 supervisor simulator API
start_http_server(), start_http_server(),
@ -23,19 +27,7 @@ start(_StartType, _StartArgs) ->
%% ssl服务 %% ssl服务
start_ssl_server(), start_ssl_server(),
case iot_sup:start_link() of iot_sup:start_link().
{ok, _Pid} = Ok ->
Ok;
Error ->
stop_started_services(),
Error
end
catch
Class:Reason:Stack ->
stop_started_services(),
logger:warning("[iot_app] start failed, class: ~p, reason: ~p, stack: ~p", [Class, Reason, Stack]),
{error, Reason}
end.
stop(_State) -> stop(_State) ->
stop_started_services(), stop_started_services(),

View File

@ -17,21 +17,21 @@
-spec init() -> ok | {error, term()}. -spec init() -> ok | {error, term()}.
init() -> init() ->
with_result(fun() -> with_result(fun() ->
ok = ensure_persistent_dir(),
_ = mnesia:stop(), _ = mnesia:stop(),
ok = create_schema_if_missing(), ok = mnesia:delete_schema([node()]),
ok = start_mnesia(), ok = mnesia:create_schema([node()]),
ok = ensure_persistent_dir(),
ok = mnesia:start(),
ok = create_tables(), ok = create_tables(),
ok = wait_for_tables(), ok = wait_for_tables(),
ok = stop(),
ok ok
end). end).
-spec start() -> ok | {error, term()}. -spec start() -> ok | {error, term()}.
start() -> start() ->
with_result(fun() -> with_result(fun() ->
ok = mnesia:start(),
ok = ensure_persistent_dir(), ok = ensure_persistent_dir(),
ok = start_mnesia(),
ok = wait_for_tables(), ok = wait_for_tables(),
ok = verify_tables(), ok = verify_tables(),
log_runtime_info(), log_runtime_info(),
@ -52,28 +52,6 @@ ensure_persistent_dir() ->
throw({mnesia_dir_required, node()}) throw({mnesia_dir_required, node()})
end. end.
-spec create_schema_if_missing() -> ok.
create_schema_if_missing() ->
case mnesia:create_schema([node()]) of
ok ->
ok;
{error, {_, {already_exists, _}}} ->
ok;
{error, Reason} ->
throw({create_schema_failed, Reason})
end.
-spec start_mnesia() -> ok.
start_mnesia() ->
case mnesia:start() of
ok ->
ok;
{error, {already_started, mnesia}} ->
ok;
{error, Reason} ->
throw({start_mnesia_failed, Reason})
end.
-spec create_tables() -> ok. -spec create_tables() -> ok.
create_tables() -> create_tables() ->
case efka_client_store:ensure_table() of case efka_client_store:ensure_table() of