serverless-plugin-warmup 7.0.1 → 7.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitattributes +1 -0
- package/README.md +5 -1
- package/package.json +1 -1
- package/src/config.js +2 -0
- package/src/index.js +6 -5
- package/src/schema.js +1 -0
- package/src/warmer.js +14 -10
package/.gitattributes
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* text=auto eol=lf
|
package/README.md
CHANGED
|
@@ -44,6 +44,7 @@ custom:
|
|
|
44
44
|
events:
|
|
45
45
|
- schedule: cron(0/5 8-17 ? * MON-FRI *)
|
|
46
46
|
concurrency: 10
|
|
47
|
+
verbose: true
|
|
47
48
|
logRetentionInDays: 10
|
|
48
49
|
outOfOfficeHoursWarmer:
|
|
49
50
|
enabled: true
|
|
@@ -52,6 +53,7 @@ custom:
|
|
|
52
53
|
- schedule: cron(0/5 18-23 ? * MON-FRI *)
|
|
53
54
|
- schedule: cron(0/5 * ? * SAT-SUN *)
|
|
54
55
|
concurrency: 1
|
|
56
|
+
verbose: false
|
|
55
57
|
testWarmer:
|
|
56
58
|
enabled: false
|
|
57
59
|
```
|
|
@@ -70,6 +72,7 @@ The options are the same for all the warmers:
|
|
|
70
72
|
* **timeout** How many seconds until the warmer lambda times out. (defaults to `10`)
|
|
71
73
|
* **environment** Can be used to set environment variables in the warmer lambda. You can also unset variables configured at the provider by setting them to undefined. However, you should almost never have to change the default. (defaults to unset all package level environment variables. )
|
|
72
74
|
* **tracing** Specify whether to enable/disable tracing at the function level. When tracing is enabled, warmer functions will use NPM to install the X-Ray client and use it to trace requests (It takes any of the values supported by serverless as `boolean`, `Active`or `PassThrough` and defaults to the provider-level setting)
|
|
75
|
+
* **verbose** If set to false, it disables the console.logs placed on this warmer lambda (defaults to `true`)
|
|
73
76
|
* **logRetentionInDays** Set the retention time in days for the log group associated to this warmer lamba
|
|
74
77
|
* **prewarm** If set to true, it warms up your lambdas right after deploying (defaults to `false`)
|
|
75
78
|
|
|
@@ -106,6 +109,7 @@ custom:
|
|
|
106
109
|
- ./**
|
|
107
110
|
timeout: 20
|
|
108
111
|
tracing: true
|
|
112
|
+
verbose: false # Disable the logs
|
|
109
113
|
logRetentionInDays: 10
|
|
110
114
|
prewarm: true # Run WarmUp immediately after a deploymentlambda
|
|
111
115
|
clientContext:
|
|
@@ -251,7 +255,7 @@ The permissions can also be added to all lambdas using setting the role to `IamR
|
|
|
251
255
|
```yaml
|
|
252
256
|
provider:
|
|
253
257
|
name: aws
|
|
254
|
-
runtime:
|
|
258
|
+
runtime: nodejs16.x
|
|
255
259
|
iamRoleStatements:
|
|
256
260
|
- Effect: 'Allow'
|
|
257
261
|
Action:
|
package/package.json
CHANGED
package/src/config.js
CHANGED
|
@@ -41,6 +41,7 @@ function getWarmerConfig(config, defaultOpts) {
|
|
|
41
41
|
? config.environment
|
|
42
42
|
: defaultOpts.environment,
|
|
43
43
|
tracing: (config.tracing !== undefined) ? config.tracing : defaultOpts.tracing,
|
|
44
|
+
verbose: (config.verbose !== undefined) ? config.verbose : defaultOpts.verbose,
|
|
44
45
|
logRetentionInDays: (config.logRetentionInDays !== undefined)
|
|
45
46
|
? config.logRetentionInDays
|
|
46
47
|
: defaultOpts.logRetentionInDays,
|
|
@@ -151,6 +152,7 @@ function getConfigsByWarmer({ service, classes }, stage) {
|
|
|
151
152
|
timeout: 10,
|
|
152
153
|
environment: Object.keys(service.provider.environment || [])
|
|
153
154
|
.reduce((obj, k) => ({ ...obj, [k]: undefined }), {}),
|
|
155
|
+
verbose: true,
|
|
154
156
|
prewarm: false,
|
|
155
157
|
});
|
|
156
158
|
|
package/src/index.js
CHANGED
|
@@ -112,11 +112,11 @@ class WarmUp {
|
|
|
112
112
|
* @return {Promise}
|
|
113
113
|
* */
|
|
114
114
|
async resetWarmerConfigs() {
|
|
115
|
-
|
|
116
|
-
.
|
|
117
|
-
|
|
118
|
-
warmerConfig
|
|
119
|
-
|
|
115
|
+
Object.entries(this.configsByWarmer)
|
|
116
|
+
.forEach(([warmerName, warmerConfig]) => {
|
|
117
|
+
if (warmerConfig.functions.length === 0) return;
|
|
118
|
+
addWarmUpFunctionToService(this.serverless.service, warmerName, warmerConfig);
|
|
119
|
+
});
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
/**
|
|
@@ -204,6 +204,7 @@ class WarmUp {
|
|
|
204
204
|
await createWarmUpFunctionArtifact(
|
|
205
205
|
warmerConfig.functions,
|
|
206
206
|
warmerConfig.tracing,
|
|
207
|
+
warmerConfig.verbose,
|
|
207
208
|
this.provider.getRegion(),
|
|
208
209
|
handlerFolder,
|
|
209
210
|
);
|
package/src/schema.js
CHANGED
|
@@ -107,6 +107,7 @@ function extendServerlessSchema(serverless) {
|
|
|
107
107
|
timeout: { $ref: '#/definitions/awsLambdaTimeout' },
|
|
108
108
|
environment: { $ref: '#/definitions/awsLambdaEnvironment' },
|
|
109
109
|
tracing: { $ref: '#/definitions/awsLambdaTracing' },
|
|
110
|
+
verbose: { type: 'boolean' },
|
|
110
111
|
logRetentionInDays: {
|
|
111
112
|
type: 'number',
|
|
112
113
|
enum: [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653],
|
package/src/warmer.js
CHANGED
|
@@ -94,7 +94,7 @@ function addWarmUpFunctionRoleToResources(service, stage, warmerName, warmerConf
|
|
|
94
94
|
'lambda:InvokeFunction',
|
|
95
95
|
],
|
|
96
96
|
Resource: warmerConfig.functions.map((fn) => ({
|
|
97
|
-
'Fn::Sub': `arn:\${AWS::Partition}:lambda:\${AWS::Region}:\${AWS::AccountId}:function:${fn.name}
|
|
97
|
+
'Fn::Sub': `arn:\${AWS::Partition}:lambda:\${AWS::Region}:\${AWS::AccountId}:function:${fn.name}*`,
|
|
98
98
|
})),
|
|
99
99
|
},
|
|
100
100
|
{
|
|
@@ -125,7 +125,7 @@ function addWarmUpFunctionRoleToResources(service, stage, warmerName, warmerConf
|
|
|
125
125
|
*
|
|
126
126
|
* @return {Promise}
|
|
127
127
|
* */
|
|
128
|
-
async function createWarmUpFunctionArtifact(functions, tracing, region, handlerFolder) {
|
|
128
|
+
async function createWarmUpFunctionArtifact(functions, tracing, verbose, region, handlerFolder) {
|
|
129
129
|
const warmUpFunction = `'use strict';
|
|
130
130
|
|
|
131
131
|
/** Generated by Serverless WarmUp Plugin **/
|
|
@@ -143,28 +143,32 @@ const lambda = new AWS.Lambda({
|
|
|
143
143
|
});
|
|
144
144
|
const functions = ${JSON.stringify(functions, null, ' ')};
|
|
145
145
|
|
|
146
|
+
function logVerbose(str) {
|
|
147
|
+
${verbose ? 'console.log(str);' : ''}
|
|
148
|
+
}
|
|
149
|
+
|
|
146
150
|
function getConcurrency(func, envVars) {
|
|
147
151
|
const functionConcurrency = envVars[\`WARMUP_CONCURRENCY_\${func.name.toUpperCase().replace(/-/g, '_')}\`];
|
|
148
152
|
|
|
149
153
|
if (functionConcurrency) {
|
|
150
154
|
const concurrency = parseInt(functionConcurrency);
|
|
151
|
-
|
|
155
|
+
logVerbose(\`Warming up function: \${func.name} with concurrency: \${concurrency} (from function-specific environment variable)\`);
|
|
152
156
|
return concurrency;
|
|
153
157
|
}
|
|
154
158
|
|
|
155
159
|
if (envVars.WARMUP_CONCURRENCY) {
|
|
156
160
|
const concurrency = parseInt(envVars.WARMUP_CONCURRENCY);
|
|
157
|
-
|
|
161
|
+
logVerbose(\`Warming up function: \${func.name} with concurrency: \${concurrency} (from global environment variable)\`);
|
|
158
162
|
return concurrency;
|
|
159
163
|
}
|
|
160
164
|
|
|
161
165
|
const concurrency = parseInt(func.config.concurrency);
|
|
162
|
-
|
|
166
|
+
logVerbose(\`Warming up function: \${func.name} with concurrency: \${concurrency}\`);
|
|
163
167
|
return concurrency;
|
|
164
168
|
}
|
|
165
169
|
|
|
166
170
|
module.exports.warmUp = async (event, context) => {
|
|
167
|
-
|
|
171
|
+
logVerbose('Warm Up Start');
|
|
168
172
|
|
|
169
173
|
const invokes = await Promise.all(functions.map(async (func) => {
|
|
170
174
|
const concurrency = getConcurrency(func, process.env);
|
|
@@ -186,15 +190,15 @@ module.exports.warmUp = async (event, context) => {
|
|
|
186
190
|
|
|
187
191
|
try {
|
|
188
192
|
await Promise.all(Array(concurrency).fill(0).map(async () => await lambda.invoke(params).promise()));
|
|
189
|
-
|
|
193
|
+
logVerbose(\`Warm Up Invoke Success: \${func.name}\`);
|
|
190
194
|
return true;
|
|
191
195
|
} catch (e) {
|
|
192
|
-
console.
|
|
196
|
+
console.error(\`Warm Up Invoke Error: \${func.name}\`, e);
|
|
193
197
|
return false;
|
|
194
198
|
}
|
|
195
199
|
}));
|
|
196
200
|
|
|
197
|
-
|
|
201
|
+
logVerbose(\`Warm Up Finished with \${invokes.filter(r => !r).length} invoke errors\`);
|
|
198
202
|
}`;
|
|
199
203
|
|
|
200
204
|
/** Write warm up file */
|
|
@@ -218,7 +222,7 @@ function addWarmUpFunctionToService(service, warmerName, warmerConfig) {
|
|
|
218
222
|
handler: warmerConfig.pathHandler.split(path.sep).join(path.posix.sep),
|
|
219
223
|
memorySize: warmerConfig.memorySize,
|
|
220
224
|
name: warmerConfig.name,
|
|
221
|
-
runtime: '
|
|
225
|
+
runtime: 'nodejs16.x',
|
|
222
226
|
package: warmerConfig.package,
|
|
223
227
|
timeout: warmerConfig.timeout,
|
|
224
228
|
...(Object.keys(warmerConfig.environment).length
|