From c2c63abbeadde39e31d38ed4b6b18f3ed79b8bae Mon Sep 17 00:00:00 2001 From: bangbang93 Date: Tue, 20 Feb 2024 15:02:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8D=95=E4=B8=AA=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=87=BA=E9=94=99=E6=97=B6=E4=B8=8D=E8=A6=81=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cluster.ts | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/cluster.ts b/src/cluster.ts index 0fc208f..0097305 100644 --- a/src/cluster.ts +++ b/src/cluster.ts @@ -144,6 +144,7 @@ export class Cluster { }) const parallel = syncConfig.concurrency const noopen = syncConfig.source === 'center' ? 1 : 0 + let hasError = false await pMap( missingFiles, async (file, i) => { @@ -153,30 +154,42 @@ export class Cluster { logger.info(`[${i + 1}/${missingFiles.length}] ${colors.green('downloading')} ${colors.underline(file.path)}`) } let lastProgress = 0 - const res = await this.got - .get(file.path.substring(1), { - searchParams: { - noopen, - }, - retry: { - limit: 10, - }, - }) - .on('downloadProgress', (progress) => { - bar.tick(progress.transferred - lastProgress) - lastProgress = progress.transferred - }) - const isFileCorrect = validateFile(res.body, file.hash) - if (!isFileCorrect) { - throw new Error(`文件${file.path}校验失败`) + try { + const res = await this.got + .get(file.path.substring(1), { + searchParams: { + noopen, + }, + retry: { + limit: 10, + }, + }) + .on('downloadProgress', (progress) => { + bar.tick(progress.transferred - lastProgress) + lastProgress = progress.transferred + }) + const isFileCorrect = validateFile(res.body, file.hash) + if (!isFileCorrect) { + hasError = true + logger.error(`文件${file.path}校验失败`) + return + } + await this.storage.writeFile(hashToFilename(file.hash), res.body) + } catch (e) { + hasError = true + logger.error(e) } - await this.storage.writeFile(hashToFilename(file.hash), res.body) }, { concurrency: parallel, }, ) await this.storage.gc(fileList.files) + if (hasError) { + throw new Error('同步失败') + } else { + logger.info('同步完成') + } } public setupExpress(https: boolean): Server {