Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getRemainingTimeInMillis() to the context when running locally. #179

Merged
merged 1 commit into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/node-lambda
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 10 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand All @@ -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":
Expand Down