As part of the startup process in a phoenix application I want to run, or not run, some code based on an environment variable. Now I’m unsure where to make the check and run the code.
I’m looking for something like init/0 in the Endpoint module but not scoped to Phoenix specifically, more my own application. Maybe it should be in Application.start/2?
I’m looking for general guidance on this, but let me give you a concrete example that hopefully makes it a bit clearer what I’m after.
In my application I have some scheduled background jobs, using the Quantum library. These are configured directly in config.exs right now. But now I need to also be able to add specific jobs only if an environment variable is set. I’m using Distillery for deployments so reading that variable is not the problem. And using Quantum I can add jobs at run-time. I’m just not sure where a good place to do that is.
In this case I need Quantum to be up and running (and in other cases it could be that the Repo should be ready etc.). So are there “standard” places where people put this type of application specific startup code?
When you say environment variable do you mean Unix shell environment variables or the environment variables associated with an application, Application:get_env/3.
For the former typically read and configure using a Unix shell script (which might go into /etc/init.d/) setting the relevant erl command line options.
While the later in Application:start/2 or perhaps even bit lower down, Supervisor or GenServer.init
I mean Unix shell environment variables, but then I use the new providers in Distillery 2.0 to to read those into the Application env. So my question is more about, where should Elixir code that it is part of the startup process be? Mostly for code organization purposes but also so that it doesn’t run before my dependencies are up and running.
Application.start is probably the place you’re looking for
Yeah, that makes sense. The reason I was a little hesitant, is that both the Phoenix Endpoint and the Repo uses callbacks instead of just straight function calls in Application.start. And also the rest of Application.start is only about setting up the supervision tree so it felt like littering to add anything else there
Thank you for your help!