I have an application (Elixir 1.5.2) up and running in our dev environment. There is a single worker defined in the Supervisor to manage database connections through poolboy.
The worker specification that I’d need to use for our production environment is different than our development environment, and isn’t solved by using environment variables.
Is there a way to list both worker configurations, and have them activate based upon MIX_ENV (via an if statement, etc.) ?
Or is there a more elegant way to do something like this?
if Mix.env == :prod do
# prod stuff
# not prod stuff
Thats what I had come up with… just thought there might be something more elegant like defining the worker configurations within dev.exs and have the worker source the configuration from there etc.
You really don’t want to do this for two reasons, one technical and the other semantic.
The technical reason is that
Mix is just a build tool and thus when you build a release, it isn’t included. Any
Mix.env calls will explode at runtime.
The semantic reason is that
if Mix.env == :prod doesn’t communicate anything. Instead do
if Application.get_env(:my_app, :run_poolboy) do. Then in your prod config you can set that config value true, and in some other environment you can set it false.
Thanks for the detailed answer.
fair points. you can also do the standard OS environment vars:
slack_key = System.get_env("SLACK_KEY")
if slack_key do
I am fairly new to Elixir.
You can, but then the only way to configure it is with your OS environment. Instead it’s better to always use the application environment in your code, and then if you want to drive your application environment off of OS vars you totally can.