Removing blocking download actions.
This commit is contained in:
@@ -172,15 +172,15 @@ function formatStatusTimestamp(date = new Date()) {
|
|||||||
// === Execution / saving ===
|
// === Execution / saving ===
|
||||||
// Functions that execute per-document fetches and write CSV outputs.
|
// Functions that execute per-document fetches and write CSV outputs.
|
||||||
|
|
||||||
function writeCsv(outputPath, rows) {
|
async function writeCsv(outputPath, rows) {
|
||||||
const csv = stringify(rows, {
|
const csv = stringify(rows, {
|
||||||
header: false,
|
header: false,
|
||||||
quoted: true,
|
quoted: true,
|
||||||
});
|
});
|
||||||
fs.writeFileSync(outputPath, csv, 'utf8');
|
await fs.promises.writeFile(outputPath, csv, 'utf8');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function executeDocumentGroup(group, fetcher) {
|
function executeDocumentGroup(group, fetcher) {
|
||||||
const sheetNames = group.sheets.map(sheet => sheet.sheetName);
|
const sheetNames = group.sheets.map(sheet => sheet.sheetName);
|
||||||
|
|
||||||
for (const sheet of group.sheets) {
|
for (const sheet of group.sheets) {
|
||||||
@@ -188,11 +188,15 @@ async function executeDocumentGroup(group, fetcher) {
|
|||||||
setTaskStatus(`${group.documentId}:${sheet.sheetName}`, `${displayName} - pulling...`);
|
setTaskStatus(`${group.documentId}:${sheet.sheetName}`, `${displayName} - pulling...`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run the actual fetch/write flow in the background so the scheduler doesn't pause.
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
const rowsBySheet = await fetcher(group.documentId, sheetNames);
|
const rowsBySheet = await fetcher(group.documentId, sheetNames);
|
||||||
|
|
||||||
for (const sheet of group.sheets) {
|
for (const sheet of group.sheets) {
|
||||||
const rows = rowsBySheet[sheet.sheetName] || [];
|
const rows = rowsBySheet[sheet.sheetName] || [];
|
||||||
writeCsv(sheet.outputPath, rows);
|
try {
|
||||||
|
await writeCsv(sheet.outputPath, rows);
|
||||||
const relativePath = path.relative(process.cwd(), sheet.outputPath) || sheet.outputPath;
|
const relativePath = path.relative(process.cwd(), sheet.outputPath) || sheet.outputPath;
|
||||||
const timestamp = formatStatusTimestamp();
|
const timestamp = formatStatusTimestamp();
|
||||||
const displayName = `${group.documentDisplayName || group.documentId}:${sheet.sheetName}`;
|
const displayName = `${group.documentDisplayName || group.documentId}:${sheet.sheetName}`;
|
||||||
@@ -200,8 +204,23 @@ async function executeDocumentGroup(group, fetcher) {
|
|||||||
`${group.documentId}:${sheet.sheetName}`,
|
`${group.documentId}:${sheet.sheetName}`,
|
||||||
`${displayName} - saved to ${relativePath} (updated ${timestamp})`
|
`${displayName} - saved to ${relativePath} (updated ${timestamp})`
|
||||||
);
|
);
|
||||||
|
} catch (writeErr) {
|
||||||
|
setTaskStatus(
|
||||||
|
`${group.documentId}:${sheet.sheetName}`,
|
||||||
|
`${group.documentId}:${sheet.sheetName} - failed to save: ${formatGoogleError(writeErr)}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
for (const sheet of group.sheets) {
|
||||||
|
setTaskStatus(
|
||||||
|
`${group.documentId}:${sheet.sheetName}`,
|
||||||
|
`${group.documentId}:${sheet.sheetName} - failed: ${interpretGoogleSheetsError(err, group.documentId, sheet.sheetName)}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
function formatGoogleError(err) {
|
function formatGoogleError(err) {
|
||||||
if (err && err.response && err.response.data) {
|
if (err && err.response && err.response.data) {
|
||||||
@@ -334,7 +353,9 @@ async function scheduleRuns() {
|
|||||||
currentIndex = (currentIndex + 1) % groups.length;
|
currentIndex = (currentIndex + 1) % groups.length;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await executeDocumentGroup(group, fetcher);
|
// Fire off the document fetch/save in the background and don't await it,
|
||||||
|
// so the scheduler can continue to the next group immediately.
|
||||||
|
executeDocumentGroup(group, fetcher);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
for (const sheet of group.sheets) {
|
for (const sheet of group.sheets) {
|
||||||
setTaskStatus(
|
setTaskStatus(
|
||||||
|
|||||||
Reference in New Issue
Block a user