2019-08-31 10:08:56 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-08-31 10:08:56 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Workers.WorkerHelper do
|
|
|
|
alias Pleroma.Config
|
2019-08-31 12:58:42 -06:00
|
|
|
alias Pleroma.Workers.WorkerHelper
|
2019-08-31 10:08:56 -06:00
|
|
|
|
|
|
|
def worker_args(queue) do
|
|
|
|
case Config.get([:workers, :retries, queue]) do
|
|
|
|
nil -> []
|
|
|
|
max_attempts -> [max_attempts: max_attempts]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sidekiq_backoff(attempt, pow \\ 4, base_backoff \\ 15) do
|
|
|
|
backoff =
|
|
|
|
:math.pow(attempt, pow) +
|
|
|
|
base_backoff +
|
|
|
|
:rand.uniform(2 * base_backoff) * attempt
|
|
|
|
|
|
|
|
trunc(backoff)
|
|
|
|
end
|
2019-08-31 12:58:42 -06:00
|
|
|
|
|
|
|
defmacro __using__(opts) do
|
|
|
|
caller_module = __CALLER__.module
|
|
|
|
queue = Keyword.fetch!(opts, :queue)
|
2024-04-15 19:53:24 -06:00
|
|
|
# by default just stop unintended duplicates - this can and should be overridden
|
|
|
|
# if you want to have a more complex uniqueness constraint
|
2024-04-15 19:58:50 -06:00
|
|
|
uniqueness = Keyword.get(opts, :unique, [period: 1])
|
2019-08-31 12:58:42 -06:00
|
|
|
|
|
|
|
quote do
|
2019-09-02 05:57:40 -06:00
|
|
|
# Note: `max_attempts` is intended to be overridden in `new/2` call
|
|
|
|
use Oban.Worker,
|
|
|
|
queue: unquote(queue),
|
2024-04-15 19:53:24 -06:00
|
|
|
max_attempts: 1,
|
|
|
|
unique: unquote(uniqueness)
|
2019-09-02 05:57:40 -06:00
|
|
|
|
2020-06-23 06:09:01 -06:00
|
|
|
alias Oban.Job
|
|
|
|
|
2019-08-31 12:58:42 -06:00
|
|
|
def enqueue(op, params, worker_args \\ []) do
|
|
|
|
params = Map.merge(%{"op" => op}, params)
|
|
|
|
queue_atom = String.to_atom(unquote(queue))
|
|
|
|
worker_args = worker_args ++ WorkerHelper.worker_args(queue_atom)
|
|
|
|
|
|
|
|
unquote(caller_module)
|
|
|
|
|> apply(:new, [params, worker_args])
|
2020-06-23 06:09:01 -06:00
|
|
|
|> Oban.insert()
|
2019-08-31 12:58:42 -06:00
|
|
|
end
|
2022-11-13 16:55:51 -07:00
|
|
|
|
|
|
|
@impl Oban.Worker
|
|
|
|
def timeout(_job) do
|
|
|
|
queue_atom = String.to_atom(unquote(queue))
|
2022-11-14 08:07:26 -07:00
|
|
|
Config.get([:workers, :timeout, queue_atom], :timer.minutes(1))
|
2022-11-13 16:55:51 -07:00
|
|
|
end
|
2019-08-31 12:58:42 -06:00
|
|
|
end
|
|
|
|
end
|
2019-08-31 10:08:56 -06:00
|
|
|
end
|