1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-23 00:22:19 +01:00

Fix/check maintenance flag once per minute (#6118)

## About the changes
Every schedule job will now check if maintenance is enabled. This ends
up querying the settings table in the db at least once per second per
running unleash instance. This small fix caches this query for 60
seconds to reduce the load somewhat.

We should reconsider this solution for the long term, but this will be a
great improvement on the short term.


**Logs after this fix running locally.** 
We can observe that we resolve settings from the DB once per minute. 

![image](https://github.com/Unleash/unleash/assets/158948/c313cf38-8d86-4b86-a0ba-4f4df60d50d6)


Also we should consider giving a warning in section where you enable
maintenance mode that it can take up to a minute to propagate.
This commit is contained in:
Ivar Conradi Østhus 2024-02-03 07:30:22 +01:00 committed by GitHub
parent 7b04db0547
commit c76c8f135a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<boolean>;
@ -15,20 +17,30 @@ export default class MaintenanceService implements IMaintenanceStatus {
private settingService: SettingService;
private resolveMaintenance: () => Promise<boolean>;
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<boolean> {
return (
this.config.flagResolver.isEnabled('maintenanceMode') ||
(await this.getMaintenanceSetting()).enabled
(await this.resolveMaintenance())
);
}
async getMaintenanceSetting(): Promise<MaintenanceSchema> {
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<void> {
//@ts-ignore
this.resolveMaintenance.clear();
return this.settingService.insert(
maintenanceSettingsKey,
setting,