2019-08-31 16:08:56 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-08-31 16:08:56 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Workers.WorkerHelper do
|
|
|
|
alias Pleroma.Config
|
2019-08-31 18:58:42 +00:00
|
|
|
alias Pleroma.Workers.WorkerHelper
|
2019-08-31 16:08:56 +00: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 18:58:42 +00:00
|
|
|
|
|
|
|
defmacro __using__(opts) do
|
|
|
|
caller_module = __CALLER__.module
|
|
|
|
queue = Keyword.fetch!(opts, :queue)
|
2024-04-16 01:53:24 +00: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-16 02:07:28 +00:00
|
|
|
uniqueness = Keyword.get(opts, :unique, period: 1)
|
2019-08-31 18:58:42 +00:00
|
|
|
|
|
|
|
quote do
|
2019-09-02 11:57:40 +00:00
|
|
|
# Note: `max_attempts` is intended to be overridden in `new/2` call
|
|
|
|
use Oban.Worker,
|
|
|
|
queue: unquote(queue),
|
2024-04-16 01:53:24 +00:00
|
|
|
max_attempts: 1,
|
|
|
|
unique: unquote(uniqueness)
|
2019-09-02 11:57:40 +00:00
|
|
|
|
2020-06-23 12:09:01 +00:00
|
|
|
alias Oban.Job
|
|
|
|
|
2019-08-31 18:58:42 +00: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 12:09:01 +00:00
|
|
|
|> Oban.insert()
|
2019-08-31 18:58:42 +00:00
|
|
|
end
|
2022-11-13 23:55:51 +00:00
|
|
|
|
|
|
|
@impl Oban.Worker
|
|
|
|
def timeout(_job) do
|
|
|
|
queue_atom = String.to_atom(unquote(queue))
|
2022-11-14 15:07:26 +00:00
|
|
|
Config.get([:workers, :timeout, queue_atom], :timer.minutes(1))
|
2022-11-13 23:55:51 +00:00
|
|
|
end
|
2019-08-31 18:58:42 +00:00
|
|
|
end
|
|
|
|
end
|
2019-08-31 16:08:56 +00:00
|
|
|
end
|