|
|
@@ -0,0 +1,36 @@
|
|
|
+defmodule Vaccins.Poller do
|
|
|
+ use GenServer
|
|
|
+ alias Vaccins.{LocationStore, Search}
|
|
|
+ @name Vaccins.Poller
|
|
|
+ @refresh_period_ms 5 * 1000
|
|
|
+
|
|
|
+ def start_link(opts), do: GenServer.start_link(__MODULE__, opts, name: @name)
|
|
|
+
|
|
|
+ @impl true
|
|
|
+ def init(_opts) do
|
|
|
+ trigger_periodic_refresh()
|
|
|
+ {:ok, %{locations: LocationStore.get_locations()}}
|
|
|
+ end
|
|
|
+
|
|
|
+ @impl true
|
|
|
+ def handle_info(:periodic_refresh, state = %{locations: locations}) do
|
|
|
+ locations |> Enum.each(&Search.async_trigger_query/1)
|
|
|
+ trigger_periodic_refresh()
|
|
|
+ {:noreply, state}
|
|
|
+ end
|
|
|
+
|
|
|
+ @impl true
|
|
|
+ def handle_info({:query_result, id, res}, state = %{locations: locations}) do
|
|
|
+ {:noreply, state}
|
|
|
+ end
|
|
|
+
|
|
|
+ @impl true
|
|
|
+ def handle_info({:location_availabilities, id, res}, state = %{locations: locations}) do
|
|
|
+ Phoenix.PubSub.broadcast(Vaccins.PubSub, "locations", {:new_availabilities, id, res})
|
|
|
+ {:noreply, state}
|
|
|
+ end
|
|
|
+
|
|
|
+ defp trigger_periodic_refresh() do
|
|
|
+ Process.send_after(self(), :periodic_refresh, @refresh_period_ms)
|
|
|
+ end
|
|
|
+end
|