Skip to content

Commit 6b64c74

Browse files
author
Maciej Wilczyński
committed
Implement requirements layer caching
1 parent ea38234 commit 6b64c74

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

lib/layer.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fse = require('fs-extra');
33
const path = require('path');
44
const JSZip = require('jszip');
55
const { writeZip, addTree } = require('./zipTree');
6+
const {sha256Path, getRequirementsLayerPath} = require("./shared");
67

78
BbPromise.promisifyAll(fse);
89

@@ -11,13 +12,33 @@ BbPromise.promisifyAll(fse);
1112
* @return {Promise} the JSZip object constructed.
1213
*/
1314
function zipRequirements() {
14-
const rootZip = new JSZip();
1515
const src = path.join('.serverless', 'requirements');
16-
const runtimepath = 'python';
17-
18-
return addTree(rootZip.folder(runtimepath), src).then(() =>
19-
writeZip(rootZip, path.join('.serverless', 'pythonRequirements.zip'))
16+
const reqChecksum = sha256Path(path.join('.serverless', 'requirements.txt'));
17+
const targetZipPath = path.join('.serverless', 'pythonRequirements.zip');
18+
const zipCachePath = getRequirementsLayerPath(
19+
reqChecksum,
20+
targetZipPath,
21+
this.options
2022
);
23+
24+
const promises = [];
25+
if (fse.existsSync(zipCachePath)) {
26+
this.serverless.cli.log("Found cached Python Requirements Lambda Layer file");
27+
} else {
28+
const rootZip = new JSZip();
29+
const runtimepath = 'python';
30+
31+
promises.push(
32+
addTree(rootZip.folder(runtimepath), src).then(() =>
33+
writeZip(rootZip, zipCachePath)
34+
)
35+
);
36+
}
37+
return BbPromise.all(promises).then(() => {
38+
if (zipCachePath !== targetZipPath) {
39+
fse.symlink(zipCachePath, targetZipPath, 'file')
40+
}
41+
});
2142
}
2243

2344
/**

lib/shared.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ function getRequirementsWorkingPath(
7676
return path.join(requirementsTxtDirectory, 'requirements');
7777
}
7878

79+
/**
80+
* Path of a cached requirements layer archive file
81+
* @param {string} subfolder
82+
* @param {string} fallback
83+
* @param {Object} options
84+
* @return {string}
85+
*/
86+
function getRequirementsLayerPath(
87+
hash,
88+
fallback,
89+
options
90+
) {
91+
// If we want to use the static cache
92+
if (hash && options && options.useStaticCache) {
93+
hash = hash + '_slspyc.zip';
94+
return path.join(getUserCachePath(options), hash);
95+
}
96+
97+
// If we don't want to use the static cache, then fallback to requirements file in .serverless directory
98+
return fallback;
99+
}
100+
79101
/**
80102
* The static cache path that will be used for this system + options, used if static cache is enabled
81103
* @param {Object} options
@@ -107,6 +129,7 @@ function sha256Path(fullpath) {
107129
module.exports = {
108130
checkForAndDeleteMaxCacheVersions,
109131
getRequirementsWorkingPath,
132+
getRequirementsLayerPath,
110133
getUserCachePath,
111134
sha256Path,
112135
};

0 commit comments

Comments
 (0)