From: Christopher Coté Date: Sat, 16 May 2026 13:44:11 +0000 (-0400) Subject: move last post to agent, update poll interval X-Git-Url: http://git.entropealabs.com/?a=commitdiff_plain;p=notifier.git move last post to agent, update poll interval --- diff --git a/lib/notifier/application.ex b/lib/notifier/application.ex index f59a548..7314521 100644 --- a/lib/notifier/application.ex +++ b/lib/notifier/application.ex @@ -38,6 +38,7 @@ defmodule Notifier.Application do server_name_indication: ~c"#{sni}", depth: 99 ]}, + FBGraph.LastPost, {FBGraph.Supervisor, ids: fb_ids, notifier: Proton, http: HTTPPool, from: username, notify: [notify]} ] diff --git a/lib/notifier/fb_graph/last_post.ex b/lib/notifier/fb_graph/last_post.ex new file mode 100644 index 0000000..c062c7c --- /dev/null +++ b/lib/notifier/fb_graph/last_post.ex @@ -0,0 +1,17 @@ +defmodule Notifier.FBGraph.LastPost do + use Agent + + def start_link(_) do + Agent.start_link(fn -> %{} end, name: __MODULE__) + end + + def set(id, post) do + Agent.update(__MODULE__, fn state -> + Map.put(state, id, post) + end) + end + + def get(id) do + Agent.get(__MODULE__, fn state -> Map.get(state, id, %{id: nil}) end) + end +end diff --git a/lib/notifier/fb_graph/worker.ex b/lib/notifier/fb_graph/worker.ex index 0cbad30..d1886dd 100644 --- a/lib/notifier/fb_graph/worker.ex +++ b/lib/notifier/fb_graph/worker.ex @@ -2,6 +2,7 @@ defmodule Notifier.FBGraph.Worker do use GenServer require Logger + alias Notifier.FBGraph.LastPost alias Notifier.SMTP @graph_url "https://www.facebook.com/api/graphql" @@ -27,34 +28,43 @@ defmodule Notifier.FBGraph.Worker do notifier: notifier, from: from, http: http, - notify: notify, - last_post: %{id: nil} + notify: notify }, {:continue, :get_latest_post}} end def handle_continue(:get_latest_post, %{http: http, id: id} = state) do - last_post = - :post - |> Finch.build(@graph_url, headers(), body(id)) - |> Finch.request(http) - |> handle_response() - |> handle_post() - |> maybe_notify(state) - - poll_interval = 60_000 * (15..25 |> Enum.random()) + :post + |> Finch.build(@graph_url, headers(), body(id)) + |> Finch.request(http) + |> handle_response() + |> handle_post() + |> maybe_notify(state) + |> set_last_post(state) + + poll_interval = 60_000 * (5..10 |> Enum.random()) Logger.info("Will poll #{state.name} again in #{poll_interval}ms") Process.send_after(self(), :get_posts, poll_interval) - {:noreply, %{state | last_post: last_post}} + {:noreply, state} end def handle_info(:get_posts, state), do: {:noreply, state, {:continue, :get_latest_post}} - defp maybe_notify({:error, _}, %{last_post: last_post}), do: last_post + defp set_last_post(nil, _), do: :noop - defp maybe_notify(%{id: id}, %{last_post: %{id: id} = last_post}), - do: last_post + defp set_last_post(post, %{id: id}) do + LastPost.set(id, post) + end + + defp maybe_notify({:error, _}, _), do: nil + + defp maybe_notify(%{id: id} = post, %{id: fb_id} = state) do + case LastPost.get(fb_id) do + %{id: ^id} -> post + _old_post -> notify(post, state) + end + end - defp maybe_notify( + defp notify( %{message: message, images: images} = post, %{notifier: notifier, from: from, notify: notify, name: name} ) do @@ -107,6 +117,16 @@ defmodule Notifier.FBGraph.Worker do error end + defp handle_post([res | _]) do + Logger.error("FB Graph returned error: #{inspect(res)}") + {:error, :no_data} + end + + defp handle_post(res) do + Logger.error("Unhandled result: #{inspect(res)}") + {:error, :no_data} + end + defp handle_response({:ok, %Finch.Response{status: 200, body: body}}) do body |> String.split("\r\n")