1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/services/maintenance-service.ts

73 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-12-21 12:23:44 +01:00
import { IUnleashConfig, IUnleashStores } from '../types';
import { Logger } from '../logger';
import { IPatStore } from '../types/stores/pat-store';
import { IEventStore } from '../types/stores/event-store';
import SettingService from './setting-service';
import { maintenanceSettingsKey } from '../types/settings/maintenance-settings';
import { MaintenanceSchema } from '../openapi/spec/maintenance-schema';
import { SchedulerService } from './scheduler-service';
2022-12-21 12:23:44 +01:00
export default class MaintenanceService {
private config: IUnleashConfig;
private logger: Logger;
private patStore: IPatStore;
private eventStore: IEventStore;
private settingService: SettingService;
private schedulerService: SchedulerService;
2022-12-21 12:23:44 +01:00
constructor(
{
patStore,
eventStore,
}: Pick<IUnleashStores, 'patStore' | 'eventStore'>,
config: IUnleashConfig,
settingService: SettingService,
schedulerService: SchedulerService,
2022-12-21 12:23:44 +01:00
) {
this.config = config;
this.logger = config.getLogger('services/pat-service.ts');
this.patStore = patStore;
this.eventStore = eventStore;
this.settingService = settingService;
this.schedulerService = schedulerService;
2022-12-21 12:23:44 +01:00
}
async isMaintenanceMode(): Promise<boolean> {
return (
this.config.flagResolver.isEnabled('maintenanceMode') ||
(await this.getMaintenanceSetting()).enabled
);
}
async getMaintenanceSetting(): Promise<MaintenanceSchema> {
return (
(await this.settingService.get(maintenanceSettingsKey)) || {
enabled: false,
}
);
}
async toggleMaintenanceMode(
setting: MaintenanceSchema,
user: string,
): Promise<void> {
if (setting.enabled) {
this.schedulerService.pause();
} else {
this.schedulerService.resume();
}
2022-12-21 12:23:44 +01:00
return this.settingService.insert(
maintenanceSettingsKey,
setting,
user,
);
}
}
module.exports = MaintenanceService;