@tramvai/module-cache-warmup 2.70.1 → 2.72.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/lib/server.es.js +1 -115
- package/lib/server.js +2 -121
- package/lib/utils.es.js +75 -0
- package/lib/utils.js +86 -0
- package/lib/warmup.es.js +45 -0
- package/lib/warmup.js +49 -0
- package/package.json +8 -9
package/lib/server.es.js
CHANGED
|
@@ -2,121 +2,7 @@ import { __decorate } from 'tslib';
|
|
|
2
2
|
import { Module, provide, commandLineListTokens } from '@tramvai/core';
|
|
3
3
|
import { PAPI_SERVICE } from '@tramvai/tokens-http-client';
|
|
4
4
|
import { LOGGER_TOKEN, ENV_MANAGER_TOKEN } from '@tramvai/module-common';
|
|
5
|
-
import {
|
|
6
|
-
import requestFactory from '@tinkoff/request-core';
|
|
7
|
-
import http from '@tinkoff/request-plugin-protocol-http';
|
|
8
|
-
|
|
9
|
-
const request = requestFactory([http()]);
|
|
10
|
-
async function queueRequests(options) {
|
|
11
|
-
const { maxSimultaneous = 2, makeRequest, requestsOptions } = options;
|
|
12
|
-
const req = async (option) => {
|
|
13
|
-
try {
|
|
14
|
-
await makeRequest(option);
|
|
15
|
-
return {
|
|
16
|
-
option,
|
|
17
|
-
result: 'resolved',
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
return {
|
|
22
|
-
option,
|
|
23
|
-
result: 'rejected',
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
const queue = [];
|
|
28
|
-
const result = [];
|
|
29
|
-
for (const option of requestsOptions) {
|
|
30
|
-
const promise = req(option);
|
|
31
|
-
result.push(promise);
|
|
32
|
-
const queuedPromise = promise.then(() => {
|
|
33
|
-
const index = queue.indexOf(queuedPromise);
|
|
34
|
-
return queue.splice(index, 1);
|
|
35
|
-
});
|
|
36
|
-
queue.push(queuedPromise);
|
|
37
|
-
if (queue.length >= maxSimultaneous) {
|
|
38
|
-
// eslint-disable-next-line no-await-in-loop
|
|
39
|
-
await Promise.race(queue);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return Promise.all(result);
|
|
43
|
-
}
|
|
44
|
-
function createRequestsOptions(options) {
|
|
45
|
-
const { urls, port } = options;
|
|
46
|
-
return urls.reduce((requestOptions, url) => {
|
|
47
|
-
const ip = generateRandomIPv4Adress();
|
|
48
|
-
requestOptions.push(...options.userAgents.map((userAgent) => ({
|
|
49
|
-
url: format({
|
|
50
|
-
hostname: 'localhost',
|
|
51
|
-
port,
|
|
52
|
-
path: url.replace(/:\w+/g, '0'),
|
|
53
|
-
}),
|
|
54
|
-
headers: {
|
|
55
|
-
'User-Agent': userAgent,
|
|
56
|
-
'X-Real-IP': ip,
|
|
57
|
-
},
|
|
58
|
-
})));
|
|
59
|
-
return requestOptions;
|
|
60
|
-
}, []);
|
|
61
|
-
}
|
|
62
|
-
function generateRandomIPv4Adress() {
|
|
63
|
-
const from = 0;
|
|
64
|
-
const to = 255;
|
|
65
|
-
const length = 4;
|
|
66
|
-
const bytes = [];
|
|
67
|
-
for (let i = 0; i < length; i++) {
|
|
68
|
-
bytes.push(generateRandomNumberFromRange(from, to));
|
|
69
|
-
}
|
|
70
|
-
return bytes.join('.');
|
|
71
|
-
}
|
|
72
|
-
function generateRandomNumberFromRange(min, max) {
|
|
73
|
-
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
74
|
-
}
|
|
75
|
-
function sendWarmUpRequest(options) {
|
|
76
|
-
return request(options);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const userAgents = [
|
|
80
|
-
/** Chrome on Mac OS */
|
|
81
|
-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36',
|
|
82
|
-
/** Chrome on Mobile */
|
|
83
|
-
'Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36',
|
|
84
|
-
];
|
|
85
|
-
async function warmUpCache(options) {
|
|
86
|
-
const { papiService, logger, environmentManager } = options;
|
|
87
|
-
const log = logger('cache-warmup');
|
|
88
|
-
const startTimestamp = Date.now();
|
|
89
|
-
log.info("Cache warmup process 'START'");
|
|
90
|
-
try {
|
|
91
|
-
const { payload: urls } = await papiService.request({
|
|
92
|
-
path: 'bundleInfo',
|
|
93
|
-
});
|
|
94
|
-
const requestsOptions = createRequestsOptions({
|
|
95
|
-
urls,
|
|
96
|
-
port: environmentManager.get('PORT'),
|
|
97
|
-
userAgents,
|
|
98
|
-
});
|
|
99
|
-
const results = await queueRequests({
|
|
100
|
-
makeRequest: sendWarmUpRequest,
|
|
101
|
-
requestsOptions,
|
|
102
|
-
maxSimultaneous: 1,
|
|
103
|
-
});
|
|
104
|
-
const failed = results.filter((result) => result.result === 'rejected');
|
|
105
|
-
if (failed.length) {
|
|
106
|
-
log.info(`Cache warmup process 'FINISHED' with errors, failed URLs:\n${failed
|
|
107
|
-
.map((v) => v.option.path)
|
|
108
|
-
.join('\n')}`);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
log.info("Cache warmup process 'SUCCESS'");
|
|
112
|
-
}
|
|
113
|
-
log.info(`Cache warmup made ${results.length} requests for ${urls.length} URLs`);
|
|
114
|
-
log.info(`Cache warmup took - ${Date.now() - startTimestamp}ms`);
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
log.error(error, "Cache warmup process 'FAILURE'");
|
|
118
|
-
}
|
|
119
|
-
}
|
|
5
|
+
import { warmUpCache } from './warmup.es.js';
|
|
120
6
|
|
|
121
7
|
let CacheWarmupModule = class CacheWarmupModule {
|
|
122
8
|
};
|
package/lib/server.js
CHANGED
|
@@ -6,126 +6,7 @@ var tslib = require('tslib');
|
|
|
6
6
|
var core = require('@tramvai/core');
|
|
7
7
|
var tokensHttpClient = require('@tramvai/tokens-http-client');
|
|
8
8
|
var moduleCommon = require('@tramvai/module-common');
|
|
9
|
-
var
|
|
10
|
-
var requestFactory = require('@tinkoff/request-core');
|
|
11
|
-
var http = require('@tinkoff/request-plugin-protocol-http');
|
|
12
|
-
|
|
13
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
|
-
|
|
15
|
-
var requestFactory__default = /*#__PURE__*/_interopDefaultLegacy(requestFactory);
|
|
16
|
-
var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
|
|
17
|
-
|
|
18
|
-
const request = requestFactory__default["default"]([http__default["default"]()]);
|
|
19
|
-
async function queueRequests(options) {
|
|
20
|
-
const { maxSimultaneous = 2, makeRequest, requestsOptions } = options;
|
|
21
|
-
const req = async (option) => {
|
|
22
|
-
try {
|
|
23
|
-
await makeRequest(option);
|
|
24
|
-
return {
|
|
25
|
-
option,
|
|
26
|
-
result: 'resolved',
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
return {
|
|
31
|
-
option,
|
|
32
|
-
result: 'rejected',
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
const queue = [];
|
|
37
|
-
const result = [];
|
|
38
|
-
for (const option of requestsOptions) {
|
|
39
|
-
const promise = req(option);
|
|
40
|
-
result.push(promise);
|
|
41
|
-
const queuedPromise = promise.then(() => {
|
|
42
|
-
const index = queue.indexOf(queuedPromise);
|
|
43
|
-
return queue.splice(index, 1);
|
|
44
|
-
});
|
|
45
|
-
queue.push(queuedPromise);
|
|
46
|
-
if (queue.length >= maxSimultaneous) {
|
|
47
|
-
// eslint-disable-next-line no-await-in-loop
|
|
48
|
-
await Promise.race(queue);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return Promise.all(result);
|
|
52
|
-
}
|
|
53
|
-
function createRequestsOptions(options) {
|
|
54
|
-
const { urls, port } = options;
|
|
55
|
-
return urls.reduce((requestOptions, url$1) => {
|
|
56
|
-
const ip = generateRandomIPv4Adress();
|
|
57
|
-
requestOptions.push(...options.userAgents.map((userAgent) => ({
|
|
58
|
-
url: url.format({
|
|
59
|
-
hostname: 'localhost',
|
|
60
|
-
port,
|
|
61
|
-
path: url$1.replace(/:\w+/g, '0'),
|
|
62
|
-
}),
|
|
63
|
-
headers: {
|
|
64
|
-
'User-Agent': userAgent,
|
|
65
|
-
'X-Real-IP': ip,
|
|
66
|
-
},
|
|
67
|
-
})));
|
|
68
|
-
return requestOptions;
|
|
69
|
-
}, []);
|
|
70
|
-
}
|
|
71
|
-
function generateRandomIPv4Adress() {
|
|
72
|
-
const from = 0;
|
|
73
|
-
const to = 255;
|
|
74
|
-
const length = 4;
|
|
75
|
-
const bytes = [];
|
|
76
|
-
for (let i = 0; i < length; i++) {
|
|
77
|
-
bytes.push(generateRandomNumberFromRange(from, to));
|
|
78
|
-
}
|
|
79
|
-
return bytes.join('.');
|
|
80
|
-
}
|
|
81
|
-
function generateRandomNumberFromRange(min, max) {
|
|
82
|
-
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
83
|
-
}
|
|
84
|
-
function sendWarmUpRequest(options) {
|
|
85
|
-
return request(options);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const userAgents = [
|
|
89
|
-
/** Chrome on Mac OS */
|
|
90
|
-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36',
|
|
91
|
-
/** Chrome on Mobile */
|
|
92
|
-
'Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36',
|
|
93
|
-
];
|
|
94
|
-
async function warmUpCache(options) {
|
|
95
|
-
const { papiService, logger, environmentManager } = options;
|
|
96
|
-
const log = logger('cache-warmup');
|
|
97
|
-
const startTimestamp = Date.now();
|
|
98
|
-
log.info("Cache warmup process 'START'");
|
|
99
|
-
try {
|
|
100
|
-
const { payload: urls } = await papiService.request({
|
|
101
|
-
path: 'bundleInfo',
|
|
102
|
-
});
|
|
103
|
-
const requestsOptions = createRequestsOptions({
|
|
104
|
-
urls,
|
|
105
|
-
port: environmentManager.get('PORT'),
|
|
106
|
-
userAgents,
|
|
107
|
-
});
|
|
108
|
-
const results = await queueRequests({
|
|
109
|
-
makeRequest: sendWarmUpRequest,
|
|
110
|
-
requestsOptions,
|
|
111
|
-
maxSimultaneous: 1,
|
|
112
|
-
});
|
|
113
|
-
const failed = results.filter((result) => result.result === 'rejected');
|
|
114
|
-
if (failed.length) {
|
|
115
|
-
log.info(`Cache warmup process 'FINISHED' with errors, failed URLs:\n${failed
|
|
116
|
-
.map((v) => v.option.path)
|
|
117
|
-
.join('\n')}`);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
log.info("Cache warmup process 'SUCCESS'");
|
|
121
|
-
}
|
|
122
|
-
log.info(`Cache warmup made ${results.length} requests for ${urls.length} URLs`);
|
|
123
|
-
log.info(`Cache warmup took - ${Date.now() - startTimestamp}ms`);
|
|
124
|
-
}
|
|
125
|
-
catch (error) {
|
|
126
|
-
log.error(error, "Cache warmup process 'FAILURE'");
|
|
127
|
-
}
|
|
128
|
-
}
|
|
9
|
+
var warmup = require('./warmup.js');
|
|
129
10
|
|
|
130
11
|
exports.CacheWarmupModule = class CacheWarmupModule {
|
|
131
12
|
};
|
|
@@ -155,7 +36,7 @@ exports.CacheWarmupModule = tslib.__decorate([
|
|
|
155
36
|
log.info('Skip cache warm up when mocker is enabled');
|
|
156
37
|
return;
|
|
157
38
|
}
|
|
158
|
-
warmUpCache({ papiService, logger, environmentManager });
|
|
39
|
+
warmup.warmUpCache({ papiService, logger, environmentManager });
|
|
159
40
|
};
|
|
160
41
|
},
|
|
161
42
|
deps: {
|
package/lib/utils.es.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { format } from '@tinkoff/url';
|
|
2
|
+
import requestFactory from '@tinkoff/request-core';
|
|
3
|
+
import http from '@tinkoff/request-plugin-protocol-http';
|
|
4
|
+
|
|
5
|
+
const request = requestFactory([http()]);
|
|
6
|
+
async function queueRequests(options) {
|
|
7
|
+
const { maxSimultaneous = 2, makeRequest, requestsOptions } = options;
|
|
8
|
+
const req = async (option) => {
|
|
9
|
+
try {
|
|
10
|
+
await makeRequest(option);
|
|
11
|
+
return {
|
|
12
|
+
option,
|
|
13
|
+
result: 'resolved',
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return {
|
|
18
|
+
option,
|
|
19
|
+
result: 'rejected',
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
const queue = [];
|
|
24
|
+
const result = [];
|
|
25
|
+
for (const option of requestsOptions) {
|
|
26
|
+
const promise = req(option);
|
|
27
|
+
result.push(promise);
|
|
28
|
+
const queuedPromise = promise.then(() => {
|
|
29
|
+
const index = queue.indexOf(queuedPromise);
|
|
30
|
+
return queue.splice(index, 1);
|
|
31
|
+
});
|
|
32
|
+
queue.push(queuedPromise);
|
|
33
|
+
if (queue.length >= maxSimultaneous) {
|
|
34
|
+
// eslint-disable-next-line no-await-in-loop
|
|
35
|
+
await Promise.race(queue);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return Promise.all(result);
|
|
39
|
+
}
|
|
40
|
+
function createRequestsOptions(options) {
|
|
41
|
+
const { urls, port } = options;
|
|
42
|
+
return urls.reduce((requestOptions, url) => {
|
|
43
|
+
const ip = generateRandomIPv4Adress();
|
|
44
|
+
requestOptions.push(...options.userAgents.map((userAgent) => ({
|
|
45
|
+
url: format({
|
|
46
|
+
hostname: 'localhost',
|
|
47
|
+
port,
|
|
48
|
+
path: url.replace(/:\w+/g, '0'),
|
|
49
|
+
}),
|
|
50
|
+
headers: {
|
|
51
|
+
'User-Agent': userAgent,
|
|
52
|
+
'X-Real-IP': ip,
|
|
53
|
+
},
|
|
54
|
+
})));
|
|
55
|
+
return requestOptions;
|
|
56
|
+
}, []);
|
|
57
|
+
}
|
|
58
|
+
function generateRandomIPv4Adress() {
|
|
59
|
+
const from = 0;
|
|
60
|
+
const to = 255;
|
|
61
|
+
const length = 4;
|
|
62
|
+
const bytes = [];
|
|
63
|
+
for (let i = 0; i < length; i++) {
|
|
64
|
+
bytes.push(generateRandomNumberFromRange(from, to));
|
|
65
|
+
}
|
|
66
|
+
return bytes.join('.');
|
|
67
|
+
}
|
|
68
|
+
function generateRandomNumberFromRange(min, max) {
|
|
69
|
+
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
70
|
+
}
|
|
71
|
+
function sendWarmUpRequest(options) {
|
|
72
|
+
return request(options);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { createRequestsOptions, queueRequests, sendWarmUpRequest };
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var url = require('@tinkoff/url');
|
|
6
|
+
var requestFactory = require('@tinkoff/request-core');
|
|
7
|
+
var http = require('@tinkoff/request-plugin-protocol-http');
|
|
8
|
+
|
|
9
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
10
|
+
|
|
11
|
+
var requestFactory__default = /*#__PURE__*/_interopDefaultLegacy(requestFactory);
|
|
12
|
+
var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
|
|
13
|
+
|
|
14
|
+
const request = requestFactory__default["default"]([http__default["default"]()]);
|
|
15
|
+
async function queueRequests(options) {
|
|
16
|
+
const { maxSimultaneous = 2, makeRequest, requestsOptions } = options;
|
|
17
|
+
const req = async (option) => {
|
|
18
|
+
try {
|
|
19
|
+
await makeRequest(option);
|
|
20
|
+
return {
|
|
21
|
+
option,
|
|
22
|
+
result: 'resolved',
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return {
|
|
27
|
+
option,
|
|
28
|
+
result: 'rejected',
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
const queue = [];
|
|
33
|
+
const result = [];
|
|
34
|
+
for (const option of requestsOptions) {
|
|
35
|
+
const promise = req(option);
|
|
36
|
+
result.push(promise);
|
|
37
|
+
const queuedPromise = promise.then(() => {
|
|
38
|
+
const index = queue.indexOf(queuedPromise);
|
|
39
|
+
return queue.splice(index, 1);
|
|
40
|
+
});
|
|
41
|
+
queue.push(queuedPromise);
|
|
42
|
+
if (queue.length >= maxSimultaneous) {
|
|
43
|
+
// eslint-disable-next-line no-await-in-loop
|
|
44
|
+
await Promise.race(queue);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return Promise.all(result);
|
|
48
|
+
}
|
|
49
|
+
function createRequestsOptions(options) {
|
|
50
|
+
const { urls, port } = options;
|
|
51
|
+
return urls.reduce((requestOptions, url$1) => {
|
|
52
|
+
const ip = generateRandomIPv4Adress();
|
|
53
|
+
requestOptions.push(...options.userAgents.map((userAgent) => ({
|
|
54
|
+
url: url.format({
|
|
55
|
+
hostname: 'localhost',
|
|
56
|
+
port,
|
|
57
|
+
path: url$1.replace(/:\w+/g, '0'),
|
|
58
|
+
}),
|
|
59
|
+
headers: {
|
|
60
|
+
'User-Agent': userAgent,
|
|
61
|
+
'X-Real-IP': ip,
|
|
62
|
+
},
|
|
63
|
+
})));
|
|
64
|
+
return requestOptions;
|
|
65
|
+
}, []);
|
|
66
|
+
}
|
|
67
|
+
function generateRandomIPv4Adress() {
|
|
68
|
+
const from = 0;
|
|
69
|
+
const to = 255;
|
|
70
|
+
const length = 4;
|
|
71
|
+
const bytes = [];
|
|
72
|
+
for (let i = 0; i < length; i++) {
|
|
73
|
+
bytes.push(generateRandomNumberFromRange(from, to));
|
|
74
|
+
}
|
|
75
|
+
return bytes.join('.');
|
|
76
|
+
}
|
|
77
|
+
function generateRandomNumberFromRange(min, max) {
|
|
78
|
+
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
79
|
+
}
|
|
80
|
+
function sendWarmUpRequest(options) {
|
|
81
|
+
return request(options);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
exports.createRequestsOptions = createRequestsOptions;
|
|
85
|
+
exports.queueRequests = queueRequests;
|
|
86
|
+
exports.sendWarmUpRequest = sendWarmUpRequest;
|
package/lib/warmup.es.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { createRequestsOptions, queueRequests, sendWarmUpRequest } from './utils.es.js';
|
|
2
|
+
|
|
3
|
+
const userAgents = [
|
|
4
|
+
/** Chrome on Mac OS */
|
|
5
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36',
|
|
6
|
+
/** Chrome on Mobile */
|
|
7
|
+
'Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36',
|
|
8
|
+
];
|
|
9
|
+
async function warmUpCache(options) {
|
|
10
|
+
const { papiService, logger, environmentManager } = options;
|
|
11
|
+
const log = logger('cache-warmup');
|
|
12
|
+
const startTimestamp = Date.now();
|
|
13
|
+
log.info("Cache warmup process 'START'");
|
|
14
|
+
try {
|
|
15
|
+
const { payload: urls } = await papiService.request({
|
|
16
|
+
path: 'bundleInfo',
|
|
17
|
+
});
|
|
18
|
+
const requestsOptions = createRequestsOptions({
|
|
19
|
+
urls,
|
|
20
|
+
port: environmentManager.get('PORT'),
|
|
21
|
+
userAgents,
|
|
22
|
+
});
|
|
23
|
+
const results = await queueRequests({
|
|
24
|
+
makeRequest: sendWarmUpRequest,
|
|
25
|
+
requestsOptions,
|
|
26
|
+
maxSimultaneous: 1,
|
|
27
|
+
});
|
|
28
|
+
const failed = results.filter((result) => result.result === 'rejected');
|
|
29
|
+
if (failed.length) {
|
|
30
|
+
log.info(`Cache warmup process 'FINISHED' with errors, failed URLs:\n${failed
|
|
31
|
+
.map((v) => v.option.path)
|
|
32
|
+
.join('\n')}`);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
log.info("Cache warmup process 'SUCCESS'");
|
|
36
|
+
}
|
|
37
|
+
log.info(`Cache warmup made ${results.length} requests for ${urls.length} URLs`);
|
|
38
|
+
log.info(`Cache warmup took - ${Date.now() - startTimestamp}ms`);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
log.error(error, "Cache warmup process 'FAILURE'");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { warmUpCache };
|
package/lib/warmup.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var utils = require('./utils.js');
|
|
6
|
+
|
|
7
|
+
const userAgents = [
|
|
8
|
+
/** Chrome on Mac OS */
|
|
9
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36',
|
|
10
|
+
/** Chrome on Mobile */
|
|
11
|
+
'Mozilla/5.0 (Linux; Android 7.0; SM-G930V Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.125 Mobile Safari/537.36',
|
|
12
|
+
];
|
|
13
|
+
async function warmUpCache(options) {
|
|
14
|
+
const { papiService, logger, environmentManager } = options;
|
|
15
|
+
const log = logger('cache-warmup');
|
|
16
|
+
const startTimestamp = Date.now();
|
|
17
|
+
log.info("Cache warmup process 'START'");
|
|
18
|
+
try {
|
|
19
|
+
const { payload: urls } = await papiService.request({
|
|
20
|
+
path: 'bundleInfo',
|
|
21
|
+
});
|
|
22
|
+
const requestsOptions = utils.createRequestsOptions({
|
|
23
|
+
urls,
|
|
24
|
+
port: environmentManager.get('PORT'),
|
|
25
|
+
userAgents,
|
|
26
|
+
});
|
|
27
|
+
const results = await utils.queueRequests({
|
|
28
|
+
makeRequest: utils.sendWarmUpRequest,
|
|
29
|
+
requestsOptions,
|
|
30
|
+
maxSimultaneous: 1,
|
|
31
|
+
});
|
|
32
|
+
const failed = results.filter((result) => result.result === 'rejected');
|
|
33
|
+
if (failed.length) {
|
|
34
|
+
log.info(`Cache warmup process 'FINISHED' with errors, failed URLs:\n${failed
|
|
35
|
+
.map((v) => v.option.path)
|
|
36
|
+
.join('\n')}`);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
log.info("Cache warmup process 'SUCCESS'");
|
|
40
|
+
}
|
|
41
|
+
log.info(`Cache warmup made ${results.length} requests for ${urls.length} URLs`);
|
|
42
|
+
log.info(`Cache warmup took - ${Date.now() - startTimestamp}ms`);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
log.error(error, "Cache warmup process 'FAILURE'");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
exports.warmUpCache = warmUpCache;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tramvai/module-cache-warmup",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.72.0",
|
|
4
4
|
"description": "tramvai cache warmup module",
|
|
5
5
|
"browser": "lib/browser.js",
|
|
6
6
|
"main": "lib/server.js",
|
|
@@ -15,20 +15,19 @@
|
|
|
15
15
|
"directory": "packages/modules/cache-warmup"
|
|
16
16
|
},
|
|
17
17
|
"scripts": {
|
|
18
|
-
"build": "tramvai-build --
|
|
19
|
-
"watch": "tsc -w"
|
|
20
|
-
"build-for-publish": "true"
|
|
18
|
+
"build": "tramvai-build --forPublish --preserveModules",
|
|
19
|
+
"watch": "tsc -w"
|
|
21
20
|
},
|
|
22
21
|
"dependencies": {
|
|
23
22
|
"@tinkoff/request-core": "^0.9.2",
|
|
24
23
|
"@tinkoff/request-plugin-protocol-http": "^0.11.6",
|
|
25
|
-
"@tinkoff/url": "0.8.
|
|
24
|
+
"@tinkoff/url": "0.8.5"
|
|
26
25
|
},
|
|
27
26
|
"peerDependencies": {
|
|
28
|
-
"@tramvai/core": "2.
|
|
29
|
-
"@tramvai/module-common": "2.
|
|
30
|
-
"@tramvai/state": "2.
|
|
31
|
-
"@tramvai/tokens-http-client": "2.
|
|
27
|
+
"@tramvai/core": "2.72.0",
|
|
28
|
+
"@tramvai/module-common": "2.72.0",
|
|
29
|
+
"@tramvai/state": "2.72.0",
|
|
30
|
+
"@tramvai/tokens-http-client": "2.72.0",
|
|
32
31
|
"tslib": "^2.4.0"
|
|
33
32
|
},
|
|
34
33
|
"module": "lib/server.es.js",
|