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
467 lines (326 loc) · 10.9 KB
/
Main.elm
File metadata and controls
467 lines (326 loc) · 10.9 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
port module Main exposing (main)
import Aws.S3 as S3
import Aws.SNS as SNS
import Aws.SQS as SQS
import ConcurrentTask as Task exposing (ConcurrentTask)
import ConcurrentTask.Time
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode
import Time
import Utils.Decode as Decode
import Utils.Encode as Encode
import Utils.Env as Env
import Utils.Logger as Logger
import Utils.Uuid as Uuid exposing (Uuid)
{-| Fruit Picking Worker 🍓🍑🍐
This is an example pipeline worker that talks to AWS services.
- It listens for messages on an SQS queue (using SQS long polling).
- Each message batch is processed concurrently and the results stored for later in an S3 bucket.
- An SNS message is sent for each message processed.
See the `processOrchards` function for more details.
-}
-- 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 Error
= SQSPollingError SQS.Error
| ProcessingError ProcessError
| EnvError Env.Error
type alias Output =
()
-- Env
type alias Env =
{ inQueue : String
, outBucket : String
, outTopic : String
}
withEnv : (Env -> ConcurrentTask Error a) -> ConcurrentTask Error a
withEnv f =
Env.load envParser
|> Task.mapError EnvError
|> Task.andThen f
envParser : Env.Parser Env
envParser =
Env.succeed Env
|> Env.required (Env.string "IN_QUEUE")
|> Env.required (Env.string "OUT_BUCKET")
|> Env.required (Env.string "OUT_TOPIC")
-- Init
init : Flags -> ( Model, Cmd Msg )
init _ =
let
( tasks, cmd ) =
startTask
{ pool = Task.pool
, task = processOrchards
}
in
( { tasks = tasks }
, cmd
)
startTask : { pool : Pool, task : Env -> SQS.Message -> ConcurrentTask ProcessError Output } -> ( Pool, Cmd Msg )
startTask { pool, task } =
Task.attempt
{ send = send
, onComplete = OnComplete
, pool = pool
}
(processMessagesWith task)
-- Update
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
OnComplete result ->
case result of
Task.UnexpectedError err ->
( model, printError ("Unexpected Error " ++ Debug.toString err) )
Task.Error (EnvError e) ->
( model, printError (Env.printError e) )
_ ->
let
( tasks, cmd ) =
startTask
{ pool = model.tasks
, task = processOrchards
}
in
( { model | tasks = tasks }, cmd )
OnProgress ( tasks, cmd ) ->
( { model | tasks = tasks }, cmd )
-- Process Fn
type alias Orchard =
{ fruit : String
, trees : Int
, yield : Yield
}
type alias Harvest =
{ fruit : String
, harvested : Int
, seeds : Int
, time : Time.Posix
}
type Yield
= High
| Medium
| Low
type alias MessageBody =
{ s3Key : String
, s3Bucket : String
}
type ProcessError
= DecodeMessageFailed Decode.Error
| GetFromBucketFailed S3.Error
| DecodeOrchardsFailed (List Decode.Error)
| SaveHarvestFailed S3.Error
| NotifyHarvestsFailed SNS.Error
{-| This is the main pipeline logic - the fruit picking process!
- Extracts a list of fruit tree orchards stored in an S3 bucket.
- Harvests the trees and saves the results in an output S3 bucket.
- Notifies saved harvests via SNS.
-}
processOrchards : Env -> SQS.Message -> ConcurrentTask ProcessError ()
processOrchards env message =
decodeMessage message.body
|> Task.andThen (Logger.withInfo "getting orchards from bucket.." << getFromBucket)
|> Task.andThen (Logger.withInfo "decoding orchards.." << decodeOrchards)
|> Task.andThen (Logger.withInfo "harvesting orchards.." << harvestOrchards)
|> Task.andThen (Logger.withInfo "saving harvests.." << saveHarvests env)
|> Task.andThen (Logger.withInfo "notifying saved harvests.." << notifySavedHarvests env)
|> Task.andThen (Logger.info << successMessage)
successMessage : List a -> String
successMessage harvests =
"success! harvested " ++ numberOf harvests ++ " orchards."
notifySavedHarvests : Env -> List String -> ConcurrentTask ProcessError (List String)
notifySavedHarvests env harvests =
SNS.publish
{ topicName = env.outTopic
, message = encodeSavedHarvestMessage harvests
}
|> Task.mapError NotifyHarvestsFailed
|> Task.return harvests
encodeSavedHarvestMessage : List String -> String
encodeSavedHarvestMessage harvests =
Encode.encode 0
(Encode.object
[ ( "message", Encode.string "harvests saved" )
, ( "harvests", Encode.list Encode.string harvests )
]
)
saveHarvests : Env -> ( Time.Posix, List Harvest ) -> ConcurrentTask ProcessError (List String)
saveHarvests env ( time, harvests ) =
harvests
|> List.map (saveHarvest env time)
|> Task.batch
saveHarvest : Env -> Time.Posix -> Harvest -> ConcurrentTask ProcessError String
saveHarvest env time harvest =
Uuid.generate
|> Task.andThen (saveHarvest_ env time harvest)
|> Task.mapError SaveHarvestFailed
saveHarvest_ : Env -> Time.Posix -> Harvest -> Uuid -> ConcurrentTask S3.Error String
saveHarvest_ env time harvest id =
let
key : String
key =
harvestFilePath id time harvest ++ ".json"
in
Encode.encode 0 (encodeHarvest harvest)
|> S3.putObject { bucket = env.outBucket, key = key }
|> Task.return key
harvestFilePath : Uuid -> Time.Posix -> Harvest -> String
harvestFilePath id time harvest =
String.join "_"
[ "harvest"
, String.fromInt (Time.posixToMillis time)
, harvest.fruit
, id
]
harvestOrchards : List Orchard -> ConcurrentTask x ( Time.Posix, List Harvest )
harvestOrchards orchards =
ConcurrentTask.Time.now
|> Task.map (toHarvests orchards)
toHarvests : List Orchard -> Time.Posix -> ( Time.Posix, List Harvest )
toHarvests orchards time =
( time, List.map (harvestOrchard time) orchards )
harvestOrchard : Time.Posix -> Orchard -> Harvest
harvestOrchard time orchard =
case orchard.yield of
High ->
{ fruit = orchard.fruit
, harvested = orchard.trees * 10
, seeds = orchard.trees * 30
, time = time
}
Medium ->
{ fruit = orchard.fruit
, harvested = orchard.trees * 5
, seeds = orchard.trees * 15
, time = time
}
Low ->
{ fruit = orchard.fruit
, harvested = orchard.trees * 3
, seeds = orchard.trees * 10
, time = time
}
decodeOrchards : String -> ConcurrentTask ProcessError (List Orchard)
decodeOrchards =
Decode.decodeJsonl decodeTodo
>> Result.mapError DecodeOrchardsFailed
>> Task.fromResult
getFromBucket : MessageBody -> ConcurrentTask ProcessError String
getFromBucket body =
Task.mapError GetFromBucketFailed
(S3.getObject
{ bucket = body.s3Bucket
, key = body.s3Key
}
)
decodeMessage : String -> ConcurrentTask ProcessError MessageBody
decodeMessage =
Decode.decodeString decodeMessageBody
>> Result.mapError DecodeMessageFailed
>> Task.fromResult
encodeHarvest : Harvest -> Encode.Value
encodeHarvest harvest =
Encode.object
[ ( "fruit", Encode.string harvest.fruit )
, ( "harvested", Encode.int harvest.harvested )
, ( "seeds", Encode.int harvest.seeds )
, ( "time", Encode.time harvest.time )
]
decodeMessageBody : Decoder MessageBody
decodeMessageBody =
Decode.map2 MessageBody
(Decode.field "s3_key" Decode.string)
(Decode.field "s3_bucket" Decode.string)
decodeTodo : Decoder Orchard
decodeTodo =
Decode.map3 Orchard
(Decode.field "fruit" Decode.string)
(Decode.field "trees" Decode.int)
(Decode.field "yield" decodeYield)
decodeYield : Decoder Yield
decodeYield =
Decode.string
|> Decode.andThen
(\yield ->
case String.toUpper yield of
"HIGH" ->
Decode.succeed High
"MEDIUM" ->
Decode.succeed Medium
"LOW" ->
Decode.succeed Low
_ ->
Decode.fail "Unrecognized Yield"
)
-- SQS
processMessagesWith : (Env -> SQS.Message -> ConcurrentTask ProcessError a) -> ConcurrentTask Error Output
processMessagesWith fn =
withEnv (processMessagesWith_ fn)
processMessagesWith_ : (Env -> SQS.Message -> ConcurrentTask ProcessError a) -> Env -> ConcurrentTask Error Output
processMessagesWith_ fn env =
Logger.info "polling for new messages.."
|> Task.andThenDo
(sqsReceiveMessages
{ queueName = env.inQueue
, waitTimeSeconds = 20
, visibilityTimeout = 2
, maxMessages = 10
}
)
|> Task.andThen (List.map (processMessage fn env) >> Task.batch)
|> Logger.inspect Debug.toString processedMessage
|> Task.return ()
processMessage : (Env -> SQS.Message -> ConcurrentTask ProcessError a) -> Env -> SQS.Message -> ConcurrentTask Error ()
processMessage processFn env msg =
processFn env msg
|> Task.mapError ProcessingError
|> Task.andThenDo
(sqsDeleteMessage
{ queueName = env.inQueue
, receiptHandle = msg.receiptHandle
}
)
processedMessage : List a -> String
processedMessage xs =
"poll complete - processed " ++ numberOf xs ++ " messages"
sqsReceiveMessages : SQS.ReceiveMessage -> ConcurrentTask Error (List SQS.Message)
sqsReceiveMessages =
SQS.receiveMessage >> Task.mapError SQSPollingError
sqsDeleteMessage : SQS.DeleteMessage -> ConcurrentTask Error ()
sqsDeleteMessage =
SQS.deleteMessage >> Task.mapError SQSPollingError
numberOf : List a -> String
numberOf =
List.length >> String.fromInt
-- 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 printError : String -> Cmd msg
-- Program
main : Program Flags Model Msg
main =
Platform.worker
{ init = init
, update = update
, subscriptions = subscriptions
}