fix: handle slow database update

This commit is contained in:
tdurieux
2021-09-07 17:31:10 +02:00
parent 792b3afcb4
commit 924990b3cb
+27 -9
View File
@@ -61,27 +61,45 @@ export default class GitHubDownload extends GitHubBase implements SourceBase {
const originalPath = this.repository.originalCachePath; const originalPath = this.repository.originalCachePath;
await storage.mk(originalPath); await storage.mk(originalPath);
let progress = null; let progress = null;
let progressInterval = setInterval(async () => { let progressTimeout;
let inDownload = true;
const that = this;
async function updateProgress() {
if (progress) { if (progress) {
await this.repository.updateStatus( await that.repository.updateStatus(
this.repository.status, that.repository.status,
progress.transferred progress.transferred
); );
} }
}, 1000); if (inDownload) {
progressTimeout = setTimeout(updateProgress, 1500);
}
}
updateProgress();
await storage.extractTar( await storage.extractTar(
originalPath, originalPath,
got got
.stream(response.url) .stream(response.url)
.on("downloadProgress", async (p) => { .on("downloadProgress", async (p) => {
console.log(p); inDownload = true;
progress = p; progress = p;
}) })
.on("error", () => clearInterval(progressInterval)) .on("error", (error) => {
.on("end", () => clearInterval(progressInterval)) inDownload = false;
.on("close", () => clearInterval(progressInterval)) clearTimeout(progressTimeout);
})
.on("end", () => {
inDownload = false;
console.log("download finished");
clearTimeout(progressTimeout);
})
.on("close", () => clearTimeout(progressTimeout))
); );
clearInterval(progressInterval);
inDownload = false;
clearTimeout(progressTimeout);
await this.repository.updateStatus("ready"); await this.repository.updateStatus("ready");
} }