Skip to content
Snippets Groups Projects

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

Merged Martin Lowe requested to merge malowe/main/retry-fix into main
3 files
+ 38
48
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 37
27
@@ -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
});
};
}
Loading