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 Responses

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

Fl4m3Ph03n1x
Background A few days ago I was listening to The future of Elixir from Elixir Talks, with Dave Thomas (@pragdave ) and Brian Mitchell. I...
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
WolfDan
After doing a port from a c++ library to my project in phoenix I’ve seen that I need a faster way to run this algorithm and I found this ...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
fireproofsocks
This is more of a general question, but I’m wondering how other people in the community think about the pattern matching in function sign...
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
mbenatti
Following https://github.com/tbrand/which_is_the_fastest |> https://raw.githubusercontent.com/tbrand/which_is_the_fastest/master/imgs...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New

Other popular topics Top

New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement