How to run an Elixir application?

Kamil Lelonek picture Kamil Lelonek · Jun 6, 2015 · Viewed 34k times · Source

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.

Answer

Paweł Obrok picture Paweł Obrok · Jun 7, 2015

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.