Assuming the code in confix.exs
is correct
config :backend,
users: ~w[user1 user2 user3]
to start supervised children dynamically you might use e. g. DynamicSupervisor
.
In your static initialization code you start DynamicSupervisor
without any children:
children = [
...,
{DynamicSupervisor, strategy: :one_for_one, name: MyApp.DS}
]
Supervisor.start_link(children, strategy: :one_for_one)
and when you want to start children dynamically, you basically do:
users = Application.get_env(:backend, :users, [])
agents =
Enum.map(users, fn user ->
with {:ok, agent} <- DynamicSupervisor.start_child(MyApp.DS, {Agent, fn -> %{} end}) do
Agent.update(agent, &Map.put(&1, :user, user))
end
end)