Refactor the async user refreshing to use Oban

Previous implementation could cause duplicate simultaneous profile fetches which is not polite.
This commit is contained in:
Mark Felder 2024-06-17 10:08:54 -04:00
parent 825541b27c
commit 9c67637255
5 changed files with 24 additions and 10 deletions

View file

View file

@ -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.Web.RichMedia.Backfill, provider: Pleroma.Web.RichMedia.Backfill
config :pleroma, Pleroma.User, sync_refreshing: true
if File.exists?("./config/test.secret.exs") do if File.exists?("./config/test.secret.exs") do
import_config "test.secret.exs" import_config "test.secret.exs"
else else

View file

@ -38,6 +38,7 @@ defmodule Pleroma.User do
alias Pleroma.Web.OAuth alias Pleroma.Web.OAuth
alias Pleroma.Web.RelMe alias Pleroma.Web.RelMe
alias Pleroma.Workers.BackgroundWorker alias Pleroma.Workers.BackgroundWorker
alias Pleroma.Workers.UserRefreshWorker
require Logger require Logger
require Pleroma.Constants require Pleroma.Constants
@ -2165,12 +2166,9 @@ defmodule Pleroma.User do
end end
defp maybe_refresh(user) do defp maybe_refresh(user) do
fun = fn -> needs_update?(user) && fetch_by_ap_id(user.ap_id) end if needs_update?(user) do
UserRefreshWorker.new(%{"ap_id" => user.ap_id})
if Config.get([__MODULE__, :sync_refreshing], false) do |> Oban.insert()
fun.()
else
Task.start(fun)
end end
end end

View file

@ -0,0 +1,14 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# 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

View file

@ -953,8 +953,12 @@ defmodule Pleroma.UserTest do
{:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin") {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/admin")
# User was updated async, fetch from cache now # Oban job was generated to refresh the stale user
updated_user = User.get_cached_by_ap_id(user.ap_id) 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 assert updated_user.inbox