lud
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?
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
New
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project.
My initial shotgu...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Is there a word for the ~> symbol used in Version strings?
Do you also just call it a Squiggle Arrow™ ?!
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
I’d keep the approach that you’ve used since it actually seems you need a synchronous init.