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

Always use a dedicated artifact for warmup function #69

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"homepage": "https://github.com/FidelLimited/serverless-plugin-warmup",
"dependencies": {
"archiver": "^3.0.0",
"bluebird": "^3.5.0",
"fs-extra": "^3.0.1"
},
Expand Down
53 changes: 48 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
* @requires 'bluebird'
* @requires 'fs-extra'
* @requires 'path'
* @requires 'archiver'
* */
const BbPromise = require('bluebird')
const fs = BbPromise.promisifyAll(require('fs-extra'))
const path = require('path')
const archiver = require('archiver')

const artifactPath = './.serverless/_warmup.zip'

/**
* @classdesc Keep your lambdas warm during Winter
Expand Down Expand Up @@ -39,6 +43,19 @@ class WarmUP {
}
}

archiveDirectory (src, dest, format = 'zip', archiverOptions = {}, directoryDestPath = false) {
return new Promise((resolve, reject) => {
const archive = archiver(format, archiverOptions)
const output = fs.createWriteStream(dest)
archive.pipe(output)
archive.directory(src, directoryDestPath)
archive.finalize()
output.on('close', () => resolve(archive))
archive.on('error', (err) => reject(err))
archive.on('warning', (err) => this.serverless.cli.log(err))
})
}

/**
* @description After package initialize hook. Create warmer function and add it to the service.
*
Expand All @@ -47,9 +64,9 @@ class WarmUP {
*
* @return {(boolean|Promise)}
* */
afterPackageInitialize () {
afterPackageInitialize () { /* eslint-disable operator-linebreak */
// See https://github.com/serverless/serverless/issues/2631
this.options.stage = this.options.stage
this.options.stage = this.options.stage
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice that you fix this style issues!

|| this.serverless.service.provider.stage
|| (this.serverless.service.defaults && this.serverless.service.defaults.stage)
|| 'dev'
Expand All @@ -58,7 +75,7 @@ class WarmUP {
|| (this.serverless.service.defaults && this.serverless.service.defaults.region)
|| 'us-east-1'
this.custom = this.serverless.service.custom

this.configPlugin()
return this.createWarmer()
}
Expand All @@ -85,9 +102,17 @@ class WarmUP {
* */
afterDeployFunctions () {
this.configPlugin()
let promise = Promise.resolve()

if (this.warmup.cleanFolder) {
richarddd marked this conversation as resolved.
Show resolved Hide resolved
promise = promise.then(() => fs.removeAsync(this.pathArchive))
}

if (this.warmup.prewarm) {
return this.warmUpFunctions()
promise = promise.then(this.warmUpFunctions)
}

return promise
}

/**
Expand All @@ -102,6 +127,7 @@ class WarmUP {
this.folderName = this.custom.warmup.folderName
}
this.pathFolder = this.getPath(this.folderName)
this.pathArchive = this.getPath(artifactPath)
this.pathFile = this.pathFolder + '/index.js'
this.pathHandler = this.folderName + '/index.warmUp'

Expand Down Expand Up @@ -174,6 +200,19 @@ class WarmUP {
return path.join(this.serverless.config.servicePath, file)
}

/**
* @description Create zip artifact
*
* @fulfil {Archiver} — Artifact created
* @reject {Error} File system error
*
* @return {Promise}
*/
createArtifact () {
const src = this.pathFolder + (this.pathFolder.endsWith('/') ? '' : '/')
return this.archiveDirectory(src, this.pathArchive, 'zip', { zlib: { level: 9 } }, this.folderName)
}

/**
* @description Clean prefix folder
*
Expand Down Expand Up @@ -265,6 +304,8 @@ module.exports.warmUp = (event, context, callback) => {
console.log("Warm Up Start");
functionNames.forEach((functionName) => {
const params = {
// base64 encoded: {"custom":{"warmupEvent":"1"}}
ClientContext: "eyJjdXN0b20iOnsid2FybXVwRXZlbnQiOiIxIn19",
FunctionName: functionName,
InvocationType: "RequestResponse",
LogType: "None",
Expand All @@ -286,6 +327,7 @@ module.exports.warmUp = (event, context, callback) => {

/** Write warm up file */
return fs.outputFileAsync(this.pathFile, warmUpFunction)
.then(res => this.createArtifact().then(() => res))
}

/**
Expand All @@ -305,7 +347,8 @@ module.exports.warmUp = (event, context, callback) => {
package: {
individually: true,
exclude: ['**'],
include: [this.folderName + '/**']
include: [this.folderName + '/**'],
artifact: artifactPath
},
timeout: this.warmup.timeout
}
Expand Down