mirror of
https://github.com/scratchfoundation/scratch-l10n.git
synced 2024-12-23 06:02:42 -05:00
fix: only retry the actual download
Some checks failed
CI/CD / build-and-deploy (push) Has been cancelled
Some checks failed
CI/CD / build-and-deploy (push) Has been cancelled
Also, further improve error messaging
This commit is contained in:
parent
41c1466604
commit
905f004b15
2 changed files with 52 additions and 33 deletions
|
@ -103,19 +103,34 @@ const getResourceLocation = async function (projectSlug, resourceSlug, localeCod
|
||||||
* strings, if the local is the source language)
|
* strings, if the local is the source language)
|
||||||
*/
|
*/
|
||||||
const txPull = async function (project, resource, locale, mode = 'default') {
|
const txPull = async function (project, resource, locale, mode = 'default') {
|
||||||
const url = await getResourceLocation(project, resource, locale, mode);
|
|
||||||
let buffer;
|
let buffer;
|
||||||
for (let i = 0; i < 5; i++) {
|
try {
|
||||||
try {
|
const url = await getResourceLocation(project, resource, locale, mode);
|
||||||
buffer = await download(url);
|
for (let i = 0; i < 5; i++) {
|
||||||
buffer = buffer.toString();
|
if (i > 0) {
|
||||||
return JSON.parse(buffer);
|
console.log(`Retrying txPull download after ${i} failed attempt(s)`);
|
||||||
} catch (e) {
|
}
|
||||||
console.error(e, {project, resource, locale, buffer});
|
try {
|
||||||
console.log(`Retrying after ${i + 1} failed attempt(s)`);
|
buffer = await download(url); // might throw(?)
|
||||||
|
break;
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e, {project, resource, locale, buffer});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (!buffer) {
|
||||||
|
throw Error(`txPull download failed after 5 retries: ${url}`);
|
||||||
|
}
|
||||||
|
buffer = buffer.toString();
|
||||||
|
return JSON.parse(buffer);
|
||||||
|
} catch (e) {
|
||||||
|
e.cause = {
|
||||||
|
project,
|
||||||
|
resource,
|
||||||
|
locale,
|
||||||
|
buffer
|
||||||
|
};
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
throw Error('failed to pull after 5 retries');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -47,30 +47,34 @@ const getLocaleData = async function (item) {
|
||||||
const locale = item.locale;
|
const locale = item.locale;
|
||||||
const resource = item.resource;
|
const resource = item.resource;
|
||||||
let txLocale = localeMap[locale] || locale;
|
let txLocale = localeMap[locale] || locale;
|
||||||
for (let i = 0; i < 5; i++) {
|
|
||||||
try {
|
|
||||||
const translations = await txPull(PROJECT, resource, txLocale);
|
|
||||||
|
|
||||||
const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`;
|
const translations = await txPull(PROJECT, resource, txLocale);
|
||||||
mkdirp.sync(txOutdir);
|
|
||||||
const fileName = `${txOutdir}/${locale}.json`;
|
|
||||||
|
|
||||||
await fs.writeFile(
|
const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`;
|
||||||
fileName,
|
const fileName = `${txOutdir}/${locale}.json`;
|
||||||
JSON.stringify(translations, null, 4)
|
|
||||||
);
|
try {
|
||||||
this.progress.increment();
|
mkdirp.sync(txOutdir);
|
||||||
return {
|
await fs.writeFile(
|
||||||
resource: resource,
|
fileName,
|
||||||
locale: locale,
|
JSON.stringify(translations, null, 4)
|
||||||
file: fileName
|
);
|
||||||
};
|
|
||||||
} catch (e) {
|
return {
|
||||||
console.error(e);
|
resource,
|
||||||
console.log(`retrying after ${i + 1} attempt(s)`);
|
locale,
|
||||||
}
|
fileName
|
||||||
|
};
|
||||||
|
} catch (e) {
|
||||||
|
e.cause = {
|
||||||
|
resource,
|
||||||
|
locale,
|
||||||
|
translations,
|
||||||
|
txOutdir,
|
||||||
|
fileName
|
||||||
|
};
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
throw Error('failed to pull translations after 5 retries');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const expandResourceFiles = (resources) => {
|
const expandResourceFiles = (resources) => {
|
||||||
|
@ -94,9 +98,9 @@ const pullTranslations = async function () {
|
||||||
const progress = new ProgressLogger(allFiles.length);
|
const progress = new ProgressLogger(allFiles.length);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await batchMap(allFiles, CONCURRENCY_LIMIT, item => {
|
await batchMap(allFiles, CONCURRENCY_LIMIT, async item => {
|
||||||
try {
|
try {
|
||||||
getLocaleData(item);
|
await getLocaleData(item);
|
||||||
} finally {
|
} finally {
|
||||||
progress.increment();
|
progress.increment();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue