lud

lud

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?

Most Liked

mpope

mpope

Unless I am misunderstanding your usecase, his can be done async. You’re task or genserver has the ability to run the databse init, then after it is finished it can add the dependent processes to the supervisor using Supervisor.start_child/2. This will avoid the use of persistent term or ets for coordination. The async Task or GenServer can be added to the supervision tree as well, to ensure that it runs successfully and retry on error. This could be a good use of a DynamicSupervisor, but if the processes are static (only kicked off once at startup), maybe a regular Supervisor will do the trick.

stefanchrobot

stefanchrobot

I’d keep the approach that you’ve used since it actually seems you need a synchronous init.

Where Next?

Popular in Discussions Top

New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement