@middy/warmup
Advanced tools
Comparing version
@@ -78,2 +78,67 @@ const middy = require('../../core') | ||
}) | ||
test(`Should execute handler with callbackWaitsForEmptyEventLoop if waitForEmptyEventLoop true`, (endTest) => { | ||
console.log = jest.fn() | ||
const handler = middy((event, context, cb) => { | ||
cb() | ||
}) | ||
handler.use(lambdaIsWarmingUp({ | ||
waitForEmptyEventLoop: true | ||
})) | ||
const event = { | ||
source: 'serverless-plugin-warmup' | ||
} | ||
const context = {} | ||
handler(event, context, (_, response) => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toBe(true) | ||
expect(response).toBe('warmup') | ||
endTest() | ||
}) | ||
}) | ||
test(`Should execute handler with callbackWaitsForEmptyEventLoop if waitForEmptyEventLoop false`, (endTest) => { | ||
console.log = jest.fn() | ||
const handler = middy((event, context, cb) => { | ||
cb() | ||
}) | ||
handler.use(lambdaIsWarmingUp({ | ||
waitForEmptyEventLoop: false | ||
})) | ||
const event = { | ||
source: 'serverless-plugin-warmup' | ||
} | ||
const context = { | ||
callbackWaitsForEmptyEventLoop: true | ||
} | ||
handler(event, context, (_, response) => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toBe(false) | ||
expect(response).toBe('warmup') | ||
endTest() | ||
}) | ||
}) | ||
test(`Should execute handler with callbackWaitsForEmptyEventLoop unchanged if waitForEmptyEventLoop is not set`, (endTest) => { | ||
console.log = jest.fn() | ||
const handler = middy((event, context, cb) => { | ||
cb() | ||
}) | ||
handler.use(lambdaIsWarmingUp({})) | ||
const event = { | ||
source: 'serverless-plugin-warmup' | ||
} | ||
const context = { | ||
callbackWaitsForEmptyEventLoop: true | ||
} | ||
handler(event, context, (_, response) => { | ||
expect(context.callbackWaitsForEmptyEventLoop).toBe(true) | ||
expect(response).toBe('warmup') | ||
endTest() | ||
}) | ||
}) | ||
}) |
@@ -6,2 +6,3 @@ import middy from '../core' | ||
onWarmup?: (event: any) => void; | ||
waitForEmptyEventLoop?: boolean; | ||
} | ||
@@ -8,0 +9,0 @@ |
module.exports = (config) => { | ||
const defaults = { | ||
isWarmingUp: (event) => event.source === 'serverless-plugin-warmup', | ||
onWarmup: (event) => console.log('Exiting early via warmup Middleware') | ||
onWarmup: (event) => console.log('Exiting early via warmup Middleware'), | ||
waitForEmptyEventLoop: null | ||
} | ||
@@ -13,2 +14,5 @@ | ||
options.onWarmup(handler.event) | ||
if (options.waitForEmptyEventLoop !== null) { | ||
handler.context.callbackWaitsForEmptyEventLoop = Boolean(options.waitForEmptyEventLoop) | ||
} | ||
return handler.callback(null, 'warmup') | ||
@@ -15,0 +19,0 @@ } |
{ | ||
"name": "@middy/warmup", | ||
"version": "1.0.0-alpha.33", | ||
"version": "1.0.0-alpha.34", | ||
"description": "Warmup (cold start mitigation) middleware for the middy framework", | ||
@@ -45,3 +45,3 @@ "engines": { | ||
}, | ||
"gitHead": "57a3d407623fa66a19005e4ea714999e7c6922da" | ||
"gitHead": "28b6b37e0d1b07c5e1684c40e361e7ed36259ccf" | ||
} |
@@ -52,2 +52,3 @@ # Middy warmup middleware | ||
- `onWarmup`: a function that gets executed before the handler exits in case of warmup. By default the function just prints: `Exiting early via warmup Middleware`. | ||
- `waitForEmptyEventLoop`: a boolean value (`null` by default), that if set will change the current value for `context.callbackWaitsForEmptyEventLoop`. In some circumstances it might be useful to force this value to be `false` to make sure that the lambda quits as early as possible in case of warmup (for instance if you have created a database connection in a previous middleware, this might be hanging and keeping you lambda active until timeout). | ||
@@ -54,0 +55,0 @@ |
11687
26.36%161
61%93
1.09%