From 9c6763725547e4927d09cf3cd8d33949a28c4824 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 17 Jun 2024 10:08:54 -0400 Subject: [PATCH] Refactor the async user refreshing to use Oban Previous implementation could cause duplicate simultaneous profile fetches which is not polite. --- changelog.d/user-refresh-rework.skip | 0 config/test.exs | 2 -- lib/pleroma/user.ex | 10 ++++------ lib/pleroma/workers/user_refresh_worker.ex | 14 ++++++++++++++ test/pleroma/user_test.exs | 8 ++++++-- 5 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 changelog.d/user-refresh-rework.skip create mode 100644 lib/pleroma/workers/user_refresh_worker.ex diff --git a/changelog.d/user-refresh-rework.skip b/changelog.d/user-refresh-rework.skip new file mode 100644 index 000000000..e69de29bb diff --git a/config/test.exs b/config/test.exs index 8cd3b0216..0d4c82e0e 100644 --- a/config/test.exs +++ b/config/test.exs @@ -183,8 +183,6 @@ config :pleroma, Pleroma.Emoji.Loader, test_emoji: true config :pleroma, Pleroma.Web.RichMedia.Backfill, provider: Pleroma.Web.RichMedia.Backfill -config :pleroma, Pleroma.User, sync_refreshing: true - if File.exists?("./config/test.secret.exs") do import_config "test.secret.exs" else diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 440dc9210..7a8a68931 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -38,6 +38,7 @@ defmodule Pleroma.User do alias Pleroma.Web.OAuth alias Pleroma.Web.RelMe alias Pleroma.Workers.BackgroundWorker + alias Pleroma.Workers.UserRefreshWorker require Logger require Pleroma.Constants @@ -2165,12 +2166,9 @@ defmodule Pleroma.User do end defp maybe_refresh(user) do - fun = fn -> needs_update?(user) && fetch_by_ap_id(user.ap_id) end - - if Config.get([__MODULE__, :sync_refreshing], false) do - fun.() - else - Task.start(fun) + if needs_update?(user) do + UserRefreshWorker.new(%{"ap_id" => user.ap_id}) + |> Oban.insert() end end diff --git a/lib/pleroma/workers/user_refresh_worker.ex b/lib/pleroma/workers/user_refresh_worker.ex new file mode 100644 index 000000000..5842143f8 --- /dev/null +++ b/lib/pleroma/workers/user_refresh_worker.ex @@ -0,0 +1,14 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Workers.UserRefreshWorker do + use Pleroma.Workers.WorkerHelper, queue: "background", max_attempts: 1, unique: [period: 300] + + alias Pleroma.User + + @impl Oban.Worker + def perform(%Job{args: %{"ap_id" => ap_id}}) do + User.fetch_by_ap_id(ap_id) + end +end diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 0da9969d0..031621875 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -953,8 +953,12 @@ defmodule Pleroma.UserTest do {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin") - # User was updated async, fetch from cache now - updated_user = User.get_cached_by_ap_id(user.ap_id) + # Oban job was generated to refresh the stale user + assert_enqueued(worker: "Pleroma.Workers.UserRefreshWorker", args: %{"ap_id" => user.ap_id}) + + # Run job to refresh the user; just capture its output instead of fetching it again + assert {:ok, updated_user} = + perform_job(Pleroma.Workers.UserRefreshWorker, %{"ap_id" => user.ap_id}) assert updated_user.inbox