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
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 have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
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
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
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
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
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
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
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











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: