From 71a7c1b9d34788e27ad15ad8c7390cf51a533569 Mon Sep 17 00:00:00 2001 From: whyour Date: Fri, 25 Apr 2025 23:52:33 +0800 Subject: [PATCH] =?UTF-8?q?boot=20=E4=BB=BB=E5=8A=A1=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=9C=A8=E4=BE=9D=E8=B5=96=E5=AE=89=E8=A3=85=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E5=90=8E=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- back/app.ts | 1 - back/loaders/initData.ts | 24 +++++++++++++----------- back/services/dependence.ts | 4 +++- back/shared/pLimit.ts | 13 +++++++++++++ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/back/app.ts b/back/app.ts index 1df44c05..ff25583a 100644 --- a/back/app.ts +++ b/back/app.ts @@ -17,7 +17,6 @@ async function startServer() { Logger.debug(`✌️ 后端服务启动成功!`); console.debug(`✌️ 后端服务启动成功!`); process.send?.('ready'); - require('./loaders/bootAfter').default(); }) .on('error', (err) => { Logger.error(err); diff --git a/back/loaders/initData.ts b/back/loaders/initData.ts index 96b4552e..02001b88 100644 --- a/back/loaders/initData.ts +++ b/back/loaders/initData.ts @@ -68,24 +68,26 @@ export default async () => { }); } - const installDependencies = () => { - // 初始化时安装所有处于安装中,安装成功,安装失败的依赖 - DependenceModel.findAll({ + const installDependencies = async () => { + const docs = await DependenceModel.findAll({ where: {}, order: [ ['type', 'DESC'], ['createdAt', 'DESC'], ], raw: true, - }).then(async (docs) => { - await DependenceModel.update( - { status: DependenceStatus.queued, log: [] }, - { where: { id: docs.map((x) => x.id!) } }, - ); - setTimeout(() => { - dependenceService.installDependenceOneByOne(docs); - }, 5000); }); + + await DependenceModel.update( + { status: DependenceStatus.queued, log: [] }, + { where: { id: docs.map((x) => x.id!) } }, + ); + + setTimeout(async () => { + await dependenceService.installDependenceOneByOne(docs); + + require('./loaders/bootAfter').default(); + }, 5000); }; // 初始化更新 linux/python/nodejs 镜像源配置 diff --git a/back/services/dependence.ts b/back/services/dependence.ts index 28f0ac89..01fdaba7 100644 --- a/back/services/dependence.ts +++ b/back/services/dependence.ts @@ -132,10 +132,12 @@ export default class DependenceService { docs: Dependence[], isInstall: boolean = true, force: boolean = false, - ) { + ): Promise { docs.forEach((dep) => { this.installOrUninstallDependency(dep, isInstall, force); }); + + return taskLimit.waitDependencyQueueDone(); } public async reInstall(ids: number[]): Promise { diff --git a/back/shared/pLimit.ts b/back/shared/pLimit.ts index 2221ca7c..225918b8 100644 --- a/back/shared/pLimit.ts +++ b/back/shared/pLimit.ts @@ -189,6 +189,19 @@ class TaskLimit { return this.scriptLimit.add(fn, options); } + public async waitDependencyQueueDone(): Promise { + if (this.dependenyLimit.size === 0 && this.dependenyLimit.pending === 0) { + return; + } + return new Promise((resolve) => { + const onIdle = () => { + this.dependenyLimit.removeListener('idle', onIdle); + resolve(); + }; + this.dependenyLimit.on('idle', onIdle); + }); + } + public runDependeny( dependency: Dependence, fn: IDependencyFn,