Merge branch 'openapi/use-boolean-like-in-requests' into 'develop'
[akkoma] / lib / pleroma / web / oauth / token / clean_worker.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.OAuth.Token.CleanWorker do
6 @moduledoc """
7 The module represents functions to clean an expired OAuth and MFA tokens.
8 """
9 use GenServer
10
11 @ten_seconds 10_000
12 @one_day 86_400_000
13
14 alias Pleroma.MFA
15 alias Pleroma.Web.OAuth
16 alias Pleroma.Workers.BackgroundWorker
17
18 def start_link(_), do: GenServer.start_link(__MODULE__, %{})
19
20 def init(_) do
21 Process.send_after(self(), :perform, @ten_seconds)
22 {:ok, nil}
23 end
24
25 @doc false
26 def handle_info(:perform, state) do
27 BackgroundWorker.enqueue("clean_expired_tokens", %{})
28 interval = Pleroma.Config.get([:oauth2, :clean_expired_tokens_interval], @one_day)
29
30 Process.send_after(self(), :perform, interval)
31 {:noreply, state}
32 end
33
34 def perform(:clean) do
35 OAuth.Token.delete_expired_tokens()
36 MFA.Token.delete_expired_tokens()
37 end
38 end