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';
@@ -16,7 +16,6 @@ import { exec as execProcess } from 'child_process';
import fs from 'fs';
import fs from 'fs';
import process from 'process';
import process from 'process';
import yaml from 'yaml';
import yaml from 'yaml';
import pRetry from 'p-retry';
const exec = util.promisify(execProcess);
const exec = util.promisify(execProcess);
// matches web URLs
// matches web URLs
@@ -33,16 +32,27 @@ const artificialSpecData = { items: [] };
@@ -33,16 +32,27 @@ const artificialSpecData = { items: [] };
* @param {*} localPath the location where the downloaded spec should be saved
* @param {*} localPath the location where the downloaded spec should be saved
*/
*/
async function pullExternalSpec(externalLocation, localPath) {
async function pullExternalSpec(externalLocation, localPath) {
fs.writeFileSync(
let retries = 0;
localPath,
// retry 3 times before giving up
await fetch(externalLocation).then(response => {
while (retries < 3) {
if (!response.ok) {
try {
throw new Error(`HTTP error! Status: ${response.status}`);
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) {
@@ -64,10 +74,21 @@ function isExternalSpec(location) {
async function processSpec(specDisplayName, specMachineName) {
async function processSpec(specDisplayName, specMachineName) {
console.log(`Processing openapi spec for ${specDisplayName}...`);
console.log(`Processing openapi spec for ${specDisplayName}...`);
// Go to the location where the spec is, and continue processing
// Go to the location where the spec is, and continue processing
await exec(
let retries = 0;
`npx @redocly/cli build-docs --output="..${path.sep}content${path.sep}${specMachineName}${path.sep}index.html" "${specMachineName}${path.sep}openapi.yaml"`
// retry 3 times before giving up
);
while (retries < 3) {
console.log(`Done processing ${specDisplayName}`);
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();
run();
@@ -101,19 +122,13 @@ async function run() {
@@ -101,19 +122,13 @@ async function run() {
console.log(`Retrieving openapi spec for ${specDisplayName}...`);
console.log(`Retrieving openapi spec for ${specDisplayName}...`);
// prepare the spec, fetching remote specs or copying the spec for processing
// prepare the spec, fetching remote specs or copying the spec for processing
if (isExternalSpec(specLocation)) {
if (isExternalSpec(specLocation)) {
await pRetry(() => pullExternalSpec(specLocation, outputSpecLocation)
await 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
});
} else {
} else {
fs.copyFileSync(potentialLocalSpecLocation, outputSpecLocation);
fs.copyFileSync(potentialLocalSpecLocation, outputSpecLocation);
console.log(`Copied local spec for ${specDisplayName} to output dir`);
console.log(`Copied local spec for ${specDisplayName} to output dir`);
}
}
// process the spec, setting the information about the spec into the output data var
// 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 });
artificialSpecData['items'].push({ name: specMachineName, displayName: specDisplayName });
}).then(() => {
}).then(() => {
console.log('Outputing generated spec data for site render');
console.log('Outputing generated spec data for site render');
@@ -121,11 +136,6 @@ async function run() {
@@ -121,11 +136,6 @@ async function run() {
fs.mkdirSync('../data', { recursive: true });
fs.mkdirSync('../data', { recursive: true });
// Dump the generated spec list to the data file for hugo build
// Dump the generated spec list to the data file for hugo build
fs.writeFileSync('../data/specs.yaml', yaml.stringify(artificialSpecData));
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