forked from andrewMacmurray/elm-concurrent-task
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.elm
More file actions
231 lines (157 loc) · 4.99 KB
/
Main.elm
File metadata and controls
231 lines (157 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
port module Main exposing (main)
import ConcurrentTask as Task exposing (ConcurrentTask)
import ConcurrentTask.Http as Http
import ConcurrentTask.Process
import Json.Decode as Decode
{-| Many Requests
- This example fires off a long chain of http requests to a local server.
- Shows examples of batching requests and recovering from http errors.
-}
-- Model
type alias Flags =
{}
type alias Model =
{ tasks : Pool
}
type Msg
= OnProgress ( Pool, Cmd Msg )
| OnComplete (Task.Response Error Output)
-- Task Types
type alias Pool =
Task.Pool Msg
type alias Error =
Http.Error
type alias Output =
String
-- Init
init : Flags -> ( Model, Cmd Msg )
init _ =
let
( tasks, cmd ) =
Task.attempt
{ send = send
, onComplete = OnComplete
, pool = Task.pool
}
longRequestChain
in
( { tasks = tasks }, cmd )
-- Requests
longRequestChain : ConcurrentTask Error Output
longRequestChain =
Task.map3 join3
(longRequest 100)
(longRequest 100)
(httpError
|> Task.onError (\_ -> longRequest 100)
|> Task.andThenDo (longRequest 100)
)
|> Task.andThenDo (longRequest 100)
|> Task.andThenDo (retry 20 sometimesFails)
|> Task.andThenDo (retry 20 sometimesFails)
|> Task.andThenDo (retry 20 sometimesFails)
|> Task.onError (\_ -> httpError)
|> Task.onError (\_ -> httpError)
|> Task.onError (\_ -> httpError)
|> Task.onError (\_ -> httpError)
|> Task.onError (\_ -> httpError)
|> Task.onError (\_ -> longRequest 100)
|> Task.andThenDo (longRequest 100)
|> Task.andThenDo (longRequest 100)
|> Task.onError (\_ -> httpError)
|> Task.onError (\_ -> httpError)
|> Task.onError (\_ -> longRequest 1000)
|> Task.andThenDo
(httpError
|> Task.onError (\_ -> httpError)
|> Task.onError (\_ -> httpError)
|> Task.onError (\_ -> longRequest 500)
|> Task.andThenDo (longRequest 500)
)
|> Task.andThenDo (longRequest 300)
|> Task.return "Completed Http Requests"
longRequest : Int -> ConcurrentTask Http.Error String
longRequest ms =
Http.get
{ url = baseUrl ++ "/wait-then-respond/" ++ String.fromInt ms
, headers = []
, expect = Http.expectJson (Decode.field "message" Decode.string)
, timeout = Nothing
}
|> Task.debug Debug.toString Debug.toString
sometimesFails : ConcurrentTask Http.Error String
sometimesFails =
Http.get
{ url = baseUrl ++ "/flaky"
, headers = []
, expect = Http.expectJson (Decode.field "message" Decode.string)
, timeout = Nothing
}
|> Task.debug Debug.toString Debug.toString
httpError : ConcurrentTask Http.Error String
httpError =
Http.get
{ url = baseUrl ++ "/boom"
, headers = []
, expect = Http.expectJson (Decode.field "message" Decode.string)
, timeout = Nothing
}
|> Task.debug Debug.toString Debug.toString
{-| Simple retry mechanism with backoff
-}
retry : Int -> ConcurrentTask x a -> ConcurrentTask x a
retry n task =
retry_ n (Task.mapError (Tuple.pair n) task)
|> Task.mapError Tuple.second
retry_ : Int -> ConcurrentTask ( Int, x ) a -> ConcurrentTask ( Int, x ) a
retry_ max task =
task
|> Task.onError
(\( n, err ) ->
if n > 0 then
ConcurrentTask.Process.sleep ((max - n) * 50)
|> Task.andThenDo task
|> Task.mapError (Tuple.mapFirst (\n_ -> n_ - 1))
|> retry_ max
else
Task.fail ( n, err )
)
join3 : String -> String -> String -> String
join3 a b c =
String.join "," [ a, b, c ]
baseUrl : String
baseUrl =
"http://localhost:4000"
-- Update
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnComplete result ->
case result of
Task.Error Http.NetworkError ->
( model, printResult "NetworkError: make sure the local dev server is running - `npm run server`" )
_ ->
( model, printResult ("Result: " ++ Debug.toString result) )
OnProgress ( tasks, cmd ) ->
( { model | tasks = tasks }, cmd )
-- Subscriptions
subscriptions : Model -> Sub Msg
subscriptions model =
Task.onProgress
{ send = send
, receive = receive
, onProgress = OnProgress
}
model.tasks
-- Ports
port send : Decode.Value -> Cmd msg
port receive : (Decode.Value -> msg) -> Sub msg
port printResult : String -> Cmd msg
-- Program
main : Program Flags Model Msg
main =
Platform.worker
{ init = init
, update = update
, subscriptions = subscriptions
}