1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

refactor: maintenance read model (#5374)

https://linear.app/unleash/issue/2-1655/refactor-maintenance-to-feature-folder-and-add-a-read-model-for-status

This PR does 2 things:
 - Moves all "maintenance" files to a `maintenance` features folder
- Adds a `IMaintenanceStatus` read model that only includes
`isMaintenanceMode()`, so we can use this interface in SchedulerService
and expose only the `isMaintenanceMode()` method instead of the entire
`MaintenanceService`

Is this what you meant in
https://github.com/Unleash/unleash/pull/5363#discussion_r1400170835
@FredrikOseberg?
This commit is contained in:
Nuno Góis 2023-11-28 13:03:02 +00:00 committed by GitHub
parent 90539a4a63
commit fd601762b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 34 additions and 30 deletions

View File

@ -25,7 +25,7 @@ import { findPublicFolder } from './util/findPublicFolder';
import { conditionalMiddleware } from './middleware/conditional-middleware'; import { conditionalMiddleware } from './middleware/conditional-middleware';
import patMiddleware from './middleware/pat-middleware'; import patMiddleware from './middleware/pat-middleware';
import { Knex } from 'knex'; import { Knex } from 'knex';
import maintenanceMiddleware from './middleware/maintenance-middleware'; import maintenanceMiddleware from './features/maintenance/maintenance-middleware';
import { unless } from './middleware/unless-middleware'; import { unless } from './middleware/unless-middleware';
import { catchAllErrorHandler } from './middleware/catch-all-error-handler'; import { catchAllErrorHandler } from './middleware/catch-all-error-handler';
import NotFoundError from './error/notfound-error'; import NotFoundError from './error/notfound-error';

View File

@ -1,6 +1,6 @@
import { ADMIN, IUnleashConfig, IUnleashServices } from '../../types'; import { ADMIN, IUnleashConfig, IUnleashServices } from '../../types';
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import Controller from '../controller'; import Controller from '../../routes/controller';
import { Logger } from '../../logger'; import { Logger } from '../../logger';
import { import {
createRequestSchema, createRequestSchema,
@ -9,13 +9,13 @@ import {
getStandardResponses, getStandardResponses,
} from '../../openapi'; } from '../../openapi';
import { OpenApiService } from '../../services'; import { OpenApiService } from '../../services';
import { IAuthRequest } from '../unleash-types'; import { IAuthRequest } from '../../routes/unleash-types';
import { extractUsername } from '../../util'; import { extractUsername } from '../../util';
import { import {
MaintenanceSchema, MaintenanceSchema,
maintenanceSchema, maintenanceSchema,
} from '../../openapi/spec/maintenance-schema'; } from '../../openapi/spec/maintenance-schema';
import MaintenanceService from 'lib/services/maintenance-service'; import MaintenanceService from 'lib/features/maintenance/maintenance-service';
import { ToggleMaintenanceSchema } from 'lib/openapi/spec/toggle-maintenance-schema'; import { ToggleMaintenanceSchema } from 'lib/openapi/spec/toggle-maintenance-schema';
export default class MaintenanceController extends Controller { export default class MaintenanceController extends Controller {

View File

@ -1,6 +1,6 @@
import { IUnleashConfig } from '../types'; import { IUnleashConfig } from '../../types';
import MaintenanceService from '../services/maintenance-service'; import MaintenanceService from './maintenance-service';
import { IAuthRequest } from '../routes/unleash-types'; import { IAuthRequest } from '../../routes/unleash-types';
export const MAINTENANCE_MODE_ENABLED = export const MAINTENANCE_MODE_ENABLED =
'Unleash is currently in maintenance mode.'; 'Unleash is currently in maintenance mode.';

View File

@ -1,9 +1,9 @@
import { SchedulerService } from '../features/scheduler/scheduler-service'; import { SchedulerService } from '../scheduler/scheduler-service';
import MaintenanceService from './maintenance-service'; import MaintenanceService from './maintenance-service';
import SettingService from './setting-service'; import SettingService from '../../services/setting-service';
import { createTestConfig } from '../../test/config/test-config'; import { createTestConfig } from '../../../test/config/test-config';
import FakeSettingStore from '../../test/fixtures/fake-setting-store'; import FakeSettingStore from '../../../test/fixtures/fake-setting-store';
import EventService from './event-service'; import EventService from '../../services/event-service';
test('Scheduler should run scheduled functions if maintenance mode is off', async () => { test('Scheduler should run scheduled functions if maintenance mode is off', async () => {
const config = createTestConfig(); const config = createTestConfig();

View File

@ -1,10 +1,14 @@
import { IUnleashConfig } from '../types'; import { IUnleashConfig } from '../../types';
import { Logger } from '../logger'; import { Logger } from '../../logger';
import SettingService from './setting-service'; import SettingService from '../../services/setting-service';
import { maintenanceSettingsKey } from '../types/settings/maintenance-settings'; import { maintenanceSettingsKey } from '../../types/settings/maintenance-settings';
import { MaintenanceSchema } from '../openapi/spec/maintenance-schema'; import { MaintenanceSchema } from '../../openapi/spec/maintenance-schema';
export default class MaintenanceService { export interface IMaintenanceStatus {
isMaintenanceMode(): Promise<boolean>;
}
export default class MaintenanceService implements IMaintenanceStatus {
private config: IUnleashConfig; private config: IUnleashConfig;
private logger: Logger; private logger: Logger;

View File

@ -1,6 +1,6 @@
import { SchedulerService } from './scheduler-service'; import { SchedulerService } from './scheduler-service';
import { LogProvider, Logger } from '../../logger'; import { LogProvider } from '../../logger';
import MaintenanceService from '../../services/maintenance-service'; import MaintenanceService from '../maintenance/maintenance-service';
import { createTestConfig } from '../../../test/config/test-config'; import { createTestConfig } from '../../../test/config/test-config';
import SettingService from '../../services/setting-service'; import SettingService from '../../services/setting-service';
import FakeSettingStore from '../../../test/fixtures/fake-setting-store'; import FakeSettingStore from '../../../test/fixtures/fake-setting-store';

View File

@ -1,6 +1,6 @@
import EventEmitter from 'events'; import EventEmitter from 'events';
import { Logger, LogProvider } from '../../logger'; import { Logger, LogProvider } from '../../logger';
import MaintenanceService from '../../services/maintenance-service'; import { IMaintenanceStatus } from '../maintenance/maintenance-service';
import { SCHEDULER_JOB_TIME } from '../../metric-events'; import { SCHEDULER_JOB_TIME } from '../../metric-events';
export class SchedulerService { export class SchedulerService {
@ -8,17 +8,17 @@ export class SchedulerService {
private logger: Logger; private logger: Logger;
private maintenanceService: MaintenanceService; private maintenanceStatus: IMaintenanceStatus;
private eventBus: EventEmitter; private eventBus: EventEmitter;
constructor( constructor(
getLogger: LogProvider, getLogger: LogProvider,
maintenanceService: MaintenanceService, maintenanceStatus: IMaintenanceStatus,
eventBus: EventEmitter, eventBus: EventEmitter,
) { ) {
this.logger = getLogger('/services/scheduler-service.ts'); this.logger = getLogger('/services/scheduler-service.ts');
this.maintenanceService = maintenanceService; this.maintenanceStatus = maintenanceStatus;
this.eventBus = eventBus; this.eventBus = eventBus;
} }
@ -46,7 +46,7 @@ export class SchedulerService {
setInterval(async () => { setInterval(async () => {
try { try {
const maintenanceMode = const maintenanceMode =
await this.maintenanceService.isMaintenanceMode(); await this.maintenanceStatus.isMaintenanceMode();
if (!maintenanceMode) { if (!maintenanceMode) {
await runScheduledFunctionWithEvent(); await runScheduledFunctionWithEvent();
} }
@ -59,7 +59,7 @@ export class SchedulerService {
); );
try { try {
const maintenanceMode = const maintenanceMode =
await this.maintenanceService.isMaintenanceMode(); await this.maintenanceStatus.isMaintenanceMode();
if (!maintenanceMode) { if (!maintenanceMode) {
await runScheduledFunctionWithEvent(); await runScheduledFunctionWithEvent();
} }

View File

@ -25,7 +25,7 @@ import NotFoundError from '../../error/notfound-error';
import { SetUiConfigSchema } from '../../openapi/spec/set-ui-config-schema'; import { SetUiConfigSchema } from '../../openapi/spec/set-ui-config-schema';
import { createRequestSchema } from '../../openapi/util/create-request-schema'; import { createRequestSchema } from '../../openapi/util/create-request-schema';
import { ProxyService } from 'lib/services'; import { ProxyService } from 'lib/services';
import MaintenanceService from 'lib/services/maintenance-service'; import MaintenanceService from 'lib/features/maintenance/maintenance-service';
class ConfigController extends Controller { class ConfigController extends Controller {
private versionService: VersionService; private versionService: VersionService;

View File

@ -28,7 +28,7 @@ import { PublicSignupController } from './public-signup';
import InstanceAdminController from './instance-admin'; import InstanceAdminController from './instance-admin';
import TelemetryController from './telemetry'; import TelemetryController from './telemetry';
import FavoritesController from './favorites'; import FavoritesController from './favorites';
import MaintenanceController from './maintenance'; import MaintenanceController from '../../features/maintenance/maintenance-controller';
import { createKnexTransactionStarter } from '../../db/transaction'; import { createKnexTransactionStarter } from '../../db/transaction';
import { Db } from '../../db/db'; import { Db } from '../../db/db';
import ExportImportController from '../../features/export-import-toggles/export-import-controller'; import ExportImportController from '../../features/export-import-toggles/export-import-controller';

View File

@ -38,7 +38,7 @@ import { PublicSignupTokenService } from './public-signup-token-service';
import { LastSeenService } from './client-metrics/last-seen/last-seen-service'; import { LastSeenService } from './client-metrics/last-seen/last-seen-service';
import { InstanceStatsService } from '../features/instance-stats/instance-stats-service'; import { InstanceStatsService } from '../features/instance-stats/instance-stats-service';
import { FavoritesService } from './favorites-service'; import { FavoritesService } from './favorites-service';
import MaintenanceService from './maintenance-service'; import MaintenanceService from '../features/maintenance/maintenance-service';
import { AccountService } from './account-service'; import { AccountService } from './account-service';
import { SchedulerService } from '../features/scheduler/scheduler-service'; import { SchedulerService } from '../features/scheduler/scheduler-service';
import { Knex } from 'knex'; import { Knex } from 'knex';

View File

@ -35,7 +35,7 @@ import { PublicSignupTokenService } from '../services/public-signup-token-servic
import { LastSeenService } from '../services/client-metrics/last-seen/last-seen-service'; import { LastSeenService } from '../services/client-metrics/last-seen/last-seen-service';
import { InstanceStatsService } from '../features/instance-stats/instance-stats-service'; import { InstanceStatsService } from '../features/instance-stats/instance-stats-service';
import { FavoritesService } from '../services/favorites-service'; import { FavoritesService } from '../services/favorites-service';
import MaintenanceService from '../services/maintenance-service'; import MaintenanceService from '../features/maintenance/maintenance-service';
import { AccountService } from '../services/account-service'; import { AccountService } from '../services/account-service';
import { SchedulerService } from '../features/scheduler/scheduler-service'; import { SchedulerService } from '../features/scheduler/scheduler-service';
import { Knex } from 'knex'; import { Knex } from 'knex';