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';
|
2023-05-24 12:26:54 +02:00
|
|
|
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;
|
|
|
|
|
2023-05-24 12:26:54 +02:00
|
|
|
private schedulerService: SchedulerService;
|
|
|
|
|
2022-12-21 12:23:44 +01:00
|
|
|
constructor(
|
|
|
|
{
|
|
|
|
patStore,
|
|
|
|
eventStore,
|
|
|
|
}: Pick<IUnleashStores, 'patStore' | 'eventStore'>,
|
|
|
|
config: IUnleashConfig,
|
|
|
|
settingService: SettingService,
|
2023-05-24 12:26:54 +02:00
|
|
|
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;
|
2023-05-24 12:26:54 +02:00
|
|
|
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> {
|
2023-05-24 12:26:54 +02:00
|
|
|
if (setting.enabled) {
|
|
|
|
this.schedulerService.pause();
|
2023-08-15 15:58:36 +02:00
|
|
|
} else if (!this.config.disableScheduler) {
|
2023-05-24 12:26:54 +02:00
|
|
|
this.schedulerService.resume();
|
|
|
|
}
|
2022-12-21 12:23:44 +01:00
|
|
|
return this.settingService.insert(
|
|
|
|
maintenanceSettingsKey,
|
|
|
|
setting,
|
|
|
|
user,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MaintenanceService;
|