Tarearbol    Kantox ❤ OSS   Test    Dialyzer

Lightweight task manager, allowing retries, callbacks, assurance that the task succeeded, and more…

Installation

def deps do
  [
    {:tarearbol, "~> 1.0"}
  ]
end

Features

Task supervision tree for granted

Add :tarearbol to the list of applications and you are all set.

Infinite retries

Tarearbol.ensure fn ->
  unless Enum.random(1..100) == 42, do: raise "Incorrect answer"
  {:ok, 42}
end

# some bad-case logging
{:ok, 42}

Async execution of many tasks with retries

res = 1..20
      |> Enum.map(fn i ->
        fn -> Process.sleep(Enum.random(1..i)); i end
      end)
      |> Tarearbol.Job.ensure_all(attempts: 1)

[{:ok, 1}, {:ok, 2}, ..., {:ok, 20}]

Limited amount of retries

Tarearbol.ensure fn ->
  raise "Incorrect answer"
end, attempts: 10

# some bad-case logging
{:error,
 %{job: #Function<20.87737649/0 in :erl_eval.expr/5>,
   outcome: {%RuntimeError{message: "Incorrect answer"},
    [{:erl_eval, :do_apply, 6, [file: &#39;erl_eval.erl&#39;, line: 668]},
     {Task.Supervised, :do_apply, 2,
      [file: &#39;lib/task/supervised.ex&#39;, line: 85]},
     {Task.Supervised, :reply, 5, [file: &#39;lib/task/supervised.ex&#39;, line: 36]},
     {:proc_lib, :init_p_do_apply, 3, [file: &#39;proc_lib.erl&#39;, line: 247]}]}}}

Delay between retries

Tarearbol.ensure fn ->
  unless Enum.random(1..100) == 42, do: raise "Incorrect answer"
  {:ok, 42}
end, delay: 1000

# some slow bad-case logging
{:ok, 42}

Callbacks

Tarearbol.ensure fn ->
  unless Enum.random(1..100) == 42, do: raise "Incorrect answer"
  {:ok, 42}
end, on_success: fn data -> IO.inspect(data, label: "★") end,
     on_retry: fn data -> IO.inspect(data, label: "☆") end

# some slow bad-case logging
# ⇓⇓⇓⇓ one or more of ⇓⇓⇓⇓
: %{cause: :on_raise,
  data: {%RuntimeError{message: "Incorrect answer"},
   [{:erl_eval, :do_apply, 6, [file: &#39;erl_eval.erl&#39;, line: 670]},
    {:erl_eval, :exprs, 5, [file: &#39;erl_eval.erl&#39;, line: 122]},
    {Task.Supervised, :do_apply, 2, [file: &#39;lib/task/supervised.ex&#39;, line: 85]},
    {Task.Supervised, :reply, 5, [file: &#39;lib/task/supervised.ex&#39;, line: 36]},
    {:proc_lib, :init_p_do_apply, 3, [file: &#39;proc_lib.erl&#39;, line: 247]}]}}
# ⇑⇑⇑⇑ one or more of ⇑⇑⇑⇑
: 42
{:ok, 42}

Allowed options

for :attempts and :delay keys one might specify the following values:

Task spawning

Tarearbol.run_in fn -> IO.puts(42) end, 1_000 # 1 sec
Tarearbol.spawn fn -> IO.puts(42) end # immediately

Task draining

Tarearbol.run_in fn -> IO.inspect(42) end, 1_000 # 1 sec
Tarearbol.run_in fn -> IO.inspect(:foo) end, 1_000 # 1 sec
Tarearbol.drain
42       # immediately, from `IO.inspect`
:foo     # immediately, from `IO.inspect`
[ok: 42, ok: :foo] # immediately, the returned value(s)

Dynamic Workers Management

Changelog

Documentation