From 924990b3cbaddf4da4791e386446bfea7145c8b8 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Tue, 7 Sep 2021 17:31:10 +0200 Subject: [PATCH] fix: handle slow database update --- src/source/GitHubDownload.ts | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/source/GitHubDownload.ts b/src/source/GitHubDownload.ts index b131977..23981c0 100644 --- a/src/source/GitHubDownload.ts +++ b/src/source/GitHubDownload.ts @@ -61,27 +61,45 @@ export default class GitHubDownload extends GitHubBase implements SourceBase { const originalPath = this.repository.originalCachePath; await storage.mk(originalPath); let progress = null; - let progressInterval = setInterval(async () => { + let progressTimeout; + let inDownload = true; + + const that = this; + async function updateProgress() { if (progress) { - await this.repository.updateStatus( - this.repository.status, + await that.repository.updateStatus( + that.repository.status, progress.transferred ); } - }, 1000); + if (inDownload) { + progressTimeout = setTimeout(updateProgress, 1500); + } + } + updateProgress(); + await storage.extractTar( originalPath, got .stream(response.url) .on("downloadProgress", async (p) => { - console.log(p); + inDownload = true; progress = p; }) - .on("error", () => clearInterval(progressInterval)) - .on("end", () => clearInterval(progressInterval)) - .on("close", () => clearInterval(progressInterval)) + .on("error", (error) => { + inDownload = false; + 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"); }