stuartc
I have a GenServer that manages a varying set of interval timers.
By default, when you start it - the list is empty; and you add timers in
via the API.
I’d like to be able to fetch a list of timers from the database,
and add them to the process on startup (Supervisor).
In addition, if the process fails and is restarted by the supervisor
add the up to date list of timers from the database.
I imagine I need a parent process that itself is a supervisor,
in order to separate the IntervalServer from my main application.
I can’t quite figure out where/how I would hook such behaviour in,
i.e. call out to another process for data before restarting/starting a process.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
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
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
stuartc
I came up with this:
And stuck it in my supervision tree with
worker(Bootstrap, [])I can verify the call to the Repo on start and restart, which means I’ll be able to
meet my requirements.
It feels rather thin, am I opening myself up to any issues with the supervisor or
is there a better/more elegant way to achieve this?
jwarlander
Strictly speaking, you can fetch your timer list in the
init/1function of your IntervalServer, eg:..and you’ll have the timers in your state. The drawback of this approach is that your database access is holding up the application startup sequence, and the application will utterly fail to start if the database isn’t available.
A different approach is to have
init/1return{:ok, [], 0}, which means it initializes with an empty array of timers, but a timeout of 0 milliseconds. When the timeout expires (immediately after startup),handle_info(:timeout, state)will be called, and you can load your state in that callback instead, without hindering the application during startup.. Eg:It depends on what your needs are. Doing it with timeouts, though, is the “friendliest” option – you can probably indicate in some other manner that the application isn’t ready yet, than having it fail to start at all. Perhaps some functionality is still useful, even without the database being (temporarily?) unavailable, etc.
NOTE: The IntervalServer can just be started in the top-level supervisor at this point, if you want to, eg.
lib/my_app.exin thestart/2function, as a worker.stuartc
Thanks! I’ve added the timeout, works nicely!
Just a thought on extending your approach, would
Process.send_after/3be a good idea?In lieu of relying on the
:timeoutmessage (despite my process not using other timeouts at the moment).Do you see any risks in this:
benwilson512
To be clear, both the
start_linkas well asinitwill block the start of the application. The supervisor will callstart_linkon the module which then synchronously waits forinitto completely. So whether you’re putting the code instart_linkorinitit’ll still block the supervisor until it’s done.jwarlander
Of course.. but since we’re just returning from
init, with a hint for GenServer to send us a timeout, the actual work (inhandle_info) will occur outside of the sequential startup sequence.jwarlander
Should be fine too
As far as I can tell though, still best practice to do it from within
init(which is executed in the context of the new GenServer process), eg: