From b687541d2791f21e41718c0a22752ad52f6edb54 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Mon, 11 May 2026 21:25:50 +0800 Subject: [PATCH] fix mneisa --- apps/iot/src/iot_app.erl | 10 +--- apps/iot/src/mnesia/iot_mnesia.erl | 92 +++++++++--------------------- 2 files changed, 30 insertions(+), 72 deletions(-) diff --git a/apps/iot/src/iot_app.erl b/apps/iot/src/iot_app.erl index b92214c..b361baa 100644 --- a/apps/iot/src/iot_app.erl +++ b/apps/iot/src/iot_app.erl @@ -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(), diff --git a/apps/iot/src/mnesia/iot_mnesia.erl b/apps/iot/src/mnesia/iot_mnesia.erl index 8668d43..2b4b151 100644 --- a/apps/iot/src/mnesia/iot_mnesia.erl +++ b/apps/iot/src/mnesia/iot_mnesia.erl @@ -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. \ No newline at end of file