Skip to content

Commit b2e7801

Browse files
authored
fix: retry VMs init for up to 2 min (#1612)
1 parent fd895ca commit b2e7801

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

api/src/unraid-api/graph/resolvers/vms/vms.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ describe('VmsService', () => {
196196
service = module.get<VmsService>(VmsService);
197197

198198
// Initialize the service
199-
await service.onModuleInit();
199+
await service.onApplicationBootstrap();
200200
});
201201

202202
afterAll(async () => {

api/src/unraid-api/graph/resolvers/vms/vms.service.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Injectable, Logger, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
1+
import { Injectable, Logger, OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common';
2+
import { Timeout } from '@nestjs/schedule';
23
import { constants } from 'fs';
34
import { access } from 'fs/promises';
45

@@ -11,7 +12,7 @@ import { getters } from '@app/store/index.js';
1112
import { 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

Comments
 (0)