Starting a child in a hot upgrade

Is it possible to start a child that was added to a list of supervised children after the initial release was built and the running application was started?

Say, the release was built with the following application.ex:

defmodule MyApp.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # MyApp.Worker
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)
  end
end

and then an upgrade was built with MyApp.Worker uncommented. In my case, after I make a hot upgrade, the uncommented MyApp.Worker is not started. What’s the easiest way to start it? Restart MyApp somehow?

Currently, I do it like this

iex(my_umbrella@127.0.0.1)3> Application.stop(:my_app)
:ok
iex(my_umbrella@127.0.0.1)4> Application.start(:my_app)
:ok
1 Like

You can specify to start the child (or restart the whole application) in your appup file.

https://hexdocs.pm/distillery/upgrades-and-downgrades.html#appups

http://erlang.org/doc/design_principles/appup_cookbook.html#sup_add

http://erlang.org/doc/design_principles/appup_cookbook.html#id87465

4 Likes