From 585386d2da7458fe3d38deec2be5b88e5dd78705 Mon Sep 17 00:00:00 2001 From: Nathan Maves Date: Sun, 15 Jan 2017 21:56:58 -0700 Subject: [PATCH 1/2] Added getRemainingTimeInMillis() to the context when running locally. --- bin/node-lambda | 2 ++ lib/main.js | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/bin/node-lambda b/bin/node-lambda index 4ae061a8..9044c896 100755 --- a/bin/node-lambda +++ b/bin/node-lambda @@ -23,6 +23,7 @@ var AWS_HANDLER = process.env.AWS_HANDLER || 'index.handler'; var AWS_ROLE = process.env.AWS_ROLE_ARN || process.env.AWS_ROLE || 'missing'; var AWS_MEMORY_SIZE = process.env.AWS_MEMORY_SIZE || 128; var AWS_TIMEOUT = process.env.AWS_TIMEOUT || 60; +var AWS_RUN_TIMEOUT = process.env.AWS_RUN_TIMEOUT || 3; var AWS_DESCRIPTION = process.env.AWS_DESCRIPTION || packageJson.description || ''; var AWS_RUNTIME = process.env.AWS_RUNTIME || 'nodejs4.3'; var AWS_PUBLISH = process.env.AWS_PUBLISH || false; @@ -92,6 +93,7 @@ program .option('-H, --handler [' + AWS_HANDLER + ']', 'Lambda Handler {index.handler}', AWS_HANDLER) .option('-j, --eventFile [' + EVENT_FILE + ']', 'Event JSON File', EVENT_FILE) .option('-u, --runtime [' + AWS_RUNTIME + ']', 'Lambda Runtime', AWS_RUNTIME) + .option('-t, --timeout [' + AWS_RUN_TIMEOUT + ']', 'Lambda Timeout', AWS_RUN_TIMEOUT) .option('-f, --configFile [' + CONFIG_FILE + ']', 'Path to file holding secret environment variables (e.g. "deploy.env")', CONFIG_FILE) .option('-x, --contextFile [' + CONTEXT_FILE + ']', 'Context JSON File', CONTEXT_FILE) diff --git a/lib/main.js b/lib/main.js index ad34deb0..72f5211c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -51,10 +51,13 @@ Lambda.prototype.run = function (program) { var event = require(process.cwd() + '/' + program.eventFile); var context = require(process.cwd() + '/' + program.contextFile); - this._runHandler(handler, event, program.runtime, context); + this._runHandler(handler, event, program, context); }; -Lambda.prototype._runHandler = function (handler, event, runtime, context) { +Lambda.prototype._runHandler = function (handler, event, program, context) { + + var startTime = new Date(); + var timeout = program.timeout * 1000; // convert the timeout into milliseconds var callback = function (err, result) { if (err) { @@ -69,7 +72,7 @@ Lambda.prototype._runHandler = function (handler, event, runtime, context) { } }; - var isNode43 = (runtime === "nodejs4.3"); + var isNode43 = (program.runtime === "nodejs4.3"); context.succeed = function (result) { if (isNode43) { console.log('context.succeed() is deprecated with Node.js 4.3 runtime'); @@ -88,6 +91,10 @@ Lambda.prototype._runHandler = function (handler, event, runtime, context) { } callback(); }; + context.getRemainingTimeInMillis = function () { + var currentTime = new Date(); + return timeout - (currentTime - startTime); + }; switch(runtime) { case "nodejs": From 49343081b09655810bfaf6486a04b47029b019ef Mon Sep 17 00:00:00 2001 From: Nathan Maves Date: Wed, 18 Jan 2017 13:58:57 -0700 Subject: [PATCH 2/2] Enforced the max of 5 minutes for the runtime timeout and updated the README docs. --- README.md | 9 +++++++++ lib/main.js | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6a388757..c924ffe0 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ $ node-lambda run --help -j, --eventFile [event.json] Event JSON File -f, --configFile [] Path to file holding secret environment variables (e.g. "deploy.env") -u, --runtime [nodejs4.3] Lambda Runtime {nodejs4.3, nodejs} - "nodejs4.3" is the current standard, "nodejs" is v0.10.36 + -t, --timeout [3] Lambda Timeout in seconds (max of 300) -x, --contextFile [context.json] Context JSON file ``` @@ -142,6 +143,14 @@ a callback function. These will still work for now for backward compatibility, v0.10.36 is still supported, and can be targeted by changing the `AWS_RUNTIME` value to `nodejs` in the `.env` file. +Runtime context options available : + +- context.getRemainingTimeInMillis() +- context.done() ***deprecated*** +- context.fail() ***deprecated*** +- context.succeed() ***deprecated*** + + ## Post install script When running `node-lambda deploy` if you need to do some action after `npm install --production` and before deploying to AWS Lambda (e.g. replace some modules with precompiled ones or download some libraries, replace some config file depending on environment) you can create `post_install.sh` script. If the file exists the script will be executed (and output shown after execution) if not it is skipped. Environment string is passed to script as first parameter so you can use it if needed. Make sure that the script is executable. diff --git a/lib/main.js b/lib/main.js index 72f5211c..581a3c74 100644 --- a/lib/main.js +++ b/lib/main.js @@ -57,7 +57,7 @@ Lambda.prototype.run = function (program) { Lambda.prototype._runHandler = function (handler, event, program, context) { var startTime = new Date(); - var timeout = program.timeout * 1000; // convert the timeout into milliseconds + var timeout = Math.min(program.timeout, 300) * 1000; // convert the timeout into milliseconds var callback = function (err, result) { if (err) {