Skip to content

feat: Support requirements layer caching #644

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

Merged
merged 9 commits into from
Dec 20, 2021
Merged
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
47 changes: 42 additions & 5 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const fse = require('fs-extra');
const path = require('path');
const JSZip = require('jszip');
const { writeZip, addTree } = require('./zipTree');
const { sha256Path, getRequirementsLayerPath } = require('./shared');

BbPromise.promisifyAll(fse);

Expand All @@ -11,13 +12,49 @@ BbPromise.promisifyAll(fse);
* @return {Promise} the JSZip object constructed.
*/
function zipRequirements() {
const rootZip = new JSZip();
const src = path.join('.serverless', 'requirements');
const runtimepath = 'python';

return addTree(rootZip.folder(runtimepath), src).then(() =>
writeZip(rootZip, path.join('.serverless', 'pythonRequirements.zip'))
const reqChecksum = sha256Path(path.join('.serverless', 'requirements.txt'));
const targetZipPath = path.join('.serverless', 'pythonRequirements.zip');
const zipCachePath = getRequirementsLayerPath(
reqChecksum,
targetZipPath,
this.options,
this.serverless
);

const promises = [];
if (fse.existsSync(zipCachePath)) {
let layerProgress;
if (this.progress && this.log) {
layerProgress = this.progress.get('python-layer-requirements');
layerProgress.update(
'Using cached Python Requirements Lambda Layer file'
);
this.log.info('Found cached Python Requirements Lambda Layer file');
} else {
this.serverless.cli.log(
'Found cached Python Requirements Lambda Layer file'
);
}
} else {
const rootZip = new JSZip();
const runtimepath = 'python';

promises.push(
addTree(rootZip.folder(runtimepath), src).then(() =>
writeZip(rootZip, zipCachePath)
)
);
}
return BbPromise.all(promises).then(() => {
if (zipCachePath !== targetZipPath) {
if (process.platform === 'win32') {
fse.copySync(zipCachePath, targetZipPath);
} else {
fse.symlink(zipCachePath, targetZipPath, 'file');
}
}
});
}

/**
Expand Down
21 changes: 21 additions & 0 deletions lib/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ function getRequirementsWorkingPath(
return path.join(requirementsTxtDirectory, 'requirements');
}

/**
* Path of a cached requirements layer archive file
* @param {string} subfolder
* @param {string} fallback
* @param {Object} options
* @param {Object} serverless
* @return {string}
*/
function getRequirementsLayerPath(hash, fallback, options, serverless) {
// If we want to use the static cache
if (hash && options && options.useStaticCache) {
const architecture = serverless.service.provider.architecture || 'x86_64';
hash = `${hash}_${architecture}_slspyc.zip`;
return path.join(getUserCachePath(options), hash);
}

// If we don't want to use the static cache, then fallback to requirements file in .serverless directory
return fallback;
}

/**
* The static cache path that will be used for this system + options, used if static cache is enabled
* @param {Object} options
Expand Down Expand Up @@ -117,6 +137,7 @@ function sha256Path(fullpath) {
module.exports = {
checkForAndDeleteMaxCacheVersions,
getRequirementsWorkingPath,
getRequirementsLayerPath,
getUserCachePath,
sha256Path,
};