1- import { Injectable , Logger , OnModuleDestroy , OnModuleInit } from '@nestjs/common' ;
1+ import { Injectable , Logger , OnApplicationBootstrap , OnModuleDestroy } from '@nestjs/common' ;
2+ import { Timeout } from '@nestjs/schedule' ;
23import { constants } from 'fs' ;
34import { access } from 'fs/promises' ;
45
@@ -11,7 +12,7 @@ import { getters } from '@app/store/index.js';
1112import { VmDomain , VmState } from '@app/unraid-api/graph/resolvers/vms/vms.model.js' ;
1213
1314@Injectable ( )
14- export class VmsService implements OnModuleInit , OnModuleDestroy {
15+ export class VmsService implements OnApplicationBootstrap , OnModuleDestroy {
1516 private readonly logger = new Logger ( VmsService . name ) ;
1617 private hypervisor : InstanceType < typeof HypervisorClass > | null = null ;
1718 private isVmsAvailable : boolean = false ;
@@ -38,11 +39,26 @@ export class VmsService implements OnModuleInit, OnModuleDestroy {
3839 }
3940 }
4041
41- async onModuleInit ( ) {
42+ async onApplicationBootstrap ( ) {
4243 this . logger . debug ( `Initializing VMs service with URI: ${ this . uri } ` ) ;
4344 await this . attemptHypervisorInitializationAndWatch ( ) ;
4445 }
4546
47+ @Timeout ( 10_000 )
48+ async healInitialization ( maxRetries = 12 , delay = 10_000 ) {
49+ let retries = 1 ;
50+ while ( ! this . isVmsAvailable && retries <= maxRetries ) {
51+ this . logger . log ( `Attempting to initialize VMs service...attempt ${ retries } /${ maxRetries } ` ) ;
52+ await this . attemptHypervisorInitializationAndWatch ( ) ;
53+ if ( this . isVmsAvailable ) {
54+ this . logger . log ( 'VMs service initialized successfully' ) ;
55+ break ;
56+ }
57+ await new Promise ( ( resolve ) => setTimeout ( resolve , delay ) ) ;
58+ retries ++ ;
59+ }
60+ }
61+
4662 async onModuleDestroy ( ) {
4763 this . logger . debug ( 'Closing file watcher...' ) ;
4864 await this . watcher ?. close ( ) ;
0 commit comments