Skip to content

Commit

Permalink
Wait for stdout flush in before exiting in nodejs 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
tmo-trustpilot authored and mhart committed Jul 8, 2017
1 parent 7c45442 commit 7dc51c1
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions nodejs4.3/run/awslambda-mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ function systemErr(str) {
process.stderr.write(formatErr(str) + '\n')
}

function handleResult(resultStr) {
process.stdout.write(resultStr)
function handleResult(resultStr, cb) {
if (!process.stdout.write(resultStr)) {
process.stdout.once('drain', cb)
} else {
process.nextTick(cb)
}
}

// Don't think this can be done in the Docker image
Expand Down Expand Up @@ -90,10 +94,13 @@ module.exports = {
'Max Memory Used: ' + Math.round(process.memoryUsage().rss / (1024 * 1024)) + ' MB',
'',
].join('\t'))

var exitCode = errored || errType ? 1 : 0
if (typeof resultStr == 'string') {
handleResult(resultStr)
handleResult(resultStr, function() { process.exit(exitCode) })
} else {
process.exit(exitCode)
}
process.exit(errored || errType ? 1 : 0)
},
reportFault: function(invokeId, msg, errName, errStack) {
errored = true
Expand Down Expand Up @@ -144,4 +151,3 @@ function randomAccountId() {
function arn(region, accountId, fnName) {
return 'arn:aws:lambda:' + region + ':' + accountId.replace(/[^\d]/g, '') + ':function:' + fnName
}

0 comments on commit 7dc51c1

Please sign in to comment.