Run tasks with a retry policy. The first attempt runs immediately; the schedule controls the waits between subsequent attempts.
Retry a GET up to three total attempts on timeouts or network errors:
import Again.Http
import Again.Policy as Policy
import Again.Schedule exposing (Schedule(..))
import Again.Task exposing (retryIf)
import Http
import Task
type Msg
= ReceivedResponse (Result Http.Error String)
loadMessage : Cmd Msg
loadMessage =
Http.task
{ method = "GET"
, headers = []
, url = "/message.txt"
, body = Http.emptyBody
, resolver = Http.stringResolver Again.Http.resolve
, timeout = Just 5000
}
|> retryIf isTransient
{ schedule = Periodic { delay = 1000 }
, limit = Policy.MaxAttempts 3
}
|> Task.attempt ReceivedResponse
isTransient : Http.Error -> Bool
isTransient error =
error == Http.Timeout || error == Http.NetworkErrorRetries only errors accepted by the predicate. Returns the last error when it is rejected or the attempt limit is reached.
For HTTP tasks, Again.Http.isRetryable supplies a default predicate.
Retries any failure until the task succeeds or the attempt limit is reached.