mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2024-11-13 12:31:13 +00:00
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:
parent
825541b27c
commit
9c67637255
5 changed files with 24 additions and 10 deletions
0
changelog.d/user-refresh-rework.skip
Normal file
0
changelog.d/user-refresh-rework.skip
Normal 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.User, sync_refreshing: true
|
||||
|
||||
if File.exists?("./config/test.secret.exs") do
|
||||
import_config "test.secret.exs"
|
||||
else
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
14
lib/pleroma/workers/user_refresh_worker.ex
Normal file
14
lib/pleroma/workers/user_refresh_worker.ex
Normal 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
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue