Convert the module you’re showing the code for in the first post to a dynamic module-based supervisor like https://hexdocs.pm/elixir/DynamicSupervisor.html#module-module-based-supervisors
Let’s say that module is called MyDynSup
, and that you start it with the name: MyDynSup
option. Then, you can Enum.each([MyApp123.Module1, MyApp123.Module2, MyApp123.Module3], fn mod -> DynamicSupervisor.start_child(MyDynSup, mod) end)
.
Later, to start a new child, simply call DynamicSupervisor.start_child(MyDynSup, MyApp123.Module3)
(or DynamicSupervisor.start_child(MyDynSup, {MyApp123.Module3, []})
if you prefer).
Note that per the docs, the DynamicSupervisor starts without any children: they must be added after starting.