diff --git a/src/lib/features/maintenance/maintenance-service.ts b/src/lib/features/maintenance/maintenance-service.ts index 8748a0e1eec7..5e2fa6c881cc 100644 --- a/src/lib/features/maintenance/maintenance-service.ts +++ b/src/lib/features/maintenance/maintenance-service.ts @@ -1,8 +1,10 @@ +import memoizee from 'memoizee'; import { IUnleashConfig } from '../../types'; import { Logger } from '../../logger'; import SettingService from '../../services/setting-service'; import { maintenanceSettingsKey } from '../../types/settings/maintenance-settings'; import { MaintenanceSchema } from '../../openapi/spec/maintenance-schema'; +import { minutesToMilliseconds } from 'date-fns'; export interface IMaintenanceStatus { isMaintenanceMode(): Promise; @@ -15,20 +17,30 @@ export default class MaintenanceService implements IMaintenanceStatus { private settingService: SettingService; + private resolveMaintenance: () => Promise; + constructor(config: IUnleashConfig, settingService: SettingService) { this.config = config; this.logger = config.getLogger('services/maintenance-service.ts'); this.settingService = settingService; + this.resolveMaintenance = memoizee( + async () => (await this.getMaintenanceSetting()).enabled, + { + promise: true, + maxAge: minutesToMilliseconds(1), + }, + ); } async isMaintenanceMode(): Promise { return ( this.config.flagResolver.isEnabled('maintenanceMode') || - (await this.getMaintenanceSetting()).enabled + (await this.resolveMaintenance()) ); } async getMaintenanceSetting(): Promise { + this.logger.debug('getMaintenanceSetting called'); return ( (await this.settingService.get(maintenanceSettingsKey)) || { enabled: false, @@ -41,6 +53,8 @@ export default class MaintenanceService implements IMaintenanceStatus { user: string, toggledByUserId: number, ): Promise { + //@ts-ignore + this.resolveMaintenance.clear(); return this.settingService.insert( maintenanceSettingsKey, setting,