如何运行erlang(rebar build)应用程序

aurhwmvo  于 2022-12-08  发布在  Erlang
关注(0)|答案(4)|浏览(171)

I am new to Erlang world and currently can't figure out how to start my dummy erlang application. Probably, I am just missing something... So, I created an application with rebar (rebar create-app appid=dummys).
Currently I have

  • rebar.config
  • src/dummys.app.src
  • src/dummys_app.erl
  • src/dummys_sup.erl

I have found that in order to run an application during a development it is better to create an additional start method which should call application:start(module).
I added some basic logging to my start methods..

start() ->
    error_logger:info_msg("Starting app(dev)..~n"),
    application:start(dummys_app).

start(_StartType, _StartArgs) ->
    error_logger:info_msg("Starting app..~n"),
    dummys_sup:start_link().

If I try

erl -noshell -pa ebin -s application start dummys
erl -noshell -pa ebin -s application start dummys_app

there are no output..
If I try

erl -noshell -pa ebin -s dummys start

erl crashes with an error..
If I try

erl -noshell -pa ebin -s dummys_app start

it outputs just "Starting app(dev).." and that's all. But I also expect to see "Starting app.."
What I am missing or doing wrong??

And another question: How to add a new module to my dummy application correctly? For example I have an additional module called "dummys_cool" which has a "start" method. How to tell my application to run that "dummys_cool#start" method?

Thank you!

lyfkaqu1

lyfkaqu11#

For quick development, if you just want to ensure your appliction can start, start a shell, then start the application:

erl -pa ebin
1> dummys_app:start().

That will give you a clean indication of what is wrong and right without the shell bombing out after.
Since you're making an application to run, rather than just a library to share, you'll want to make a release. Rebar can get you most of the way there:

mkdir rel
cd rel
rebar create-node nodeid=dummysnode

After you've compiled your application, you can create a release:

rebar generate

This will build a portable release which includes all the required libraries and even the erlang runtime system. This is put by default in the rel/ directory; in your case rel/dummys.
Within that directory there will be a control script that you can use to start, stop, and attach to the application:

rel/dummys/bin/dummys start
rel/dummys/bin/dummys stop
rel/dummys/bin/dummys start
rel/dummys/bin/dummys attach
a7qyws3x

a7qyws3x2#

Have a look at your dummys.app.src file. The meaning of all the directives is explained in the 'app' manpage , but the one I suspect is missing here is mod , which indicates the name of your application callback module. So make sure that this line is present:

{mod, {dummys_app, []}}

The empty list in there will be passed as the StartArgs argument to dummys_app:start/2 .
To make a new module start along with your application, add it to the supervision tree in dummys_sup:init . This function should look something like:

init(_) ->
    {ok, {{one_for_one, 10, 10},
         [{dummys_cool, {dummys_cool, start_link, []},
           permanent, brutal_kill, worker, [dummys_cool]}]}.

This is described in the 'supervisor' manpage , but basically this means that on startup, this supervisor will start one child process. dummys_cool:start_link() will be called, and that function is expected to spawn a new process, link to it, and return its process id. If you need more processes, just add more child specifications to the list.

hfsqlsce

hfsqlsce3#

erl -noshell -pa ebin -s application start dummys

上面的代码将无法工作,因为将调用application:start([dummys])
详情请参考Erlang documentation
对于你的案子,

erl -noshell -pa ebin -s dummys
jyztefdp

jyztefdp4#

我遇到了这个问题,这是谷歌上的第一个答案。
如果使用rebar3,标准配置将包含一个shell命令,用于编译项目并打开shell:

$ rebar3 shell
===> Analyzing applications...
===> Compiling myapp
Erlang/OTP 21 [erts-10.2.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]

Eshell V10.2.4  (abort with ^G)
1> ===> Booted myapp

相关问题