fix mneisa
This commit is contained in:
parent
eaddd964ea
commit
b687541d27
@ -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(),
|
||||
|
||||
@ -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(),
|
||||
safe_start() ->
|
||||
case ensure_schema() of
|
||||
create_and_start ->
|
||||
ok = mnesia:start(),
|
||||
ok = create_tables(),
|
||||
ok = wait_for_tables(),
|
||||
ok
|
||||
end).
|
||||
|
||||
-spec start() -> ok | {error, term()}.
|
||||
start() ->
|
||||
with_result(fun() ->
|
||||
ok = wait_for_tables();
|
||||
only_start ->
|
||||
ok = mnesia:start(),
|
||||
ok = ensure_persistent_dir(),
|
||||
ok = wait_for_tables(),
|
||||
ok = verify_tables(),
|
||||
log_runtime_info(),
|
||||
ok
|
||||
end).
|
||||
ok = wait_for_tables()
|
||||
end,
|
||||
log_runtime_info().
|
||||
|
||||
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.
|
||||
@ -95,16 +72,3 @@ log_runtime_info() ->
|
||||
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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user