migration to move tokens expiration into Oban

This commit is contained in:
Alexander Strizhakov 2020-09-07 13:44:42 +03:00
parent c6647c08e1
commit e11fca88d4
No known key found for this signature in database
GPG key ID: 022896A53AEF1381

View file

@ -0,0 +1,36 @@
defmodule Pleroma.Repo.Migrations.MoveTokensExpirationIntoOban do
use Ecto.Migration
import Ecto.Query, only: [from: 2]
def change do
Supervisor.start_link([{Oban, Pleroma.Config.get(Oban)}],
strategy: :one_for_one,
name: Pleroma.Supervisor
)
if Pleroma.Config.get([:oauth2, :clean_expired_tokens]) do
from(t in Pleroma.Web.OAuth.Token, where: t.valid_until > ^NaiveDateTime.utc_now())
|> Pleroma.Repo.stream()
|> Stream.each(fn token ->
Pleroma.Workers.PurgeExpiredToken.enqueue(%{
token_id: token.id,
valid_until: DateTime.from_naive!(token.valid_until, "Etc/UTC"),
mod: Pleroma.Web.OAuth.Token
})
end)
|> Stream.run()
end
from(t in Pleroma.MFA.Token, where: t.valid_until > ^NaiveDateTime.utc_now())
|> Pleroma.Repo.stream()
|> Stream.each(fn token ->
Pleroma.Workers.PurgeExpiredToken.enqueue(%{
token_id: token.id,
valid_until: DateTime.from_naive!(token.valid_until, "Etc/UTC"),
mod: Pleroma.MFA.Token
})
end)
|> Stream.run()
end
end