How to run an Elixir application?

Elixir

Elixir Problem Overview


What is the correct way to run an Elixir application?

I'm creating a simple project by:

mix new app

and after that I can do:

mix run

which basically compiles my app once. So when I add:

IO.puts "running"

in lib/app.ex I see "running" only for the first time, each consecutive run does nothing unless there are some changes. What can I do next with generated app.app?

Of course I know I can do:

escript: [main_module: App]

in mix.exs, provide def main(args): and then:

mix escript.build
./app

but's it's kinda cumbersome in my opinion.

There's also something like:

elixir lib/app.exs

but it does not count mix.exs obviously, which is needed for dependencies in my app.

Elixir Solutions


Solution 1 - Elixir

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.

Solution 2 - Elixir

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKamil LelonekView Question on Stackoverflow
Solution 1 - ElixirPaweł ObrokView Answer on Stackoverflow
Solution 2 - ElixirholyxiaoxinView Answer on Stackoverflow