fireproofsocks
Sometimes I need to do some quick setup tasks before my app starts. There is a similar question here:
However, the problem is that using a Task for this does not block execution.
In other words, if I have something like this:
children = [
{Task,
fn ->
Foo.setup_stuff()
end},
{Foo.Worker, 100}
]
There appears to be no guarantee that the Task runs before the other children.
For a quick solution, I could simply rearrange my app’s start/2 so it does something like this:
Foo.setup_stuff()
children = [
{Foo.Worker, 100}
]
opts = [strategy: :one_for_one, name: Foo.Supervisor]
Supervisor.start_link(children, opts)
Is there a better or more idiomatic way to do this?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
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
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
andyleclair
Your solution of calling the function before passing the children to
Supervisor.start_linkis totally reasonable, and is what Sentry does to set up its’Loggerbackend. However, you’ll need to make sure that whatever lib you’re using to make the HTTP request is started before you make the call!Reqmight do this for you automatically (guessing yes) but I knowFinchwon’t, as it must be started by the client app.fireproofsocks
Thanks, good to get some confirmation on my approach. In my case, it should be fine – I’m doing some more complex RabbitMQ setup that gets a little hard to follow if it’s strung across so many Broadway pipelines and their
:after_connectarguments.However, that is an interesting point… sometimes you need certain things started in order (like the HTTP client example). Is there any way to ensure an ordered startup like that (with blocking)?
andyleclair
Yup! Check this out: Application — Elixir v1.20.2
fireproofsocks
Ah… hmm… I’ve only used that in
Mixtasks. WouldApplication.ensure_all_started/2work to ensure specific children had been started?E.g. if
Worker2requiredWorker2to be started, could you doand inside
Worker2:?
andyleclair
I’d say this is more of a smell than anything. Your applications shouldn’t be coupled in that way, and if they are, then
Worker2should be havingWorker1in its’application.exanthonator
There are two main approaches to this. One is to do whatever you need to do in the application
start/2callback which you’ve already discussed. This is good if you need to do something before any process has started.If you need some parts of the application to be started (e.g. Ecto) then you’ll need to add a process to your supervisor that performs the setup after the needed processes are started but before the processes that require the setup step.
I use the latter approach to ensure all Ecto connections have been established before moving on to starting something like Oban. What I’ve done is created a transient GenServer that performs the blocking work in the
init/1callback. This will halt the supervisor start process until the work ininit/1has finished. Once the work ininit/1has finished I return:ignoreto prevent the GenServer from starting and continue on with starting the remaining processes.Whether this is a code smell or not I’ll let you decide.
Here is some example code.
martosaur
I think this should work, as you can return
:ignoreright from thestartMFA:And then just add
OnStartupto the list of children in yourapplication.exdepending on when you want it to rundimitarvp
I think you should either just do something in an
initcallback of your ownGenServeror just use ahandle_continuein it – assuming that the latter will not leave your app in an inconsistent state until your own init code succeeds.axelson
You wouldn’t want to use
handle_continuein this case becausehandle_continuewill explicitly not block startup, although in my experience most of the time you actually don’t want to block startup. But this thread is specifically about the times that you do want to block startup.dimitarvp
Yeah I get this, and I am questioning the need to block startup. But it’s also true that it’s more complex to have both: (1) not block startup and (2) not have your app in degraded state, so maybe blocking startup really does save time and effort.