I have an Elixir/Mix app (also Phoenix, but plenty of non-Phoenix stuff in there too), and I'm wondering what the best practices are for putting in "startup" code, things like dynamically adding children to supervisors, firing off "I'm alive!" pings, or other things that you want to happen immediately after startup.
One obvious place is the Application file, but the expected return there is the return from Supervisor.start_link(children, opts). So, for example, in a Phoenix app, I could do this:
defmodule MyApp do
use Application
def start(_type, _args) do
import Supervisor.Spec
children = [
supervisor(MyApp.Repo, []),
supervisor(MyApp.Endpoint, []),
supervisor(MyApp.DynamicSupervisorThingy, [])
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
start_val = Supervisor.start_link(children, opts)
# --- Put things right here that I want to start ----
MyApp.DynamicSupervisorThingy.add_children_dynamically()
MyApp.SomeModule.do_some_thingy()
MyApp.OtherModule.send_some_pings()
if MIX_ENV == :prod do
MyApp.YetAnother.prod_stuff_i_dont_want_in_dev()
end
start_val
end
end
This seems wrong, but I can't figure out where I'm supposed to be putting this code.