In a recent Elixir project, I wanted to take a value from my application’s runtime config and insert it into the database. Because this value is provided via environment variables, it can only change at the time of application startup. This means it’s reasonable to set this value in the database once immediately after the application starts up.

I found the best way to accomplish this is to use a Task started under a supervision tree to perform the insert:

def init([]) do
  children = [
    {Task, &MyApp.insert_runtime_config/0}

  Supervisor.init(children, strategy: :one_for_one)

Because the Task specifies a :temporary restart behavior in its child_spec, the supervisor runs the task to completion and never tries to restart it. “Any termination (even abnormal) is considered successful”.