2019-11-04 15:10:58 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/workers/worker_helper.ex
|
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
defmodule Mobilizon.Service.Workers.Helper do
|
2019-11-04 15:10:58 +01:00
|
|
|
@moduledoc """
|
|
|
|
Tools to ease dealing with workers
|
|
|
|
"""
|
2020-01-23 00:55:07 +01:00
|
|
|
|
2019-11-04 15:10:58 +01:00
|
|
|
alias Mobilizon.Config
|
2020-01-23 21:59:50 +01:00
|
|
|
alias Mobilizon.Service.Workers.Helper
|
2019-11-04 15:10:58 +01: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
|
|
|
|
|
|
|
|
defmacro __using__(opts) do
|
|
|
|
caller_module = __CALLER__.module
|
|
|
|
queue = Keyword.fetch!(opts, :queue)
|
|
|
|
|
|
|
|
quote do
|
|
|
|
# Note: `max_attempts` is intended to be overridden in `new/2` call
|
|
|
|
use Oban.Worker,
|
|
|
|
queue: unquote(queue),
|
|
|
|
max_attempts: 1
|
|
|
|
|
|
|
|
def enqueue(operation, params, worker_args \\ []) do
|
|
|
|
params = Map.merge(%{"op" => operation}, params)
|
|
|
|
queue_atom = String.to_existing_atom(unquote(queue))
|
2020-01-23 21:59:50 +01:00
|
|
|
worker_args = worker_args ++ Helper.worker_args(queue_atom)
|
2019-11-04 15:10:58 +01:00
|
|
|
|
|
|
|
unquote(caller_module)
|
|
|
|
|> apply(:new, [params, worker_args])
|
2020-02-18 08:57:00 +01:00
|
|
|
|> Oban.insert()
|
2019-11-04 15:10:58 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|