mirror of
https://github.com/unraid/api.git
synced 2026-01-09 01:59:57 -06:00
feat(api-config): enhance ApiConfigPersistence with shutdown handling
- Added `OnApplicationShutdown` lifecycle hook to `ApiConfigPersistence` for managing OS version tracking during application shutdown. - Removed the previous method for tracking OS version upgrades and streamlined the logic to set the last seen OS version directly during shutdown. - Introduced a private variable to store the current OS version for improved state management. This update improves the reliability of OS version tracking by ensuring it is properly handled during application shutdown, enhancing overall configuration persistence.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Injectable, Module, OnApplicationBootstrap } from '@nestjs/common';
|
||||
import { Injectable, Module, OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
|
||||
import { ConfigService, registerAs } from '@nestjs/config';
|
||||
import path from 'path';
|
||||
|
||||
@@ -56,8 +56,10 @@ export const apiConfig = registerAs<ApiConfig>('api', loadApiConfig);
|
||||
@Injectable()
|
||||
export class ApiConfigPersistence
|
||||
extends ConfigFilePersister<ApiConfig>
|
||||
implements OnApplicationBootstrap
|
||||
implements OnApplicationBootstrap, OnApplicationShutdown
|
||||
{
|
||||
private currentOsVersion: string | undefined;
|
||||
|
||||
constructor(configService: ConfigService) {
|
||||
super(configService);
|
||||
}
|
||||
@@ -88,19 +90,18 @@ export class ApiConfigPersistence
|
||||
|
||||
async onApplicationBootstrap() {
|
||||
this.configService.set('api.version', API_VERSION);
|
||||
await this.trackOsVersionUpgrade();
|
||||
this.currentOsVersion = this.configService.get<string>('store.emhttp.var.version');
|
||||
}
|
||||
|
||||
private async trackOsVersionUpgrade() {
|
||||
const currentOsVersion = this.configService.get<string>('store.emhttp.var.version');
|
||||
const lastSeenOsVersion = this.configService.get<string>('api.lastSeenOsVersion');
|
||||
async onApplicationShutdown() {
|
||||
if (!this.currentOsVersion) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentOsVersion && currentOsVersion !== lastSeenOsVersion) {
|
||||
this.configService.set('api.lastSeenOsVersion', currentOsVersion);
|
||||
const currentConfig = this.configService.get<ApiConfig>('api');
|
||||
if (currentConfig) {
|
||||
await this.persist(currentConfig);
|
||||
}
|
||||
const apiConfig = this.configService.get<ApiConfig>('api');
|
||||
if (apiConfig) {
|
||||
apiConfig.lastSeenOsVersion = this.currentOsVersion;
|
||||
await this.persist(apiConfig);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user