Skip to content

Commit 0c4cd91

Browse files
author
Ives van Hoorne
committed
Rollback to old packager
1 parent c11d43c commit 0c4cd91

File tree

3 files changed

+132
-26
lines changed

3 files changed

+132
-26
lines changed

src/sandbox/npm/fetch-dependencies.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import _debug from 'app/utils/debug';
22
import dependenciesToQuery from './dependencies-to-query';
3-
import { PACKAGER_URL } from './';
43
import delay from '../utils/delay';
54
import actions, { dispatch } from '../actions';
65
import setScreen from '../status-screen';
@@ -9,7 +8,6 @@ type Dependencies = {
98
[dependency: string]: string,
109
};
1110

12-
const RETRY_COUNT = 60;
1311
const debug = _debug('cs:sandbox:packager');
1412

1513
function callApi(url: string) {
@@ -26,39 +24,44 @@ function callApi(url: string) {
2624
.then(response => response.json());
2725
}
2826

27+
export const PACKAGER_URL = 'https://webpack-dll-prod.herokuapp.com/v6';
28+
export const NEW_PACKAGER_URL =
29+
'https://42qpdtykai.execute-api.eu-west-1.amazonaws.com/prod/package';
30+
31+
const RETRY_COUNT = 20;
32+
2933
/**
3034
* Request the packager, if retries > RETRY_COUNT it will throw if something goes wrong
3135
* otherwise it will retry again with an incremented retry
3236
*
3337
* @param {string} query The dependencies to call
3438
*/
35-
async function requestManifest(url) {
39+
async function requestPackager(query: string) {
3640
let retries = 0;
3741

3842
while (true) {
3943
debug(`Trying to call packager for ${retries} time`);
4044
try {
41-
const manifest = await callApi(url); // eslint-disable-line no-await-in-loop
45+
const url = `${PACKAGER_URL}/${query}`;
46+
const result = await callApi(`${url}/manifest.json`); // eslint-disable-line no-await-in-loop
4247

43-
return manifest;
48+
return { ...result, url };
4449
} catch (e) {
4550
const statusCode = e.response && e.response.status;
4651

47-
setScreen({ type: 'loading', text: 'Bundling Dependencies...' });
48-
49-
// 403 status code means the bundler is still bundling
50-
if (retries < RETRY_COUNT && statusCode === 403) {
52+
// 504 status code means the bundler is still bundling
53+
if (retries < RETRY_COUNT && statusCode === 504) {
5154
retries += 1;
52-
await delay(1000 * 2); // eslint-disable-line no-await-in-loop
5355
} else if (retries < RETRY_COUNT && statusCode === 500) {
54-
dispatch(
55-
actions.notifications.showNotification(
56-
'It seems like all packagers are busy, retrying in 10 seconds...',
57-
'warning',
58-
),
59-
);
60-
61-
await delay(1000 * 2); // eslint-disable-line no-await-in-loop
56+
if (dispatch) {
57+
dispatch(
58+
actions.notifications.showNotification(
59+
'It seems like all packagers are busy, retrying in 10 seconds...',
60+
'warning',
61+
),
62+
);
63+
}
64+
await delay(1000 * 10); // eslint-disable-line no-await-in-loop
6265
retries += 1;
6366
} else {
6467
throw e;
@@ -70,19 +73,26 @@ async function requestManifest(url) {
7073
async function callPackager(dependencies: Object) {
7174
const dependencyUrl = dependenciesToQuery(dependencies);
7275

73-
const result = await callApi(`${PACKAGER_URL}/${dependencyUrl}`);
74-
75-
const dataUrl = result.url;
76-
const manifest = await requestManifest(`${dataUrl}/manifest.json`);
76+
try {
77+
// Warmup cache
78+
window.fetch(`${NEW_PACKAGER_URL}/${dependencyUrl}`);
79+
} catch (e) {
80+
console.error(e);
81+
}
7782

78-
return { url: dataUrl, manifest };
83+
const manifest = await requestPackager(dependencyUrl);
84+
return manifest;
7985
}
8086

87+
// eslint-disable-next-line
88+
/*::
89+
const _apiActions = createAPIActions('pref', 'act');
90+
*/
8191
export default async function fetchDependencies(npmDependencies: Dependencies) {
8292
if (Object.keys(npmDependencies).length !== 0) {
8393
// New Packager flow
8494
try {
85-
const result = await callPackager(npmDependencies);
95+
const result = await callPackager(npmDependencies, dispatch);
8696

8797
return result;
8898
} catch (e) {
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import _debug from 'app/utils/debug';
2+
import dependenciesToQuery from './dependencies-to-query';
3+
import { PACKAGER_URL } from './';
4+
import delay from '../utils/delay';
5+
import actions, { dispatch } from '../actions';
6+
import setScreen from '../status-screen';
7+
8+
type Dependencies = {
9+
[dependency: string]: string,
10+
};
11+
12+
const RETRY_COUNT = 60;
13+
const debug = _debug('cs:sandbox:packager');
14+
15+
function callApi(url: string) {
16+
return fetch(url)
17+
.then(response => {
18+
if (response.status >= 200 && response.status < 300) {
19+
return Promise.resolve(response);
20+
}
21+
22+
const error = new Error(response.statusText || response.status);
23+
error.response = response;
24+
return Promise.reject(error);
25+
})
26+
.then(response => response.json());
27+
}
28+
29+
/**
30+
* Request the packager, if retries > RETRY_COUNT it will throw if something goes wrong
31+
* otherwise it will retry again with an incremented retry
32+
*
33+
* @param {string} query The dependencies to call
34+
*/
35+
async function requestManifest(url) {
36+
let retries = 0;
37+
38+
while (true) {
39+
debug(`Trying to call packager for ${retries} time`);
40+
try {
41+
const manifest = await callApi(url); // eslint-disable-line no-await-in-loop
42+
43+
return manifest;
44+
} catch (e) {
45+
const statusCode = e.response && e.response.status;
46+
47+
setScreen({ type: 'loading', text: 'Bundling Dependencies...' });
48+
49+
// 403 status code means the bundler is still bundling
50+
if (retries < RETRY_COUNT && statusCode === 403) {
51+
retries += 1;
52+
await delay(1000 * 2); // eslint-disable-line no-await-in-loop
53+
} else if (retries < RETRY_COUNT && statusCode === 500) {
54+
dispatch(
55+
actions.notifications.showNotification(
56+
'It seems like all packagers are busy, retrying in 10 seconds...',
57+
'warning',
58+
),
59+
);
60+
61+
await delay(1000 * 2); // eslint-disable-line no-await-in-loop
62+
retries += 1;
63+
} else {
64+
throw e;
65+
}
66+
}
67+
}
68+
}
69+
70+
async function callPackager(dependencies: Object) {
71+
const dependencyUrl = dependenciesToQuery(dependencies);
72+
73+
const result = await callApi(`${PACKAGER_URL}/${dependencyUrl}`);
74+
75+
const dataUrl = result.url;
76+
const manifest = await requestManifest(`${dataUrl}/manifest.json`);
77+
78+
return { url: dataUrl, manifest };
79+
}
80+
81+
export default async function fetchDependencies(npmDependencies: Dependencies) {
82+
if (Object.keys(npmDependencies).length !== 0) {
83+
// New Packager flow
84+
try {
85+
const result = await callPackager(npmDependencies);
86+
87+
return result;
88+
} catch (e) {
89+
e.message = `Could not fetch dependencies: ${e.message}`;
90+
dispatch(actions.notifications.showNotification(e.message, 'error'));
91+
92+
throw e;
93+
}
94+
}
95+
return false;
96+
}

src/sandbox/npm/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ export default async function loadDependencies(dependencies: NPMDependencies) {
3939
// Mark that the last requested url is this
4040
loadedDependencyCombination = depQuery;
4141

42-
setScreen({ type: 'loading', text: 'Fetching Dependency Versions...' });
42+
setScreen({ type: 'loading', text: 'Bundling Dependencies...' });
4343

4444
const data = await fetchDependencies(dependencies);
4545

46-
manifest = data.manifest;
46+
manifest = data;
4747

4848
setScreen({ type: 'loading', text: 'Downloading Dependencies...' });
4949

0 commit comments

Comments
 (0)