Skip to content

Commit 6664c8b

Browse files
committed
fix: retry VMs init up to 2 min
1 parent fd895ca commit 6664c8b

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

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: 15 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,22 @@ 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+
await new Promise((resolve) => setTimeout(resolve, delay));
54+
retries++;
55+
}
56+
}
57+
4658
async onModuleDestroy() {
4759
this.logger.debug('Closing file watcher...');
4860
await this.watcher?.close();

0 commit comments

Comments
 (0)