|
| 1 | +module Http.CmdRetry exposing |
| 2 | + ( with, newAttempt |
| 3 | + , RetryContext |
| 4 | + ) |
| 5 | + |
| 6 | +{-| Tries to perform a `Task` and retry it upon failure, allowing you to execute other `Cmd`s between each failure. |
| 7 | +
|
| 8 | +The following example executes a request with a retry policy when the request fails and |
| 9 | +executes a port before each new retry: |
| 10 | +
|
| 11 | + type Msg |
| 12 | + = OnRetry (Http.CmdRetry.RetryContext Msg Entity) |
| 13 | + | OnEntityRetrieved (RemoteData.RemoteData Http.Error.RequestError Entity) |
| 14 | +
|
| 15 | + type alias Model = |
| 16 | + { entity : RemoteData.RemoteData Http.Error.RequestError Entity } |
| 17 | +
|
| 18 | + init : ( Model, Cmd Msg ) |
| 19 | + init = |
| 20 | + ( { entity = RemoteData.Loading } |
| 21 | + , getEntity |
| 22 | + |> Http.CmdRetry.with |
| 23 | + [ Http.Retry.maxDuration 7000 |
| 24 | + , Http.Retry.exponentialBackoff { interval = 500, maxInterval = 3000 } |
| 25 | + ] |
| 26 | + [ Http.Retry.onUnauthenticatedStatus ] |
| 27 | + OnRetry |
| 28 | + ) |
| 29 | +
|
| 30 | + update : Msg -> Model -> ( Model, Cmd Msg ) |
| 31 | + update msg model = |
| 32 | + case msg of |
| 33 | + OnRetry retryContext -> |
| 34 | + ( model |
| 35 | + , Http.CmdRetry.newAttempt |
| 36 | + executePort |
| 37 | + OnEntityRetrieved |
| 38 | + retryContext |
| 39 | + ) |
| 40 | +
|
| 41 | + OnEntityRetrieved entity -> |
| 42 | + ( { model | entity = entity }, Cmd.none ) |
| 43 | +
|
| 44 | + getEntity : Task Never (RemoteData.RemoteData Http.Error.RequestError Entity) |
| 45 | + getEntity = |
| 46 | + Request.request |
| 47 | + { url = "<http://endpoint"> |
| 48 | + , headers = [] |
| 49 | + , body = Json.Encode.object [] |
| 50 | + , documentDecoder = JsonApi.Decode.resources "resource-type" entityDecoder |
| 51 | + } |
| 52 | +
|
| 53 | +
|
| 54 | +# Retry |
| 55 | +
|
| 56 | +@docs with, newAttempt |
| 57 | +
|
| 58 | +
|
| 59 | +# Types |
| 60 | +
|
| 61 | +@docs RetryContext |
| 62 | +
|
| 63 | +-} |
| 64 | + |
| 65 | +import Http.Error |
| 66 | +import Http.Internal as Internal |
| 67 | +import RemoteData |
| 68 | +import Task exposing (Task) |
| 69 | +import Time |
| 70 | + |
| 71 | + |
| 72 | +{-| Type used by the module to keep the context of the retry process. |
| 73 | +You will never handle it directly |
| 74 | +
|
| 75 | +`msg` is your `Msg` type and `data` is the data you you to receive from your request. |
| 76 | +
|
| 77 | +-} |
| 78 | +type RetryContext msg data |
| 79 | + = FailedTask (Context msg data) |
| 80 | + | FinishedTask (Task Never (RemoteData.RemoteData Http.Error.RequestError data)) |
| 81 | + |
| 82 | + |
| 83 | +type alias Context msg data = |
| 84 | + { partContext : PartContext msg data |
| 85 | + , lastError : Http.Error.RequestError |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +type alias PartContext msg data = |
| 90 | + { startTime : Int |
| 91 | + , failureConditions : List Internal.FailureCondition |
| 92 | + , originalTask : Task Never (RemoteData.RemoteData Http.Error.RequestError data) |
| 93 | + , onRetryMsg : RetryContext msg data -> msg |
| 94 | + , policies : List (Internal.Policy Http.Error.RequestError) |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | +{-| Attempt a new retry from your update function with the `RetryContext` you received. |
| 99 | +The first parameter allows you to execute a `Cmd` just before the next retry. |
| 100 | +The second parameter is the message you want to send when the request finally succeeded or failed (after all configured retries) |
| 101 | +
|
| 102 | + update : Msg -> Model -> ( Model, Cmd Msg ) |
| 103 | + update msg model = |
| 104 | + case msg of |
| 105 | + OnRetry retryContext -> |
| 106 | + ( model |
| 107 | + , Http.CmdRetry.newAttempt |
| 108 | + (\lastError -> |
| 109 | + doSomethingWithLastError lastError |
| 110 | + ) |
| 111 | + OnRequestDone |
| 112 | + retryContext |
| 113 | + ) |
| 114 | +
|
| 115 | +-} |
| 116 | +newAttempt : |
| 117 | + (Http.Error.RequestError -> Cmd msg) |
| 118 | + -> (RemoteData.RemoteData Http.Error.RequestError data -> msg) |
| 119 | + -> RetryContext msg data |
| 120 | + -> Cmd msg |
| 121 | +newAttempt onRetryCmd msg retryContext = |
| 122 | + case retryContext of |
| 123 | + FailedTask ({ partContext, lastError } as context) -> |
| 124 | + Cmd.batch |
| 125 | + [ onRetryCmd lastError |
| 126 | + , retryFromContext partContext |
| 127 | + |> Task.perform partContext.onRetryMsg |
| 128 | + ] |
| 129 | + |
| 130 | + FinishedTask task -> |
| 131 | + Task.perform msg task |
| 132 | + |
| 133 | + |
| 134 | +{-| Tries to execute the given task. You will receive a message with the retry context. |
| 135 | +From there you will call `newAttempt` which will handle your request retry, allowing you |
| 136 | +to execute a `Cmd` before the next retry. |
| 137 | +
|
| 138 | + originalTask |
| 139 | + |> Http.CmdRetry.with |
| 140 | + [ Http.Retry.maxDuration 7000 |
| 141 | + , Http.Retry.exponentialBackoff { interval = 500, maxInterval = 3000 } |
| 142 | + ] |
| 143 | + [ Http.Retry.onUnauthenticatedStatus ] |
| 144 | + OnRetry |
| 145 | +
|
| 146 | +-} |
| 147 | +with : |
| 148 | + List (Internal.Policy Http.Error.RequestError) |
| 149 | + -> List Internal.FailureCondition |
| 150 | + -> (RetryContext msg data -> msg) |
| 151 | + -> Task Never (RemoteData.RemoteData Http.Error.RequestError data) |
| 152 | + -> Cmd msg |
| 153 | +with errTasks failureConditions onRetryMsg originalTask = |
| 154 | + Task.map Time.posixToMillis Time.now |
| 155 | + |> Task.map |
| 156 | + (\nowMillis -> |
| 157 | + { policies = errTasks |
| 158 | + , startTime = nowMillis |
| 159 | + , failureConditions = failureConditions |
| 160 | + , onRetryMsg = onRetryMsg |
| 161 | + , originalTask = originalTask |
| 162 | + } |
| 163 | + ) |
| 164 | + |> Task.andThen retryFromContext |
| 165 | + |> Task.perform onRetryMsg |
| 166 | + |
| 167 | + |
| 168 | +retryFromContext : PartContext msg data -> Task Never (RetryContext msg data) |
| 169 | +retryFromContext { startTime, policies, originalTask, onRetryMsg, failureConditions } = |
| 170 | + let |
| 171 | + onError time currPolicies err = |
| 172 | + currPolicies |
| 173 | + |> List.map (\((Internal.Policy nextPolicy) as cfg) -> nextPolicy time cfg err) |
| 174 | + |> Task.sequence |
| 175 | + |> Task.map |
| 176 | + (\nextPolicies -> |
| 177 | + FailedTask |
| 178 | + { partContext = |
| 179 | + { startTime = time |
| 180 | + , failureConditions = failureConditions |
| 181 | + , originalTask = originalTask |
| 182 | + , onRetryMsg = onRetryMsg |
| 183 | + , policies = nextPolicies |
| 184 | + } |
| 185 | + , lastError = err |
| 186 | + } |
| 187 | + ) |
| 188 | + |> Task.onError (RemoteData.Failure >> Task.succeed >> FinishedTask >> Task.succeed) |
| 189 | + in |
| 190 | + originalTask |
| 191 | + |> Task.mapError (always (Http.Error.CustomError "no error")) |
| 192 | + |> Task.andThen (Internal.convertToError failureConditions) |
| 193 | + |> Task.map (Task.succeed >> FinishedTask) |
| 194 | + |> Task.onError (onError startTime policies) |
0 commit comments