Skip to content
Snippets Groups Projects
Commit 45d91bcf authored by Martin Lowe's avatar Martin Lowe :flag_ca:
Browse files

Remove pretry as it doesn't catch all of the errors, add manual handling

parent a5233787
No related branches found
No related tags found
1 merge request!9Remove pretry as it doesn't catch all of the errors, add manual handling
Pipeline #42277 passed
......@@ -6,8 +6,7 @@
"dependencies": {
"@redocly/cli": "^1.6.0",
"eclipsefdn-api-support": "^1.0.0",
"eclipsefdn-hugo-solstice-theme": "^0.0.176",
"p-retry": "^6.2.0"
"eclipsefdn-hugo-solstice-theme": "^0.0.176"
},
"scripts": {
"postinstall": "yarn build-specs",
......
......@@ -16,7 +16,6 @@ import { exec as execProcess } from 'child_process';
import fs from 'fs';
import process from 'process';
import yaml from 'yaml';
import pRetry from 'p-retry';
const exec = util.promisify(execProcess);
// matches web URLs
......@@ -33,16 +32,27 @@ const artificialSpecData = { items: [] };
* @param {*} localPath the location where the downloaded spec should be saved
*/
async function pullExternalSpec(externalLocation, localPath) {
fs.writeFileSync(
localPath,
await fetch(externalLocation).then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
let retries = 0;
// retry 3 times before giving up
while (retries < 3) {
try {
fs.writeFileSync(
localPath,
await fetch(externalLocation).then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.text();
})
);
return response.text();
})
);
return;
} catch (e) {
console.log('Error while fetching external spec', e);
}
retries++;
}
throw new Error(`Cannot process ${externalLocation} after 3 retries.`);
}
/**
......@@ -64,10 +74,21 @@ function isExternalSpec(location) {
async function processSpec(specDisplayName, specMachineName) {
console.log(`Processing openapi spec for ${specDisplayName}...`);
// Go to the location where the spec is, and continue processing
await exec(
`npx @redocly/cli build-docs --output="..${path.sep}content${path.sep}${specMachineName}${path.sep}index.html" "${specMachineName}${path.sep}openapi.yaml"`
);
console.log(`Done processing ${specDisplayName}`);
let retries = 0;
// retry 3 times before giving up
while (retries < 3) {
try {
await exec(
`npx @redocly/cli build-docs --output="..${path.sep}content${path.sep}${specMachineName}${path.sep}index.html" "${specMachineName}${path.sep}openapi.yaml"`
);
console.log(`Done processing ${specDisplayName}`);
return;
} catch (e) {
console.log('Error while processing spec', e);
}
retries++;
}
throw new Error(`Cannot process spec '${specDisplayName}' after 3 retries.`);
}
run();
......@@ -101,19 +122,13 @@ async function run() {
console.log(`Retrieving openapi spec for ${specDisplayName}...`);
// prepare the spec, fetching remote specs or copying the spec for processing
if (isExternalSpec(specLocation)) {
await pRetry(() => pullExternalSpec(specLocation, outputSpecLocation)
.catch(result => console.log(`Failed to fetch the resource at '${specLocation}`)), {
retries: 3,
onFailedAttempt: error => console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`),
minTimeout: 1000,
maxRetryTime: 10000
});
await pullExternalSpec(specLocation, outputSpecLocation);
} else {
fs.copyFileSync(potentialLocalSpecLocation, outputSpecLocation);
console.log(`Copied local spec for ${specDisplayName} to output dir`);
}
// process the spec, setting the information about the spec into the output data var
await pRetry(async () => processSpec(specDisplayName, specMachineName).then(() => {
await processSpec(specDisplayName, specMachineName).then(() => {
artificialSpecData['items'].push({ name: specMachineName, displayName: specDisplayName });
}).then(() => {
console.log('Outputing generated spec data for site render');
......@@ -121,11 +136,6 @@ async function run() {
fs.mkdirSync('../data', { recursive: true });
// Dump the generated spec list to the data file for hugo build
fs.writeFileSync('../data/specs.yaml', yaml.stringify(artificialSpecData));
}).catch(result => console.log(`Failed to process spec ${specDisplayName}`)), {
retries: 3,
onFailedAttempt: error => console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`),
minTimeout: 1000,
maxRetryTime: 10000
});
};
}
......@@ -1479,11 +1479,6 @@
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==
"@types/retry@0.12.2":
version "0.12.2"
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.2.tgz#ed279a64fa438bb69f2480eda44937912bb7480a"
integrity sha512-XISRgDJ2Tc5q4TRqvgJtzsRkFYNJzZrhTdtMoGVBttwzzQJkPnS3WWTFc7kuDRoPtPakl+T+OfdEUjYJj7Jbow==
"@types/send@*":
version "0.17.4"
resolved "https://registry.yarnpkg.com/@types/send/-/send-0.17.4.tgz#6619cd24e7270793702e4e6a4b958a9010cfc57a"
......@@ -4010,11 +4005,6 @@ is-negative-zero@^2.0.2:
resolved "https://registry.yarnpkg.com/is-negative-zero/-/is-negative-zero-2.0.2.tgz#7bf6f03a28003b8b3965de3ac26f664d765f3150"
integrity sha512-dqJvarLawXsFbNDeJW7zAz8ItJ9cd28YufuuFzh0G8pNHjJMnY08Dv7sYX2uF5UpQOwieAeOExEYAWWfu7ZZUA==
is-network-error@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/is-network-error/-/is-network-error-1.0.1.tgz#a68061a20387e9144e145571bea693056a370b92"
integrity sha512-OwQXkwBJeESyhFw+OumbJVD58BFBJJI5OM5S1+eyrDKlgDZPX2XNT5gXS56GSD3NPbbwUuMlR1Q71SRp5SobuQ==
is-number-object@^1.0.4:
version "1.0.7"
resolved "https://registry.yarnpkg.com/is-number-object/-/is-number-object-1.0.7.tgz#59d50ada4c45251784e9904f5246c742f07a42fc"
......@@ -5005,15 +4995,6 @@ p-retry@^4.5.0:
"@types/retry" "0.12.0"
retry "^0.13.1"
p-retry@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/p-retry/-/p-retry-6.2.0.tgz#8d6df01af298750009691ce2f9b3ad2d5968f3bd"
integrity sha512-JA6nkq6hKyWLLasXQXUrO4z8BUZGUt/LjlJxx8Gb2+2ntodU/SS63YZ8b0LUTbQ8ZB9iwOfhEPhg4ykKnn2KsA==
dependencies:
"@types/retry" "0.12.2"
is-network-error "^1.0.0"
retry "^0.13.1"
p-try@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment