[#1149] Added more oban workers. Refactoring.
[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
10 # 10 seconds
11 @start_interval 10_000
12 @interval Pleroma.Config.get(
13 # 24 hours
14 [:oauth2, :clean_expired_tokens_interval],
15 86_400_000
16 )
17
18 alias Pleroma.Repo
19 alias Pleroma.Web.OAuth.Token
20 alias Pleroma.Workers.BackgroundWorker
21
22 defdelegate worker_args(queue), to: Pleroma.Workers.Helper
23
24 def start_link, do: GenServer.start_link(__MODULE__, nil)
25
26 def init(_) do
27 if Pleroma.Config.get([:oauth2, :clean_expired_tokens], false) do
28 Process.send_after(self(), :perform, @start_interval)
29 {:ok, nil}
30 else
31 :ignore
32 end
33 end
34
35 @doc false
36 def handle_info(:perform, state) do
37 %{"op" => "clean_expired_tokens"}
38 |> BackgroundWorker.new(worker_args(:background))
39 |> Repo.insert()
40
41 Process.send_after(self(), :perform, @interval)
42 {:noreply, state}
43 end
44
45 # Job Worker Callbacks
46 def perform(:clean), do: Token.delete_expired_tokens()
47 end