Skip to content

Commit 318e71e

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

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

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

Lines changed: 14 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,21 @@ 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) {
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+
retries++;
54+
}
55+
}
56+
4657
async onModuleDestroy() {
4758
this.logger.debug('Closing file watcher...');
4859
await this.watcher?.close();

0 commit comments

Comments
 (0)