如何运行长生不老药应用程序?

运行长生不老药应用程序的正确方法是什么?

我正在创建一个简单的项目:

mix new app

然后我可以做:

mix run

基本上就是编译我的应用程序一次,所以当我添加:

IO.puts "running"

lib/app.ex中,我只是第一次看到 "running",除非有一些变化,否则每个连续的 run什么也不做。我接下来可以用生成的 app.app做什么?

我当然知道我能做到:

escript: [main_module: App]

mix.exs提供 def main(args):,然后:

mix escript.build
./app

但在我看来有点麻烦。

还有这样的东西:

elixir lib/app.exs

但是它显然没有计算 mix.exs,这是我的 app中的依赖所需要的。

41617 次浏览

mix run does run your app. It's just that when you simply put IO.puts "something" in a file that line is only evaluated in compile-time, it does nothing at runtime. If you want something to get started when you start your app you need to specify that in your mix.exs.

Usually you want a top-level Application that will get started. To achieve that add a mod option to your mix.exs:

def application do
[
# this is the name of any module implementing the Application behaviour
mod: {NewMix, []},
applications: [:logger]
]
end

And then in that module you need to implement a callback that will be called on application start:

defmodule NewMix do
use Application


def start(_type, _args) do
IO.puts "starting"
# some more stuff
end
end

The start callback should actually setup your top-level process or supervision tree root but in this case you will already see that it is called every time you use mix run, although followed by an error.

def start(_type, _args) do
IO.puts "starting"
Task.start(fn -> :timer.sleep(1000); IO.puts("done sleeping") end)
end

In this case we are starting a simple process in our callback that just sleeps for one second and then outputs something - this is enough to satisfy the API of the start callback but we don't see "done sleeping". The reason for this is that by default mix run will exit once that callback has finished executing. In order for that not to happen you need to use mix run --no-halt - in this case the VM will not be stopped.

Another useful way of starting your application is iex -S mix - this will behave in a similar way to mix run --no-halt but also open up an iex shell where you can interact with your code and your running application.

You can run tasks by importing Mix.Task into your module instead of mix run.

I think this is what you're looking for.

On top of that, instead of mix <task.run>, you can simply run mix to run the default task. Simply add default_task: "bot.run" into the list of def project do [..] end in mix.exs. Refer here.