Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Created May 23, 2019 00:06
Show Gist options
  • Save theburningmonk/d5073bddd6445f4a7ba1bb672e0174f9 to your computer and use it in GitHub Desktop.
Save theburningmonk/d5073bddd6445f4a7ba1bb672e0174f9 to your computer and use it in GitHub Desktop.
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