Long running process with periodic tasks

  • Have a GenServer that is responsible for calculating and returning the aggregated data.
  • Have that GenServer maintain this state:
    • Date/time showing when it last did a calculation.
    • Last cached calculation.
  • When the GenServer receives a message to supply the aggregated data, have it check against the date/time; if more than 60_000 ms have passed, re-calculate it and return it. If the data has been calculated less time before that, just return a cached response.

I personally wouldn’t reach for a fixed time period recalculation. Imagine if nobody pings that state for a week. Why recalculate it every minute?

It depends on the project and how frequently will this data be requested but in any case, food for thought. I have changed several hobby projects to the pattern above and my OCD about not wasting CPU resources has calmed down. :101: