Packages
tarearbol
1.9.101
1.12.0
1.11.2
1.11.1
1.11.0
1.10.4
1.10.3
1.10.2
1.10.0
1.9.102
1.9.101
1.9.100
1.9.99
1.9.11
1.9.10
1.9.9
1.9.8
1.9.7
1.9.6
1.9.5
1.9.4
1.9.3
1.9.2
1.9.1
1.9.0
1.8.2
1.8.1
1.8.0
1.7.0
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.0
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.0
1.2.1
1.2.0
1.1.2
1.1.1
1.1.0
1.0.4
1.0.3
1.0.2
1.0.0
0.99.11
0.99.10
0.99.9
0.99.8
0.99.7
0.99.6
0.99.4
0.99.3
0.99.2
0.99.1
0.99.0
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.0
0.9.6
0.9.5
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.0
0.6.0
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.2
0.2.1
0.2.0
0.1.0
The supervised tree of tasks, simplifying the process of handling: - recurrent tasks - retried tasks - long tasks - etc
Current section
Files
Jump to
Current section
Files
lib/tarearbol/utils.ex
defmodule Tarearbol.Utils do
@moduledoc false
# Set of utilities used in `Tarearbol`. It currently includes human-to-machine
# interval conversions and options extractor.
use Boundary
@type interval ::
integer()
| float()
| :none
| :tiny
| :medium
| :regular
| :timeout
| :infinity
| :random
@doc """
Converts a human representation of time interval to the one
understandable by a computer. The conversion rules are:
- `integer` → amount to be used as is (milliseconds for `delay`, number for attempts);
- `float` → amount of thousands, rounded (seconds for `delay`, thousands for attempts);
- `:none` → `0`;
- `:tiny` → `10`;
- `:medium` → `100`;
- `:infinity` → `-1`, `:attempts` only.
"""
# credo:disable-for-lines:16 Credo.Check.Refactor.CyclomaticComplexity
@spec interval(interval(), list()) :: integer()
def interval(input, opts \\ []) do
case input do
0 -> -1
0.0 -> -1
msec when is_integer(msec) and msec > 0 -> msec
sec when is_float(sec) and sec > 0 -> round(1_000 * sec)
:tiny -> 10
:medium -> 100
:regular -> 1_000
:timeout -> 5_000
:infinity -> opts[:value] || -1
:random -> :rand.uniform((opts[:value] || 5_000) + 1) - 1
_ -> 0
end
end
@doc """
Adds an interval to the given `DateTime` instance (or to
`DateTime.utc_now` if none given, returning the `DateTime` instance.
"""
@spec add_interval(interval(), DateTime.t() | nil) :: DateTime.t()
def add_interval(input, to \\ nil)
def add_interval(input, nil), do: add_interval(input, DateTime.utc_now())
def add_interval(input, %DateTime{} = to) do
to
|> DateTime.to_unix(:millisecond)
|> Kernel.+(interval(input))
|> DateTime.from_unix!(:millisecond)
end
#############################################################################
@default_opts [
attempts: 0,
delay: 0,
timeout: 5_000,
raise: false,
accept_not_ok: true,
on_success: nil,
on_retry: :debug,
on_fail: :error
]
@doc false
@spec opts(keyword()) :: keyword()
def opts(opts),
do: Keyword.merge(Application.get_env(:tarearbol, :job_options, @default_opts), opts)
@doc false
@spec extract_opts(keyword(), atom() | [atom()], any()) :: {any(), keyword()}
def extract_opts(opts, name, default \\ nil)
def extract_opts(opts, name, default) when is_atom(name),
do: opts |> opts() |> Keyword.pop(name, default)
def extract_opts(opts, names, default) when is_list(names) and is_nil(default),
do: opts |> opts() |> Keyword.split(names)
#############################################################################
@doc false
def cron_to_time(at) when is_binary(at), do: raise("NYI: #{inspect(at)}")
@spec random_module_name(binary()) :: binary()
@doc false
def random_module_name(prefix \\ "M"),
do: Enum.join([prefix, abs(:erlang.unique_integer())], "_")
#############################################################################
@doc false
def get_opt(opts, name, default \\ nil),
do: Keyword.get(opts, name, Application.get_env(:tarearbol, name, default))
end