Im trying to allow users set config at runtime but from what I’ve read so far it seems it cannot be done. Is that the case?
Check out Application.put_env/4
and see if that’s what you need.
thank you, I think this is what I need so if I use this function in release task then the values from this function should supercede those in runtime.exs ?
What Im trying to achieve is to load config values from a configs table in the DB.
The DB is not started at the time that runtime.exs is evaluated…
I tried something like this:
alias Dynamic.Repo
alias Dynamic.Structures.Config
Get config from DB and set it to the application
config = Config
|> Repo.all()
|> Enum.reduce(%{}, fn config, acc → Map.put(acc, config.key, config.value) end)
Then I tried setting the value with a value in config and fallback to ENV var
like so:
config :dynamic, Dynamic.PromEx,
grafana: [
host: config[“GRAFANA_HOST”] || System.fetch_env!(“GRAFANA_HOST”),
username: config[“GRAFANA_USER”] || System.fetch_env!(“GRAFANA_USER”),
password: config[“GRAFANA_PASSWORD”] || System.fetch_env!(“GRAFANA_PASSWORD”),
upload_dashboards_on_start: config[“UPLOAD_DASHBOARDS_ON_START”] || false
],
metrics_server: :disabled
You can do some or all of it in runtime.exs
using environment variables. In your application’s start function, you can load config from files, the environment, etc. and use Application.put_env
as @dimitarvp suggests.
defmodule My.Application do
def start(_type, _args) do
# set the config
grafana_config = ....
Application.put_env(:some_app, :key, grafana_config)
# merge the config
extra_some_config = ...
existing_some_config = Application.get_env(:some_app, :some_config)
Application.put_env(:some_app, :some_config, Keyword.merge(existing_some_config, extra_some_config)
children = [...]
# start the children
end
end
To get it out of the repo, you’d have to start the Repo process first. You could try inserting a process in the children that starts after the repo but before promex and do the config in there.
Also check out Config Providers.
Thank you both, this is just what I needed.