[#1149] Merge remote-tracking branch 'remotes/upstream/develop' into 1149-oban-job...
[akkoma] / lib / pleroma / web / oauth / token / clean_worker.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 tokens.
8 """
9 use GenServer
10
11 @ten_seconds 10_000
12 @one_day 86_400_000
13
14 @interval Pleroma.Config.get(
15 [:oauth2, :clean_expired_tokens_interval],
16 @one_day
17 )
18
19 alias Pleroma.Repo
20 alias Pleroma.Web.OAuth.Token
21 alias Pleroma.Workers.BackgroundWorker
22
23 defdelegate worker_args(queue), to: Pleroma.Workers.Helper
24
25 def start_link(_), do: GenServer.start_link(__MODULE__, %{})
26
27 def init(_) do
28 Process.send_after(self(), :perform, @ten_seconds)
29 {:ok, nil}
30 end
31
32 @doc false
33 def handle_info(:perform, state) do
34 %{"op" => "clean_expired_tokens"}
35 |> BackgroundWorker.new(worker_args(:background))
36 |> Repo.insert()
37
38 Process.send_after(self(), :perform, @interval)
39 {:noreply, state}
40 end
41
42 def perform(:clean), do: Token.delete_expired_tokens()
43 end