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,