Skip to content

Commit

Permalink
added retry mechanicasm for enoent errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vishvamsinh28 committed Oct 19, 2024
1 parent 4a4dcb7 commit bd6424d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
24 changes: 21 additions & 3 deletions scripts/build-newsroom-videos.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const fetch = require('node-fetch-2');
async function buildNewsroomVideos(writePath) {
try {
const dir = dirname(writePath);

if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}

const response = await fetch('https://youtube.googleapis.com/youtube/v3/search?' + new URLSearchParams({
key: process.env.YOUTUBE_TOKEN,
part: 'snippet',
Expand All @@ -23,7 +25,6 @@ async function buildNewsroomVideos(writePath) {
}

const data = await response.json();
console.log(data)

if (!data.items || !Array.isArray(data.items)) {
throw new Error('Invalid data structure received from YouTube API');
Expand All @@ -39,17 +40,34 @@ async function buildNewsroomVideos(writePath) {
const videoData = JSON.stringify(videoDataItems, null, ' ');
console.log('The following are the Newsroom Youtube videos: ', videoData);

writeFileSync(writePath, videoData);
await retryWriteFile(writePath, videoData);

return videoData;
} catch (err) {
throw new Error(`Failed to build newsroom videos: ${err.message}`);
}
}

async function retryWriteFile(filePath, data, retries = 3, delay = 1000) {
for (let attempt = 0; attempt < retries; attempt++) {
try {
writeFileSync(filePath, data);
console.log(`File written successfully to ${filePath}`);
break;
} catch (err) {
if (err.code === 'ENOENT') {
console.error(`ENOENT error on attempt ${attempt + 1}. Retrying in ${delay}ms...`);
await new Promise((resolve) => setTimeout(resolve, delay));

Check warning on line 60 in scripts/build-newsroom-videos.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-newsroom-videos.js#L58-L60

Added lines #L58 - L60 were not covered by tests
} else {
throw err;

Check warning on line 62 in scripts/build-newsroom-videos.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-newsroom-videos.js#L62

Added line #L62 was not covered by tests
}
}
}
}

/* istanbul ignore next */
if (require.main === module) {
buildNewsroomVideos(resolve(__dirname, '../config', 'newsroom_videos.json'))
buildNewsroomVideos(resolve(__dirname, '../config', 'newsroom_videos.json'));
}

module.exports = { buildNewsroomVideos };
23 changes: 19 additions & 4 deletions scripts/build-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,32 @@ const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPa
if (!fs.existsSync(automatedDir)) {
fs.mkdirSync(automatedDir, { recursive: true });
}
fs.writeFileSync(
automatedToolsPath,
JSON.stringify(automatedTools, null, ' ')
);

await retryWriteFile(automatedToolsPath, JSON.stringify(automatedTools, null, ' '));

await combineTools(automatedTools, require(manualToolsPath), toolsPath, tagsPath);
} catch (err) {
throw new Error(`An error occurred while building tools: ${err.message}`);
}
};

async function retryWriteFile(filePath, data, retries = 3, delay = 1000) {
for (let attempt = 0; attempt < retries; attempt++) {
try {
fs.writeFileSync(filePath, data);
console.log(`File written successfully to ${filePath}`);
break;
} catch (err) {
if (err.code === 'ENOENT') {
console.error(`ENOENT error on attempt ${attempt + 1}. Retrying in ${delay}ms...`);
await new Promise((resolve) => setTimeout(resolve, delay));

Check warning on line 35 in scripts/build-tools.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-tools.js#L33-L35

Added lines #L33 - L35 were not covered by tests
} else {
throw err;

Check warning on line 37 in scripts/build-tools.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-tools.js#L37

Added line #L37 was not covered by tests
}
}
}
}

/* istanbul ignore next */
if (require.main === module) {
const automatedToolsPath = resolve(__dirname, '../config', 'tools-automated.json');
Expand Down

0 comments on commit bd6424d

Please sign in to comment.