简体   繁体   English

如何运行erlang(钢筋构建)应用程序

[英]How to run erlang (rebar build) application

I am new to Erlang world and currently can't figure out how to start my dummy erlang application.我是 Erlang 世界的新手,目前不知道如何启动我的虚拟 erlang 应用程序。 Probably, I am just missing something... So, I created an application with rebar (rebar create-app appid=dummys).可能,我只是错过了一些东西......所以,我用钢筋创建了一个应用程序(rebar create-app appid=dummys)。

Currently I have目前我有

  • rebar.config钢筋配置
  • src/dummys.app.src src/dummys.app.src
  • src/dummys_app.erl src/dummys_app.erl
  • src/dummys_sup.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).我发现为了在开发过程中运行应用程序,最好创建一个额外的启动方法,该方法应该调用 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.. erl 因错误而崩溃..

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.例如,我有一个名为“*dummys_cool*”的附加模块,它有一个“ start ”方法。 How to tell my application to run that "dummys_cool#start" method?如何告诉我的应用程序运行“dummys_cool#start”方法?

Thank you!谢谢!

For quick development, if you just want to ensure your appliction can start, start a shell, then start the application:为了快速开发,如果您只是想确保您的应用程序可以启动,请启动一个 shell,然后启动应用程序:

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: Rebar 可以帮助您完成大部分工作:

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.这将构建一个可移植的版本,其中包括所有必需的库,甚至 erlang 运行时系统。 This is put by default in the rel/ directory;这默认放在 rel/ 目录中; in your case rel/dummys.在你的情况下 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

Have a look at your dummys.app.src file.看看你的dummys.app.src文件。 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.所有指令的含义在“app”手册页中都有解释,但我怀疑这里缺少的是mod ,它表示您的应用程序回调模块的名称。 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 .那里的空列表将作为StartArgs参数传递给dummys_app:start/2


To make a new module start along with your application, add it to the supervision tree in dummys_sup:init . 要使新模块与您的应用程序一起启动,请将其添加到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.这在'supervisor' manpage 中有描述,但基本上这意味着在启动时,这个supervisor 将启动一个子进程。 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. dummys_cool:start_link()将被调用,该函数预计会产生一个新进程,链接到它,并返回其进程 ID。 If you need more processes, just add more child specifications to the list.如果您需要更多进程,只需将更多子规范添加到列表中。

erl -noshell -pa ebin -s application start dummys

The code above will not work because application:start([dummys]) will be called.上面的代码将不起作用,因为application:start([dummys])将被调用。

You can take a reference of the Erlang documentation for details.您可以参考Erlang 文档了解详细信息。

For your case,对于你的情况,

erl -noshell -pa ebin -s dummys

I ran into this problem, and this was the first answer on Google.我遇到了这个问题,这是谷歌上的第一个答案。

If you are using rebar3, the standard configuration will have a shell command that compiles your project and opens a shell:如果您使用的是 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM