|
|
Hi list,
I have an OTP application which uses mnesia. It checks/ensures the
schema and table files exist and then calls application:ensure_started(
mnesia ) in its application:start/2 callback. It works fine when I run
the application on its own under rebar3 shell, but it doesn't work when
I try to run it from an OTP release:
- When I run the OTP app under rebar3 shell in its own separate
directory, with { mnesia, [ { dir, "/tmp/my_db_dir" } ] } in
priv/sys.config, it works fine and application:get_env( mnesia, dir )
returns /tmp/my_db_dir as normal/expected
- If I set { mnesia, [ { dir, "/tmp/my_db_dir" } ] } in
<release>/config/sys.config, then:
- under rebar3 shell --name=myapp@mynode, calling
application:get_env( mnesia, dir ) succeeds (returns /tmp/my_db_dir) ,
and the app uses reads/writes mnesia tables successfully
- under_build/<profile>/rel/<release>/bin/<release> console it
fails (returns undefined), so the app crashes on start, regardless of
whether <profile> is prod or default
How do I set mnesia application environment variables in the release
project? (Or, maybe, how do I get the OTP application to be able to
access the release project's application environment variables?)
Also, I realise I could be going about this the wrong way - maybe mnesia
should be configured inside the specific OTP app, even under a release?
Apart from the fact that that doesn't seem to work (as I have it set up,
anyway), I'm aware I should steer clear of "included applications" - not
least because I want to be able to use the OTP app in other projects -
so I guess there's a broader question of how to configure mnesia in
general, in how & when to create schema and tables, if it could be used
by multiple OTP apps in a release?
Bit puzzled, appreciate any advice!
Cheers,
Igor
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
|
|
- under_build/<profile>/rel/<release>/bin/<release> console it fails (returns undefined), so the app crashes on start, regardless of whether <profile> is prod or default it may or may not be crashing due the mnesia configuration .you may have to check the the erl_crash.dump to see the cause of the crash .
How do I set mnesia application environment variables in the release project? (Or, maybe, how do I get the OTP application to be able to access the release project's application environment variables?) in the relx section of your rebar.config you can set a sys_config to be used {relx, [ {sys_config,"config/sys.config"} ] in that sys.config you can setup environmental variables to be used by your applications including an entry for mnesia. [ {mnesia, [{dir,"/tmp/my_db_dir"}] }, {app1,[{a,1},{b,2}], {app2,[{a,3},{b,4}]
} ] this will be used by your release system when the release is created .
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
|
|
Thanks Nuku,
- It's definitely crashing due to the mnesia configuration - it
shows a {badmatch,undefined} error on the line where I
call { ok, MnesiaDir } = application:get_env( mnesia, dir ),
and I've tried io:format()'ing the result of application:get_env(
mnesia, dir ), which shows ' undefined' when running
as a release
- relx section of rebar.config is set up exactly like that - I used
rebar3 to generate the release
- sys.config has exactly that mnesia entry
It all works as expected with 'rebar3 shell', environment variable
is found, mnesia starts, it all works fine - but when I run 'rebar3
release' and try to run the generated startup script, either with
'console' or with 'start' and looking in the log file, the OTP
application included in the release gets ' undefined' from application:get_env(
mnesia, dir ).
From your sys.config example I realise I could say for example [
{ my_app, [ { mnesia_dir, "/tmp/my_db_dir" } ] } ], and use
application:get_env( my_app, mnesia_dir ) to initialise; this works
and returns { ok, "/tmp/my_db_dir" }, which is great, but
then ok = application:ensure_started( mnesia ) fails with
{{badmatch, {error,{"no such file or directory","mnesia.app"}}}.
I have no idea where mnesia.app should go, and I haven't
been able to find any references to this file in docs or online that
explain where it should go or what should be in it, except that it
exists in the mnesia package under the OTP source tree.
So still pretty unclear how to proceed, pointers welcome!
Thanks,
Igor
On 15/04/2018 16:06, Nuku Ameyibor
wrote:
- under_build/<profile>/rel/<release>/bin/<release>
console it fails (returns undefined), so the app crashes
on start, regardless of whether <profile> is prod or
default
it
may or may not be crashing due the mnesia configuration .
you
may have to check the the erl_crash.dump to see the cause of
the crash .
How
do I set mnesia application environment variables in the
release project? (Or, maybe, how do I get the OTP
application to be able to access the release project's
application environment variables?)
in the relx section of your rebar.config you can set a
sys_config to be used
{relx,
[
{sys_config,"config/sys.config"}
]
in that sys.config you can setup environmental variables to
be used by your applications including an entry for mnesia.
[
{mnesia,
[{dir,"/tmp/my_db_dir"}]
},
{app1,[{a,1},{b,2}],
{app2,[{a,3},{b,4}]
}
]
this
will be used by your release system when the release is
created .
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
|
|
mnesia is not included as part of the release . when rebar3 shell command is run the path to mnesia is added automatically to the code paths . in a release environment however its not copied over into the release libs unless specified in one of your .app.src files. in your app.src file check to see if mnesia is one of the applications which has to be started before the main application is started.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
|
|
Thanks very much Nuku, that's the piece of the puzzle I was missing.
I hadn't understood properly about the release packaging - I thought
that if I added mnesia into the relx config, it would automatically
start the mnesia app too, and I wanted to do that manually in my
app. But after reading your email I added it into the relx/release
section, and as you said, the mnesia package is now included in the
release, but I can still start it manually, and the console boots
correctly and accesses the mnesia data.
Thank you!
Best,
Igor
On 15/04/2018 21:01, Nuku Ameyibor
wrote:
mnesia is not included as part of the release .
when
rebar3 shell command is run the path to mnesia is added
automatically to the code paths .
in a release environment however its not copied over into
the release libs unless specified in one of your
.app.src files.
in your app.src file check to see if mnesia is one of the
applications which has to be started before the main
application is started.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
|
|
you are welcome iggor but i think we may be talking about two different things the relx section like below {release, {"app_release", "0.3.3"},[app_name,mnesia]}, would be one way to include mnesia and to have libs added as part of the release and mnesia started when your application is started . another way which is what i was talking about would be to add it in the .app.src file like this {applications,[kernel,stdlib,mnesia ]} you may want mnesia to be started only when specific applications are started not when the whole release is started .
cheers!!
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
|
|
On Sun, Apr 15, 2018 at 1:48 PM Igor Clark < [hidden email]> wrote: How do I set mnesia application environment variables in the release
project? (Or, maybe, how do I get the OTP application to be able to
access the release project's application environment variables?)
[..]
Also, I realise I could be going about this the wrong way - maybe mnesia
should be configured inside the specific OTP app, even under a release?
[Quote] A common problem people run into with Mnesia is how to “get started”.
What people often resort to are solutions where an initial database is
created if it doesn’t exist. These solutions are often brittle.
Here, we pick another solution. A helper can create a database schema
for us, with all the necessary tables. The real release assumes the
presence of an initial database and won’t boot without one. This means
the Erlang release is simpler. There is always some database from
which it can boot and operate. That database might be the empty
database since we are just starting out. But in particular, the
release won’t concern itself with creating an initial database. Rather
it will assume one is already existing.
The situation is not much different than using a traditional
schema-oriented database. Usually, you have to create the database
first, and then populate the schema with some initial data. It is just
because of Rails/Django like systems in which databases are
migrate-established, we’ve started using different models. [EndQuote] Mnesia is part of your release and is started as part of your release, but its configuration is embedded within the release. This avoids the whole problem of creation-if-not-exist, which is probably not a good solution for a database schema, I think. It is convenient... until it accidentally the whole database you had.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
|
|
Thanks Jesper, that makes sense. So people tend to use a helper (I
guess an escript?) which sets up the database schema & tables
and then they start the app.
When you say "configuration is embedded in the release", do you mean
the mnesia conf goes in the config/sys.config at the top level of
the release? Does that mean that all the OTP apps included in the
release have to use the same mnesia conf?
Cheers,
Igor
On 16/04/2018 10:55, Jesper Louis
Andersen wrote:
On Sun, Apr 15, 2018 at 1:48 PM Igor Clark < [hidden email]>
wrote:
How do I
set mnesia application environment variables in the release
project? (Or, maybe, how do I get the OTP application to be
able to
access the release project's application environment
variables?)
[..]
Also, I realise I could be going about this the wrong way -
maybe mnesia
should be configured inside the specific OTP app, even under
a release?
[Quote]
A common problem people run into with Mnesia is how to
“get started”.
What people often resort to are solutions where an
initial database is
created if it doesn’t exist. These solutions are often
brittle.
Here, we pick another solution. A helper can create a
database schema
for us, with all the necessary tables. The real release
assumes the
presence of an initial database and won’t boot without
one. This means
the Erlang release is simpler. There is always some
database from
which it can boot and operate. That database might be
the empty
database since we are just starting out. But in
particular, the
release won’t concern itself with creating an initial
database. Rather
it will assume one is already existing.
The situation is not much different than using a
traditional
schema-oriented database. Usually, you have to create
the database
first, and then populate the schema with some initial
data. It is just
because of Rails/Django like systems in which databases
are
migrate-established, we’ve started using different
models.
[EndQuote]
Mnesia is part of your release and is started as part
of your release, but its configuration is embedded
within the release. This avoids the whole problem of
creation-if-not-exist, which is probably not a good
solution for a database schema, I think. It is
convenient... until it accidentally the whole database
you had.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
|
|
Got it. Thanks Nuku.
On 15/04/2018 22:39, Nuku Ameyibor
wrote:
you are welcome iggor but i think we may be talking
about two different things
the relx section like below
{release,
{"app_release", "0.3.3"},[app_name,mnesia]},
would be one way to include mnesia and to have libs added
as part of the release and mnesia started when your
application is started .
another way which is what i was talking about would be to
add it in the .app.src file like this
{applications,[kernel,stdlib,mnesia ]}
you may want mnesia to be started only when specific
applications are started not when the whole release is
started .
cheers!!
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
|
|