Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/src/unraid-api/graph/resolvers/vms/vms.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ describe('VmsService', () => {
service = module.get<VmsService>(VmsService);

// Initialize the service
await service.onModuleInit();
await service.onApplicationBootstrap();
});

afterAll(async () => {
Expand Down
22 changes: 19 additions & 3 deletions api/src/unraid-api/graph/resolvers/vms/vms.service.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<typeof HypervisorClass> | null = null;
private isVmsAvailable: boolean = false;
Expand All @@ -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();
Expand Down
Loading