Limit the number of orphaned to delete at 100 every 10 mins due to the cascading queries that have to check oauth_authorizations and oauth_tokens tables.

This should keep ahead of most app registration spam and not overwhelm lower powered servers.
This commit is contained in:
Mark Felder 2024-09-04 11:43:43 -04:00
parent a1951f3af7
commit 53744bf146
2 changed files with 10 additions and 5 deletions

View file

@ -598,7 +598,7 @@ config :pleroma, Oban,
crontab: [ crontab: [
{"0 0 * * 0", Pleroma.Workers.Cron.DigestEmailsWorker}, {"0 0 * * 0", Pleroma.Workers.Cron.DigestEmailsWorker},
{"0 0 * * *", Pleroma.Workers.Cron.NewUsersDigestWorker}, {"0 0 * * *", Pleroma.Workers.Cron.NewUsersDigestWorker},
{"0 0 * * *", Pleroma.Workers.Cron.AppCleanupWorker} {"*/10 * * * *", Pleroma.Workers.Cron.AppCleanupWorker}
] ]
config :pleroma, Pleroma.Formatter, config :pleroma, Pleroma.Formatter,

View file

@ -166,9 +166,14 @@ defmodule Pleroma.Web.OAuth.App do
def maybe_update_owner(_), do: :ok def maybe_update_owner(_), do: :ok
@spec remove_orphans() :: :ok @spec remove_orphans(pos_integer()) :: :ok
def remove_orphans() do def remove_orphans(limit \\ 100) do
from(a in __MODULE__, where: is_nil(a.user_id)) Repo.transaction(fn ->
|> Repo.delete_all() from(a in __MODULE__, where: is_nil(a.user_id), limit: ^limit)
|> Repo.all()
|> Enum.each(&Repo.delete(&1))
end)
:ok
end end
end end