Skip to content

Commit c23977f

Browse files
author
Maciej Wilczyński
committed
Merge branch 'layer-zip-cache' into multiarch_with_layer_caching
* layer-zip-cache: Copy instead of linking on Windows Implement requirements layer caching
2 parents 29e8d4e + 40c81d6 commit c23977f

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

lib/layer.js

Lines changed: 30 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,37 @@ 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+
if (process.platform === 'win32') {
40+
fse.copySync(zipCachePath, targetZipPath);
41+
} else {
42+
fse.symlink(zipCachePath, targetZipPath, 'file')
43+
}
44+
}
45+
});
2146
}
2247

2348
/**

lib/shared.js

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

82+
/**
83+
* Path of a cached requirements layer archive file
84+
* @param {string} subfolder
85+
* @param {string} fallback
86+
* @param {Object} options
87+
* @return {string}
88+
*/
89+
function getRequirementsLayerPath(
90+
hash,
91+
fallback,
92+
options
93+
) {
94+
// If we want to use the static cache
95+
if (hash && options && options.useStaticCache) {
96+
hash = hash + '_slspyc.zip';
97+
return path.join(getUserCachePath(options), hash);
98+
}
99+
100+
// If we don't want to use the static cache, then fallback to requirements file in .serverless directory
101+
return fallback;
102+
}
103+
82104
/**
83105
* The static cache path that will be used for this system + options, used if static cache is enabled
84106
* @param {Object} options
@@ -110,6 +132,7 @@ function sha256Path(fullpath) {
110132
module.exports = {
111133
checkForAndDeleteMaxCacheVersions,
112134
getRequirementsWorkingPath,
135+
getRequirementsLayerPath,
113136
getUserCachePath,
114137
sha256Path,
115138
};

0 commit comments

Comments
 (0)