2019-05-22 15:44:50 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.OAuth.Token.CleanWorker do
|
|
|
|
@moduledoc """
|
|
|
|
The module represents functions to clean an expired oauth tokens.
|
|
|
|
"""
|
2019-08-14 15:55:17 +00:00
|
|
|
use GenServer
|
2019-05-22 15:44:50 +00:00
|
|
|
|
2019-08-14 15:57:50 +00:00
|
|
|
@ten_seconds 10_000
|
|
|
|
@one_day 86_400_000
|
2019-05-22 15:44:50 +00:00
|
|
|
|
|
|
|
@interval Pleroma.Config.get(
|
|
|
|
[:oauth2, :clean_expired_tokens_interval],
|
2019-08-14 15:57:50 +00:00
|
|
|
@one_day
|
2019-05-22 15:44:50 +00:00
|
|
|
)
|
|
|
|
|
2019-08-13 17:20:26 +00:00
|
|
|
alias Pleroma.Repo
|
2019-05-22 15:44:50 +00:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
2019-08-13 17:20:26 +00:00
|
|
|
alias Pleroma.Workers.BackgroundWorker
|
|
|
|
|
2019-08-31 16:08:56 +00:00
|
|
|
import Pleroma.Workers.WorkerHelper, only: [worker_args: 1]
|
2019-05-22 15:44:50 +00:00
|
|
|
|
2019-08-14 15:57:50 +00:00
|
|
|
def start_link(_), do: GenServer.start_link(__MODULE__, %{})
|
2019-05-22 15:44:50 +00:00
|
|
|
|
|
|
|
def init(_) do
|
2019-08-14 15:57:50 +00:00
|
|
|
Process.send_after(self(), :perform, @ten_seconds)
|
|
|
|
{:ok, nil}
|
2019-05-22 15:44:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def handle_info(:perform, state) do
|
2019-08-13 17:20:26 +00:00
|
|
|
%{"op" => "clean_expired_tokens"}
|
|
|
|
|> BackgroundWorker.new(worker_args(:background))
|
|
|
|
|> Repo.insert()
|
|
|
|
|
2019-05-22 15:44:50 +00:00
|
|
|
Process.send_after(self(), :perform, @interval)
|
|
|
|
{:noreply, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(:clean), do: Token.delete_expired_tokens()
|
|
|
|
end
|