Created
May 23, 2019 00:06
-
-
Save theburningmonk/d5073bddd6445f4a7ba1bb672e0174f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Promise = require('bluebird') | |
const Log = require('@perform/lambda-powertools-logger') | |
module.exports = () => { | |
let isTimedOut = undefined | |
let promise = undefined | |
const resetPromise = () => { | |
if (promise) { | |
promise.cancel() | |
promise = undefined | |
} | |
} | |
return { | |
before: (handler, next) => { | |
const timeLeft = handler.context.getRemainingTimeInMillis() | |
handler.context.callbackWaitsForEmptyEventLoop = false | |
isTimedOut = undefined | |
promise = Promise.delay(timeLeft - 10).then(() => { | |
if (isTimedOut !== false) { | |
const awsRequestId = handler.context.awsRequestId | |
const invocationEvent = JSON.stringify(handler.event) | |
Log.error('invocation timed out', { awsRequestId, invocationEvent }) | |
} | |
}) | |
next() | |
}, | |
after: (handler, next) => { | |
isTimedOut = false | |
resetPromise() | |
next() | |
}, | |
onError: (handler, next) => { | |
isTimedOut = false | |
resetPromise() | |
next(handler.error) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment