From 011cfdaaf09ea79b38f7a6bd657176afd98202c3 Mon Sep 17 00:00:00 2001 From: KaylaBrady <31781298+KaylaBrady@users.noreply.github.com> Date: Wed, 25 Sep 2024 10:00:42 -0400 Subject: [PATCH] feat(GlobalDataCache): Try populating cache out of band on startup --- lib/mobile_app_backend/global_data_cache.ex | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/mobile_app_backend/global_data_cache.ex b/lib/mobile_app_backend/global_data_cache.ex index c98864c..60a9fec 100644 --- a/lib/mobile_app_backend/global_data_cache.ex +++ b/lib/mobile_app_backend/global_data_cache.ex @@ -132,14 +132,21 @@ defmodule MobileAppBackend.GlobalDataCache.Impl do update_ms: opts[:update_ms] || :timer.minutes(5) } + Process.send_after(self(), :recalculate, :timer.seconds(1)) + {:ok, state} end @impl GenServer - def handle_info(:recalculate, %State{} = state) do + def handle_info(:recalculate, %State{update_ms: update_ms} = state) do update_data(state.key) - Process.send_after(self(), :recalculate, state.update_ms) + if :persistent_term.get(state.key, nil) do + Process.send_after(self(), :recalculate, update_ms) + else + # no data yet, try again sooner + Process.send_after(self(), :recalculate, :timer.seconds(5)) + end {:noreply, state} end