Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run an Elixir application?

Tags:

elixir

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.

like image 850
Kamil Lelonek Avatar asked Jun 06 '15 21:06

Kamil Lelonek


People also ask

How do I compile and run elixir code?

You can compile an elixir file using the elixirc command. This will create a . beam file in the current directory for each module defined in your file. If you start iex in this directory your modules from the file will be available.

What is an elixir application?

Application behaviour (Elixir v1. 12.3) A module for working with applications and defining application callbacks. Applications are the idiomatic way to package software in Erlang/OTP.


1 Answers

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.

like image 142
Paweł Obrok Avatar answered Sep 19 '22 18:09

Paweł Obrok