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
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #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: