Recompile and restart applications from the outside?

Hi, I’m currently building a small REST api in Elixir and in order to learn the basics better I’m not using Phoenix, just Cowboy and Plug. Whenever I change a file I want to recompile and restart the (umbrella) application so that the changes are visible on the next request during development. At this time I’m restarting the whole docker container, but I’m wondering if there is a better way. Ideally I would like to have something like “mix app.restart” but that sort of command doesn’t seem to exist. How would you guys solve this problem?

When I run a phoenix app that I want to debug, I start it with

$ iex -S mix phx.server

Just adapt with your own command. Then the recompile command is available…

iex> recompile()

In Phoenix, there is a live websocket reload mecanism in dev mode, but I don’t know about your usage.

I should add that the application is currently started via “mix run --no-halt”, which is executed each time the container is started.

I’ve come up with what I think is the perfect solution now: If I name the elixir node when starting it via mix run, I can then remote shell into it with iex and recompile it, and then it works. That should be the fastest way for sure. Here’s the commands for anyone that comes across this problem later:

Start a named elixir node: elixir --sname <node name> -S mix run --no-halt
Remote shell with iex: iex --remsh <name@host> --sname <just make up a name>

2 Likes

Indeed you can remotely connect to the shell and once there you can use the recompile command to recompile your code. But how will you restart it after?

A way of doing it is to restart BEAM. You can do this by typing :init.restart. It returns :ok and proceeds to restart all applications smoothly:

http://erlang.org/doc/man/init.html

Notice however, that doing so will kick out of your IEX session after :stuck_out_tongue:

Hope it helps!

2 Likes

Thanks! As it turned out, a restart wasn’t actually needed in my case but still, it’s good to know.