From b2e78012384e6b3f2630341281fc811026be23b9 Mon Sep 17 00:00:00 2001 From: Pujit Mehrotra Date: Fri, 22 Aug 2025 15:29:44 -0400 Subject: [PATCH] fix: retry VMs init for up to 2 min (#1612) --- .../graph/resolvers/vms/vms.service.spec.ts | 2 +- .../graph/resolvers/vms/vms.service.ts | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/api/src/unraid-api/graph/resolvers/vms/vms.service.spec.ts b/api/src/unraid-api/graph/resolvers/vms/vms.service.spec.ts index e6256b2a5..7b6733990 100644 --- a/api/src/unraid-api/graph/resolvers/vms/vms.service.spec.ts +++ b/api/src/unraid-api/graph/resolvers/vms/vms.service.spec.ts @@ -196,7 +196,7 @@ describe('VmsService', () => { service = module.get(VmsService); // Initialize the service - await service.onModuleInit(); + await service.onApplicationBootstrap(); }); afterAll(async () => { diff --git a/api/src/unraid-api/graph/resolvers/vms/vms.service.ts b/api/src/unraid-api/graph/resolvers/vms/vms.service.ts index 4f507ff51..58ecffdc2 100644 --- a/api/src/unraid-api/graph/resolvers/vms/vms.service.ts +++ b/api/src/unraid-api/graph/resolvers/vms/vms.service.ts @@ -1,4 +1,5 @@ -import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common'; +import { Injectable, Logger, OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common'; +import { Timeout } from '@nestjs/schedule'; import { constants } from 'fs'; import { access } from 'fs/promises'; @@ -11,7 +12,7 @@ import { getters } from '@app/store/index.js'; import { VmDomain, VmState } from '@app/unraid-api/graph/resolvers/vms/vms.model.js'; @Injectable() -export class VmsService implements OnModuleInit, OnModuleDestroy { +export class VmsService implements OnApplicationBootstrap, OnModuleDestroy { private readonly logger = new Logger(VmsService.name); private hypervisor: InstanceType | null = null; private isVmsAvailable: boolean = false; @@ -38,11 +39,26 @@ export class VmsService implements OnModuleInit, OnModuleDestroy { } } - async onModuleInit() { + async onApplicationBootstrap() { this.logger.debug(`Initializing VMs service with URI: ${this.uri}`); await this.attemptHypervisorInitializationAndWatch(); } + @Timeout(10_000) + async healInitialization(maxRetries = 12, delay = 10_000) { + let retries = 1; + while (!this.isVmsAvailable && retries <= maxRetries) { + this.logger.log(`Attempting to initialize VMs service...attempt ${retries}/${maxRetries}`); + await this.attemptHypervisorInitializationAndWatch(); + if (this.isVmsAvailable) { + this.logger.log('VMs service initialized successfully'); + break; + } + await new Promise((resolve) => setTimeout(resolve, delay)); + retries++; + } + } + async onModuleDestroy() { this.logger.debug('Closing file watcher...'); await this.watcher?.close();