Skip to content

Commit

Permalink
fix: retry save xls after 10 seconds when file busy
Browse files Browse the repository at this point in the history
  • Loading branch information
popstas committed Apr 18, 2020
1 parent 0942833 commit 7b54273
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/scrap-site.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,29 @@ module.exports = async (baseUrl, options = {}) => {
const perPage = Math.round((t / requestedCount) * 100) / 100;
await crawler.close();

saveAsXlsx(csvPath, xlsxPath);
const finishScan = () => {
if(options.removeCsv) {
fs.unlinkSync(csvPath);
}

console.log(`\n${color.yellow}Saved to ${xlsxPath}${color.reset}`);
console.log(`Finish: ${t} sec (${perPage} per page)`);
};

if(options.removeCsv) {
fs.unlinkSync(csvPath);
let isSuccess = true;
try {
saveAsXlsx(csvPath, xlsxPath);
} catch (e) {
if(e.code == 'EBUSY'){
isSuccess = false;
console.error(`${color.red}${xlsxPath} is busy, please close file in 10 seconds!`);
setTimeout(() => {
saveAsXlsx(csvPath, xlsxPath);
finishScan();
}, 10000)
}
else console.error(e);
}

console.log(`\n${color.yellow}Saved to ${xlsxPath}${color.reset}`);
console.log(`Finish: ${t} sec (${perPage} per page)`);
if(isSuccess) finishScan();
};

0 comments on commit 7b54273

Please sign in to comment.