Tarearbol (tarearbol v1.1.1) View Source

Tarearbol module provides an interface to run tasks in easy way.

Examples

iex> result = Tarearbol.ensure(fn -> raise "¡?" end, attempts: 1, raise: false)
iex> {:error, %{job: _job, outcome: outcome}} = result
iex> {error, _stacktrace} = outcome
iex> error
%RuntimeError{message: "¡?"}

Link to this section Summary

Functions

Executes all the scheduled tasks immediately, cleaning up the queue.

Ensures the task to be completed; restarts it when necessary.

Same as Tarearbol.ensure/2, but it raises on fail and returns the result itself on successful execution.

Executes Tarearbol.ensure_all_streamed/2 and collects tasks results.

Runs a task specified by the first argument at a given time.

Runs a task specified by the first argument in a given interval.

Spawns the task for the immediate async execution.

Spawns an ensured job asynchronously, passing all options given.

Link to this section Functions

Link to this function

drain(jobs \\ Tarearbol.Application.jobs())

View Source

Executes all the scheduled tasks immediately, cleaning up the queue.

Specs

ensure((() -> any()) | {atom(), atom(), list()}, keyword()) ::
  {:error, any()} | {:ok, any()}

Ensures the task to be completed; restarts it when necessary.

Possible options:

  • attempts [default: :infinity] Might be any of @Tarearbol.Utils.interval type (5 for five attempts, :random for the random amount etc)
  • delay [default: 1 msec]. Might be any of @Tarearbol.Utils.interval type (1_000 or 1.0 for one second, :timeout for five seconds etc)
  • on_success [default: nil], the function to be called on successful execution (arity ∈ [0, 1] or tuple {Mod, fun} where fun is of arity zero or one.) When the arity of given function is 1, the result of task execution is passed
  • on_retry [default: nil], same as above, called on retries after insuccessful attempts or one of [:debug, :info, :warn, :error] atoms to log a retry with default logger
  • on_fail [default: nil], same as above, called when the task finally failed after attempts amount of insuccessful attempts
Link to this function

ensure!(job, opts \\ [])

View Source

Specs

ensure!((() -> any()) | {atom(), atom(), list()}, keyword()) ::
  {:error, any()} | {:ok, any()}

Same as Tarearbol.ensure/2, but it raises on fail and returns the result itself on successful execution.

Link to this function

ensure_all(jobs, opts \\ [])

View Source

Specs

ensure_all([(() -> any()) | {atom(), atom(), list()}], keyword()) :: [
  error: any(),
  ok: any()
]

Executes Tarearbol.ensure_all_streamed/2 and collects tasks results.

Link to this function

ensure_all_streamed(jobs, opts \\ [])

View Source

Specs

ensure_all_streamed([(() -> any()) | {atom(), atom(), list()}], keyword()) ::
  %Stream{
    accs: term(),
    done: nil,
    enum: term(),
    funs: nonempty_maybe_improper_list()
  }

Wrapper for Task.Supervisor.async_stream/4.

Link to this function

run_at(job, at, opts \\ [])

View Source

Specs

run_at(
  (() -> any()) | {atom(), atom(), list()},
  DateTime.t() | String.t(),
  keyword()
) :: Task.t()

Runs a task specified by the first argument at a given time.

If the second parameter is a [DateTime] struct, the task will be run once. If the second parameter is a [Time] struct, the task will be run at that time on daily basis.

Link to this function

run_in(job, interval, opts \\ [])

View Source

Specs

run_in(
  (() -> any()) | {atom(), atom(), list()},
  atom() | integer() | float(),
  keyword()
) :: Task.t()

Runs a task specified by the first argument in a given interval.

See [Tarearbol.ensure/2] for all possible variants of the interval argument.

Specs

spawn((() -> any()) | {atom(), atom(), list()}, keyword()) :: Task.t()

Spawns the task for the immediate async execution.

Link to this function

spawn_ensured(job, opts)

View Source

Specs

spawn_ensured((() -> any()) | {atom(), atom(), list()}, keyword()) :: Task.t()

Spawns an ensured job asynchronously, passing all options given.