Conduit v0.12.10 Conduit.Util View Source

Provides utilities to wait for something to happen

Link to this section Summary

Functions

Attempts to run a function and retry’s if it fails

Runs a function until it returns a truthy value

Link to this section Types

Link to this type attempt_function() View Source
attempt_function() ::
  (() -> {:error, term()} | term() | no_return())
  | (integer() -> {:error, term()} | term() | no_return())

Link to this section Functions

Link to this function retry(opts \\ [], fun) View Source
retry(opts :: Keyword.t(), attempt_function()) :: term()

Attempts to run a function and retry’s if it fails.

Allows the following options:

Options

  • attempts - Number of times to run the function before giving up. (defaults to 3)
  • backoff_factor - What multiple of the delay should be backoff on each attempt. For a backoff of 2, on each retry we double the amount of time of the last delay. Set to 1 to use the same delay each retry. (defaults to 2)
  • jitter - Size of randomness applied to delay. This is useful to prevent multiple processes from retrying at the same time. (defaults to 0)
  • delay - How long to wait between attempts. (defaults to 1000ms)

Examples

Conduit.Util.retry(fn ->
  # thing that sometimes fails
end)

Conduit.Util.retry([attempts: 20, delay: 100], fn ->
  # thing that sometimes fails
end)
Link to this function wait_until(timeout \\ :infinity, fun) View Source
wait_until(timeout :: integer() | :infinity, attempt_function()) ::
  :ok | {:error, term()}

Runs a function until it returns a truthy value.

A timeout can optionally be specified to limit how long a function is attempted.

Examples

Conduit.Util.wait_until(fn ->
  table
  |> :ets.lookup(:thing)
  |> List.first()
end)

Conduit.Util.wait_until(30_000, fn ->
  table
  |> :ets.lookup(:thing)
  |> List.first()
end)