Long, synchronous running code in application initialization

Hi,

I need to run some code (including database drop and recreate) for a staging setup. This has to be done after the application supervisor is initialized, because some children are required to be alive before this long code is ran (K8S probes).

The rest of the children of the top supervisor must not be started before the initialization code has ran, because those children will use data setup by this code.

So I cannot use a Task here, as the execution is asynchronous. I abused a GenServer’s init/1 callback to run the code:

defmodule SyncTask do
  use GenServer

  def child_spec(opts) do
    case opts[:id] do
      nil -> super(opts)
      id -> Supervisor.child_spec(super(opts), id: {__MODULE__, id})
    end
  end

  def start_link(opts) do
    GenServer.start_link(__MODULE__, Map.new(opts))
  end

  def init(%{once: true, id: id, call: f}) when id not in [nil, :undefined] do
    pkey = {__MODULE__, id}

    case :persistent_term.get(pkey, nil) do
      nil ->
        f.()
        :persistent_term.put(pkey, :ran)
        :ignore

      :ran ->
        :ignore
    end
  end

  def init(%{once: true}) do
    raise ArgumentError, "the once: true option requires the :id option to be set"
  end

  def init(%{call: f}) do
    f.()
    :ignore
  end
end

And so I use it like this in when starting the application supervisor:

@impl true
def start(_type, _args) do
  children =
    :lists.flatten([
      k8s_stack(),
      {SyncTask, call: fn -> before_start() end, once: true, id: :before_start},
      db_stack(),
      app_stack(),
      endpoint_stack()
    ])

  opts = [strategy: :one_for_one, name: MyApp.Supervisor]
  Supervisor.start_link(children, opts)
end

It seems to work well, but I guess it is not really idiomatic. What would you do?

1 Like