gpt4 book ai didi

erlang - 你如何在生产环境中初始化 mnesia?

转载 作者:行者123 更新时间:2023-12-01 10:58:55 24 4
gpt4 key购买 nike

我正在使用 rebar3 生成一个版本,但我如何在生产中初始化 mnesia?

如果我编写执行 mnesia:create_schema([node()]) 的“安装”脚本 - 它将使用与发布版本完全不同的节点名称。

因此,当我使用 my-app-1.0.0 start 启动我的应用程序时,我最终为“nonode@nonode”创建了一个模式,而生产 mnesia 试图访问“myapp@localhost”节点。

另外,这是一个先有鸡还是先有蛋的问题:

  1. 没有 mnesia 表我无法启动我的应用
  2. 我无法在我的应用程序未运行的情况下安装我的 mnesia 表(与应用程序将使用的相同 node() 名称)。

只是徘徊是否有一个好的方法来处理这个问题?

这是我独立运行的安装脚本:

#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable ls-mnesia debug verbose
-include("../include/rr.hrl").

main(_) ->
application:set_env(mnesia, dir, "/usr/local/src/db/mnesia"),
application:stop(mnesia),
install([node()|nodes()]).

install(Nodes) ->
case mnesia:create_schema(Nodes) of
ok ->
rpc:multicall(Nodes, application, start, [mnesia]),
read_store_create_tables(Nodes),
event_store_create_tables(Nodes),
rpc:multicall(Nodes, application, stop, [mnesia]);
Err ->
error_logger:warning_msg("Could not create schema: ~p~n", [Err]),
Err
end.

event_store_create_tables(Nodes) ->
{_, ok} = mnesia:create_table(rr_events,
[{attributes, record_info(fields, rr_events)},
{disc_copies, Nodes},
{type, bag}]).

read_store_create_tables(Nodes) ->
% Initialize the actual data-tables for the projections
{_, ok} = mnesia:create_table(rr_competencies,
[{attributes, record_info(fields, rr_competencies)},
{disc_copies, Nodes}]).

P.S.:我正在使用 rebar3,它使用 relx 来构建版本。

最佳答案

我正在使用自己的 build system编写它主要是因为这个确切的要求——能够在启动节点之前安装和初始化节点。这个想法很简单:有 two releases ,在此特定示例中称为 cmdhumbundeecmd 版本不会启动主要应用程序,只会加载它们。然后执行一个特殊的函数来初始化节点。该功能在 reltool.config 中配置文件。在本例中为 hbd_setupdeploy 应用程序。该函数读取配置文件并从备份创建和初始化 mnesia 数据库,或者如果备份不存在则创建一个新数据库。一旦安装了节点,它就会使用正确的版本启动。在开发(直接从源代码)和生产(从 OTP 发布)中执行相同的步骤。

通过该设置,您描述的问题不存在,因为两个版本都是从同一位置启动的,使用几乎相同的命令和配置文件(builderl 中的配置生成它们reltool.config).


您可以将相同的想法用于任何构建工具,包括 rebar3relx,方法是手动或使用某种脚本执行这些步骤。

builderl 所做的是它自动执行这些步骤,并提供一个环境以在开发和生产中以相同的方式执行它们,例如查看bottom of the humbundee project's README文件:

安装节点。这将启动cmd 版本并执行hbd_setup:install/2 函数来初始化节点:

./bin/init.esh

启动节点。这将启动 humbundee 版本,它会启动所有应用程序及其相应的主管树:

./bin/start.esh

builderl 实际使用rebar 拉取编译dependencies that depend on other projects ,但是它仅使用 OTP 来创建版本。它还可以使用 project-wide dependency file 下载依赖项本身, 然后是 compiled with make (没有 make 的编译是 work in progress )。希望对您有所帮助。

关于erlang - 你如何在生产环境中初始化 mnesia?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36803280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com