Keep track of a remote value and retry it when things go wrong.
Suppose your operation reports a Result through a message:
attempt : (Result String String -> msg) -> Cmd msgYou can track it in your model and schedule retries from update:
import Again.Policy as Policy
import Again.Remote as Remote
import Again.Schedule exposing (Schedule(..))
import Process
import Task
type alias Model =
Remote.Remote String String
type Msg
= Received (Result String String)
| Retry
init : () -> ( Model, Cmd Msg )
init _ =
( Remote.init (Policy.unlimited (Periodic { delay = 1000 }))
, attempt Received
)
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Received (Ok value) ->
( Remote.succeed value model, Cmd.none )
Received (Err error) ->
let
( next, delay ) =
Remote.fail error model
in
( next
, case delay of
Just milliseconds ->
Process.sleep milliseconds
|> Task.perform (always Retry)
Nothing ->
Cmd.none
)
Retry ->
( Remote.beginRetry model, attempt Received )A successful value can fail later. For example, a connection may close after it has opened.
See whether your value is available, still being attempted, or has failed.
The number of completed attempts and the latest error in a retry sequence.
Start tracking a value with its first attempt already under way. By default, all failures are retryable, subject to your policy.
Choose which errors should be retried. Changing the classifier leaves your current value and retry progress alone.
Describe your remote value with a readable label, such as
"waiting to retry (attempt 2/3)".
Get the successful value or the error that ended the retry sequence.
You get Nothing while an attempt or retry is pending.
Tell the Remote value that the wait time has elapsed. It will assume you
are actually retrying afterwards.
This keeps your retry context, if any. Otherwise you start a fresh attempt, so any previous value or error is discarded.
Tell the Remote that an attempt succeeded. This keeps the value and clears
its previous failures and attempt count.
Tell the Remote that an attempt failed, or that a successful value was lost.
The stored classifier and policy decide whether to retry.
You get a retry delay in milliseconds, or Nothing to give up. A failure after
success discards the value and starts counting failures from one again.
A remote value together with its retry policy and error classifier.