Skip to content

Commit 404e0d2

Browse files
committed
Remove IPC
1 parent 298a34f commit 404e0d2

File tree

10 files changed

+49
-303
lines changed

10 files changed

+49
-303
lines changed

app/Main.hs

Lines changed: 0 additions & 9 deletions
This file was deleted.

package.yaml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: aws-lambda-haskell-runtime
2-
version: 1.1.1
2+
version: 2.0.0
33
github: "theam/aws-lambda-haskell-runtime"
44
license: Apache-2.0
55
author: Nikita Tchayka
@@ -23,11 +23,8 @@ library:
2323
- bytestring
2424
- http-client
2525
- http-types
26-
- optparse-generic
27-
- process
2826
- template-haskell
2927
- text
30-
- uuid
3128
- safe-exceptions-checked
3229
- path
3330
- path-io
@@ -36,14 +33,6 @@ library:
3633
- Aws.Lambda
3734
- Aws.Lambda.Runtime
3835

39-
executables:
40-
bootstrap-lol:
41-
source-dirs: app
42-
main: Main.hs
43-
dependencies:
44-
- aws-lambda-haskell-runtime
45-
- http-client
46-
4736
tests:
4837
aws-lambda-haskell-runtime-test:
4938
main: Spec.hs

src/Aws/Lambda/Configuration.hs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
11
{-# OPTIONS_GHC -fno-warn-unused-pattern-binds #-}
22
module Aws.Lambda.Configuration
33
( Main.LambdaOptions(..)
4-
, Main.getRecord
54
, configureLambda
6-
, bootstrapLambda
7-
, IPC.returnAndFail
8-
, IPC.returnAndSucceed
95
, Dispatch.decodeObj
10-
, DispatchNoIPC.encodeObj
6+
, Dispatch.encodeObj
117
)
128
where
139

1410
import qualified Language.Haskell.TH as Meta
1511

1612
import qualified Aws.Lambda.Meta.Dispatch as Dispatch
17-
import qualified Aws.Lambda.Meta.DispatchNoIPC as DispatchNoIPC
1813
import qualified Aws.Lambda.Meta.Main as Main
1914
import qualified Aws.Lambda.Meta.Run as Run
20-
import qualified Aws.Lambda.Runtime.IPC as IPC
2115

22-
{-| Generates a @main@ function to be used with the
23-
AWS Lambda layer.
16+
{-| Generates a @main@ function that acts as a dispatcher
2417
-}
2518
configureLambda :: Meta.DecsQ
2619
configureLambda = do
27-
main <- Main.generateIPC
20+
main <- Main.generate
2821
run <- Run.generate
2922
return (main <> [run])
30-
31-
{-| -}
32-
bootstrapLambda :: Meta.DecsQ
33-
bootstrapLambda = do
34-
main <- Main.generateDirectCall
35-
run <- Run.generateNoIPC
36-
return (main <> [run])

src/Aws/Lambda/Meta/Dispatch.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
module Aws.Lambda.Meta.Dispatch
33
( generate
44
, decodeObj
5+
, encodeObj
6+
, Runtime.LambdaResult(..)
57
) where
68

79
import Data.Function ((&))
@@ -13,6 +15,7 @@ import qualified Data.ByteString.Lazy.Char8 as LazyByteString
1315
import qualified Language.Haskell.TH as Meta
1416

1517
import Aws.Lambda.Meta.Common
18+
import qualified Aws.Lambda.Runtime.Common as Runtime
1619

1720
{-| Helper function that the dispatcher will use to
1821
decode the JSON that comes as an AWS Lambda event into the
@@ -24,6 +27,14 @@ decodeObj x =
2427
Left e -> error e
2528
Right v -> v
2629

30+
{-| Helper function that the dispatcher will use to
31+
decode the JSON that comes as an AWS Lambda event into the
32+
appropriate type expected by the handler.
33+
-}
34+
encodeObj :: ToJSON a => a -> String
35+
encodeObj x = LazyByteString.unpack (encode x)
36+
37+
2738
{-| Generates the dispatcher out of a list of
2839
handler names in the form @src/Foo/Bar.handler@
2940
@@ -43,7 +54,7 @@ handlerCase lambdaHandler = do
4354
let pat = Meta.LitP (Meta.StringL $ Text.unpack lambdaHandler)
4455
body <- [e|do
4556
result <- $(expressionName qualifiedName) (decodeObj $(expressionName "eventObject")) (decodeObj $(expressionName "contextObject"))
46-
either (returnAndFail $(expressionName "executionUuid")) (returnAndSucceed $(expressionName "executionUuid")) result |]
57+
either (pure . Left . encodeObj) (pure . Right . $(constructorName "LambdaResult") . encodeObj) result |]
4758
pure $ Meta.Match pat (Meta.NormalB body) []
4859
where
4960
qualifiedName =
@@ -56,6 +67,6 @@ unmatchedCase :: Meta.MatchQ
5667
unmatchedCase = do
5768
let pattern = Meta.WildP
5869
body <- [e|
59-
returnAndFail $(expressionName "executionUuid") ("Handler " <> $(expressionName "functionHandler") <> " does not exist on project")
70+
pure $ Left ("Handler " <> $(expressionName "functionHandler") <> " does not exist on project")
6071
|]
6172
pure $ Meta.Match pattern (Meta.NormalB body) []

src/Aws/Lambda/Meta/DispatchNoIPC.hs

Lines changed: 0 additions & 73 deletions
This file was deleted.

src/Aws/Lambda/Meta/Main.hs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
{-| main function generation for interoperation with the layer -}
22
module Aws.Lambda.Meta.Main
33
( Runtime.LambdaOptions(..)
4-
, generateIPC
5-
, generateDirectCall
6-
, Options.getRecord
4+
, generate
75
) where
86

97
import qualified Language.Haskell.TH as Meta
10-
import qualified Options.Generic as Options
118

129
import Aws.Lambda.Meta.Common
1310
import qualified Aws.Lambda.Runtime.Common as Runtime
1411

15-
-- | Generate the main function that the layer will call
16-
generateIPC :: Meta.DecsQ
17-
generateIPC = [d|
18-
$(declarationName "main") = getRecord "" >>= run
19-
|]
20-
21-
generateDirectCall :: Meta.DecsQ
22-
generateDirectCall = [d|
12+
-- | Generate the main function with the dispatcher
13+
generate :: Meta.DecsQ
14+
generate = [d|
2315
$(declarationName "main") = $(directCallBody)
2416
|]
2517
where
2618
directCallBody =
2719
[e|do
28-
runLambda $ $(constructorName "DirectCall") run
20+
runLambda run
2921
|]

src/Aws/Lambda/Meta/Run.hs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,16 @@
11
module Aws.Lambda.Meta.Run
22
( generate
3-
, generateNoIPC
43
) where
54

65
import qualified Language.Haskell.TH as Meta
76

87
import Aws.Lambda.Meta.Common
98
import qualified Aws.Lambda.Meta.Discover as Discover
109
import qualified Aws.Lambda.Meta.Dispatch as Dispatch
11-
import qualified Aws.Lambda.Meta.DispatchNoIPC as DispatchNoIPC
1210

13-
{-| Generate the run function
14-
15-
It will create a dispatcher that is a huge @case@ expression that
16-
expects the name of the handler provided by AWS Lambda, and will
17-
execute the appropriate user function
18-
-}
1911
generate :: Meta.DecQ
2012
generate = do
2113
handlers <- Meta.runIO Discover.handlers
2214
clause' <- getFieldsFrom "LambdaOptions" ["functionHandler", "contextObject", "eventObject", "executionUuid"]
2315
body <- Dispatch.generate handlers
2416
pure $ Meta.FunD (Meta.mkName "run") [Meta.Clause [clause'] (Meta.NormalB body) []]
25-
26-
generateNoIPC :: Meta.DecQ
27-
generateNoIPC = do
28-
handlers <- Meta.runIO Discover.handlers
29-
clause' <- getFieldsFrom "LambdaOptions" ["functionHandler", "contextObject", "eventObject", "executionUuid"]
30-
body <- DispatchNoIPC.generate handlers
31-
pure $ Meta.FunD (Meta.mkName "run") [Meta.Clause [clause'] (Meta.NormalB body) []]

src/Aws/Lambda/Runtime.hs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module Aws.Lambda.Runtime
22
( runLambda
3-
, Runtime.Mode(..)
43
, Runtime.LambdaResult(..)
54
) where
65

@@ -15,20 +14,19 @@ import qualified Aws.Lambda.Runtime.ApiInfo as ApiInfo
1514
import qualified Aws.Lambda.Runtime.Context as Context
1615
import qualified Aws.Lambda.Runtime.Environment as Environment
1716
import qualified Aws.Lambda.Runtime.Error as Error
18-
import qualified Aws.Lambda.Runtime.IPC as IPC
1917
import qualified Aws.Lambda.Runtime.Publish as Publish
2018
import qualified Aws.Lambda.Runtime.Common as Runtime
2119

2220
-- | Runs the user @haskell_lambda@ executable and posts back the
2321
-- results. This is called from the layer's @main@ function.
24-
runLambda :: Runtime.Mode -> IO ()
25-
runLambda mode = do
22+
runLambda :: Runtime.RunCallback -> IO ()
23+
runLambda callback = do
2624
manager <- Http.newManager httpManagerSettings
2725
forever $ do
2826
lambdaApi <- Environment.apiEndpoint `catch` variableNotSet
2927
event <- ApiInfo.fetchEvent manager lambdaApi `catch` errorParsing
3028
context <- Context.initialize event `catch` errorParsing `catch` variableNotSet
31-
((invokeAndRun mode manager lambdaApi event context
29+
((invokeAndRun callback manager lambdaApi event context
3230
`catch` \err -> Publish.parsingError err lambdaApi context manager)
3331
`catch` \err -> Publish.invocationError err lambdaApi context manager)
3432
`catch` \(err :: Error.EnvironmentVariableNotSet) -> Publish.runtimeInitError err lambdaApi context manager
@@ -41,45 +39,40 @@ httpManagerSettings =
4139
}
4240

4341
invokeAndRun
44-
:: Throws Error.Parsing
45-
=> Throws Error.Invocation
42+
:: Throws Error.Invocation
4643
=> Throws Error.EnvironmentVariableNotSet
47-
=> Runtime.Mode
44+
=> Runtime.RunCallback
4845
-> Http.Manager
4946
-> String
5047
-> ApiInfo.Event
5148
-> Context.Context
5249
-> IO ()
53-
invokeAndRun mode manager lambdaApi event context = do
54-
result <- invokeWithMode mode event context
50+
invokeAndRun callback manager lambdaApi event context = do
51+
result <- invokeWithMode callback event context
5552
Publish.result result lambdaApi context manager
5653
`catch` \err -> Publish.invocationError err lambdaApi context manager
5754

5855
invokeWithMode
5956
:: Throws Error.Invocation
60-
=> Throws Error.Parsing
6157
=> Throws Error.EnvironmentVariableNotSet
62-
=> Runtime.Mode
58+
=> Runtime.RunCallback
6359
-> ApiInfo.Event
6460
-> Context.Context
6561
-> IO Runtime.LambdaResult
66-
invokeWithMode mode event context =
67-
case mode of
68-
Runtime.IPC -> IPC.invoke (ApiInfo.event event) context
69-
(Runtime.DirectCall f) -> do
70-
handlerName <- Environment.handlerName
71-
let lambdaOptions = Runtime.LambdaOptions
72-
{ eventObject = LazyByteString.unpack $ ApiInfo.event event
73-
, contextObject = LazyByteString.unpack . encode $ context
74-
, functionHandler = handlerName
75-
, executionUuid = "" -- DirectCall doesnt use UUID
76-
}
77-
result <- f lambdaOptions
78-
case result of
79-
Left err ->
80-
throw $ Error.Invocation err
81-
Right value ->
82-
pure value
62+
invokeWithMode callback event context = do
63+
handlerName <- Environment.handlerName
64+
let lambdaOptions = Runtime.LambdaOptions
65+
{ eventObject = LazyByteString.unpack $ ApiInfo.event event
66+
, contextObject = LazyByteString.unpack . encode $ context
67+
, functionHandler = handlerName
68+
, executionUuid = "" -- DirectCall doesnt use UUID
69+
}
70+
result <- callback lambdaOptions
71+
case result of
72+
Left err ->
73+
throw $ Error.Invocation err
74+
Right value ->
75+
pure value
8376

8477
variableNotSet :: Error.EnvironmentVariableNotSet -> IO a
8578
variableNotSet (Error.EnvironmentVariableNotSet env) =

0 commit comments

Comments
 (0)