patmaddox

patmaddox

How to reload env vars when restarting a mix release app?

I have encountered something surprising when restarting a mix release app - it doesn’t pick up new env vars.

$ mix new release_test
$ cd release_test
$ mix release
$ FOO=foo _build/dev/rel/release_test/bin/release_test daemon
$ _build/dev/rel/release_test/bin/release_test rpc 'System.fetch_env("FOO") |> IO.inspect()'

This prints {:ok, "foo"} as expected.

Now I want to change the FOO environment variable and restart:

$ FOO=bar _build/dev/rel/release_test/bin/release_test restart
$ _build/dev/rel/release_test/bin/release_test rpc 'System.fetch_env("FOO") |> IO.inspect()'

But it still prints {:ok, "foo"} - it didn’t pick up the updated env var.

When I look at the OS processes, I see a run_erl and a beam.smp process that have the same PIDs as before the restart. So it seems like it may be more of a reload than actually stopping and starting the process, and maybe those long-running processes only read the env var when they first start?

Is there a way, using the mix release scripts, to restart the app and read new env vars as part of the restart?

Most Liked

patmaddox

patmaddox

Just to follow up: I did figure out how to lean heavily on FreeBSD’s rc system. The main thing is running the app under daemon(8) rather than using the elixir daemon. This produces a pid file, and so the service can use the built-in stop / restart / status behavior.

The following RC script is auto-generated by ExFreeBSD:

#!/bin/sh
#
# PROVIDE: release_test
# REQUIRE: DAEMON

. /etc/rc.subr

name=release_test
rcvar=${name}_enable
pidfile="/var/run/${name}.pid"
procname="/usr/local/libexec/release_test/erts-13.0.2/bin/beam.smp"

: ${release_test_env_file:=/usr/local/etc/release_test.env}

command="/usr/local/libexec/release_test/bin/release_test"
extra_commands="remote"
start_cmd="daemon -f -t $name -p $pidfile $command start"
remote_cmd=release_test_remote
#rpc_cmd=release_test_rpc

release_test_remote()
{
  export TERM=xterm
  $command remote
}

#release_test_rpc()
#{
#  $command rpc "$1"
#}

load_rc_config $name
run_rc_command "$@"
patmaddox

patmaddox

Okay. Do you have any suggestions on how to do a “real” restart, so I can reload the env vars?

I can run _build/dev/rel/release_test/bin/release_test stop but that can take a minute or two, and I’m not sure how to tell when it’s done in an automated way.

Typically daemons will write a pid file somewhere that you can use to reliably check the status. I don’t see anything like that.

I may be misunderstanding here - how do you do that? I can open up a new terminal tab, run the same second command, and get the same result. So, that’s not it.

Interesting… my takeaway from reading all this is that you should not configure Elixir apps using env vars, because once the erlang process is running, that’s it - there’s no way to get new env vars into it.

Instead, you use env vars to configure the erlang process. Then your application reads its own config from a file. Some of those tools you linked look like they’re intended to simulate env vars by reading the config from env-var-like files - but they’re not actually env vars.

I think for production releases, it’s probably important to cleanly separate these, and not load any app config from env vars since that only applies to app start. Vapor looks like it should do the trick there, but reading a config file should be simple enough. Thank you for the pointers!

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement