Skip to content
This repository has been archived by the owner on Oct 22, 2022. It is now read-only.

Commit

Permalink
implement callbackWaitsForEmptyEventLoop (#105)
Browse files Browse the repository at this point in the history
* implement callbackWaitsForEmptyEventLoop

per https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html

## The Context Object Properties (Node.js)

The context object provides the following property that you can update:

*callbackWaitsForEmptyEventLoop*

 > The default value is true. This property is useful only to modify the default behavior of the callback. By default, the callback will wait until the Node.js runtime event loop is empty before freezing the process and returning the results to the caller. You can set this property to false to request AWS Lambda to freeze the process soon after the callback is called, even if there are events in the event loop. AWS Lambda will freeze the process, any state data and the events in the Node.js event loop (any remaining events in the event loop processed when the Lambda function is called next and if AWS Lambda chooses to use the frozen process). For more information about callback, see Using the Callback Parameter.

* use compound names for callback wait property
  • Loading branch information
joekiller authored and moea committed Mar 14, 2018
1 parent 528d1ff commit f78c33f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions cljs-lambda/src-deps/cljs_lambda/externs/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ context.clientContext;
context.logGroupName;
context.logStreamName;
context.functionName;
context.callbackWaitsForEmptyEventLoop;

context.getMemoryLimitInMB = function() {};
context.getFunctionName = function() {};
Expand Down
28 changes: 26 additions & 2 deletions cljs-lambda/src/cljs_lambda/context.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@
associated with this context.")
(environment
[this]
"Retrieve a map of environment variables."))
"Retrieve a map of environment variables.")
(waits?
[this]
"By default, the callback will wait until the Node.js runtime event loop is
empty before freezing the process and returning the results to the caller.
You can set this property to false to request AWS Lambda to freeze the
process soon after the callback is called, even if there are events in the
event loop.")
(set-wait!
[this tf]
"Set the callback-waits"))

(defrecord ^:no-doc LambdaContext [js-handle]
ContextHandle
Expand All @@ -35,7 +45,21 @@
(msecs-remaining [this]
(.getRemainingTimeInMillis js-handle))
(environment [this]
(json->edn js/process.env)))
(json->edn js/process.env))
(waits? [this]
(.-callbackWaitsForEmptyEventLoop js-handle))
(set-wait! [this tf]
(set! (.-callbackWaitsForEmptyEventLoop js-handle) tf)))

(defn waits-on-event-loop?
[ctx]
"Returns state of context property callbackWaitsForEmptyEventLoop"
(waits? ctx))

(defn set-wait-on-event-loop!
"Set the context property callbackWaitsForEmptyEventLoop"
[ctx tf]
(set-wait! ctx tf))

(defn env
"Retrieve an environment variable by name, defaulting to `nil` if not found.
Expand Down
6 changes: 5 additions & 1 deletion cljs-lambda/src/cljs_lambda/local.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
(msecs-remaining [this]
-1)
(environment [this]
env))
env)
(waits? [this]
true)
(set-wait! [this tf]
tf))

(defn- stringify-keys
"Shallowly un-keyword/un-symbol the keys in m"
Expand Down
18 changes: 18 additions & 0 deletions cljs-lambda/test/cljs_lambda/test/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,21 @@
ctx (local/->context {:env {'ENV_VAR "deftest-async env"}})]
(p/then (invoke f nil ctx)
(will= "deftest-async env"))))

(deftest-async
waits
(let [f (lambda/async-lambda-fn
(fn [_ ctx]
(ctx/waits-on-event-loop? ctx)))]
(p/then
(invoke f)
(will= true))))

(deftest-async
set-waits
(let [f (lambda/async-lambda-fn
(fn [_ ctx]
(ctx/set-wait-on-event-loop! ctx false)))]
(p/then
(invoke f)
(will= false))))

0 comments on commit f78c33f

Please sign in to comment.