Extg (extg v0.1.1)

Task Group that expects all tasks to succeed or fail fast due to one fails.

Example

Normal case:

res =
  Extg.new()
  |> Extg.add(fn -> :ok end)
  |> Extg.add(fn -> :ok end)
  |> Extg.wait()

res # => [:ok, :ok]

If one of tasks fails:

res =
  Extg.new()
  |> Extg.add(fn ->
    :timer.sleep(5_000)
    :ok
  end)
  |> Extg.add(fn ->
    :timer.sleep(1_000)
    raise "something wrong"
    :ok
  end)
  |> Extg.wait()

res # => {:error, %RuntimeError{message: "something wrong"}}

In this case, wait() will stop as soon as error raised in the 2nd task. The first task was sent a :exit.

For more details please see the test cases or document for each functions.

Link to this section Summary

Functions

Add a task (in the form of an anonymous function) to the task group. Returning pid of task group.

Returns a specification to start this module under a supervisor.

Manually exit the task group process, letting all tasks exit too.

Create a new task group process, returning a pid of it if succeeds.

Wait for all tasks in task group to complete. It blocks. If timeout is :infinity (default), this would block ad infinite.

Link to this section Functions

Specs

add(pid(), function()) :: pid()

Add a task (in the form of an anonymous function) to the task group. Returning pid of task group.

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Specs

close(pid()) :: true

Manually exit the task group process, letting all tasks exit too.

Specs

new() :: pid()

Create a new task group process, returning a pid of it if succeeds.

Link to this function

wait(tg, timeout \\ :infinity)

Specs

wait(pid(), timeout :: atom() | integer()) :: [any()] | {:error, term()}

Wait for all tasks in task group to complete. It blocks. If timeout is :infinity (default), this would block ad infinite.