mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-10 01:16:39 +02:00
refactor: proxy service scheduler (#5125)
This commit is contained in:
parent
314a08b4e6
commit
1d1aa27ca3
@ -4,7 +4,12 @@ import { createTestConfig } from '../../test/config/test-config';
|
|||||||
import FakeEventStore from '../../test/fixtures/fake-event-store';
|
import FakeEventStore from '../../test/fixtures/fake-event-store';
|
||||||
import { randomId } from '../util/random-id';
|
import { randomId } from '../util/random-id';
|
||||||
import FakeProjectStore from '../../test/fixtures/fake-project-store';
|
import FakeProjectStore from '../../test/fixtures/fake-project-store';
|
||||||
import { EventService, ProxyService, SettingService } from '../../lib/services';
|
import {
|
||||||
|
EventService,
|
||||||
|
ProxyService,
|
||||||
|
SchedulerService,
|
||||||
|
SettingService,
|
||||||
|
} from '../../lib/services';
|
||||||
import { ISettingStore } from '../../lib/types';
|
import { ISettingStore } from '../../lib/types';
|
||||||
import { frontendSettingsKey } from '../../lib/types/settings/frontend-settings';
|
import { frontendSettingsKey } from '../../lib/types/settings/frontend-settings';
|
||||||
import { minutesToMilliseconds } from 'date-fns';
|
import { minutesToMilliseconds } from 'date-fns';
|
||||||
@ -55,7 +60,6 @@ test('corsOriginMiddleware origin validation', async () => {
|
|||||||
userName,
|
userName,
|
||||||
),
|
),
|
||||||
).rejects.toThrow('Invalid origin: a');
|
).rejects.toThrow('Invalid origin: a');
|
||||||
proxyService.destroy();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('corsOriginMiddleware without config', async () => {
|
test('corsOriginMiddleware without config', async () => {
|
||||||
@ -82,7 +86,6 @@ test('corsOriginMiddleware without config', async () => {
|
|||||||
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
||||||
frontendApiOrigins: [],
|
frontendApiOrigins: [],
|
||||||
});
|
});
|
||||||
proxyService.destroy();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('corsOriginMiddleware with config', async () => {
|
test('corsOriginMiddleware with config', async () => {
|
||||||
@ -109,12 +112,9 @@ test('corsOriginMiddleware with config', async () => {
|
|||||||
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
expect(await proxyService.getFrontendSettings(false)).toEqual({
|
||||||
frontendApiOrigins: ['*'],
|
frontendApiOrigins: ['*'],
|
||||||
});
|
});
|
||||||
proxyService.destroy();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('corsOriginMiddleware with caching enabled', async () => {
|
test('corsOriginMiddleware with caching enabled', async () => {
|
||||||
jest.useFakeTimers();
|
|
||||||
|
|
||||||
const { proxyService } = createSettingService([]);
|
const { proxyService } = createSettingService([]);
|
||||||
|
|
||||||
const userName = randomId();
|
const userName = randomId();
|
||||||
@ -133,24 +133,11 @@ test('corsOriginMiddleware with caching enabled', async () => {
|
|||||||
frontendApiOrigins: [],
|
frontendApiOrigins: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
jest.advanceTimersByTime(minutesToMilliseconds(2));
|
await proxyService.fetchFrontendSettings(); // called by the scheduler service
|
||||||
|
|
||||||
jest.useRealTimers();
|
const settings = await proxyService.getFrontendSettings();
|
||||||
|
|
||||||
/*
|
expect(settings).toEqual({
|
||||||
This is needed because it is not enough to fake time to test the
|
frontendApiOrigins: ['*'],
|
||||||
updated cache, we also need to make sure that all promises are
|
});
|
||||||
executed and completed, in the right order.
|
|
||||||
*/
|
|
||||||
await new Promise<void>((resolve) =>
|
|
||||||
process.nextTick(async () => {
|
|
||||||
const settings = await proxyService.getFrontendSettings();
|
|
||||||
|
|
||||||
expect(settings).toEqual({
|
|
||||||
frontendApiOrigins: ['*'],
|
|
||||||
});
|
|
||||||
resolve();
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
proxyService.destroy();
|
|
||||||
});
|
});
|
||||||
|
@ -60,7 +60,6 @@ async function createApp(
|
|||||||
metricsMonitor.stopMonitoring();
|
metricsMonitor.stopMonitoring();
|
||||||
stores.clientInstanceStore.destroy();
|
stores.clientInstanceStore.destroy();
|
||||||
services.clientMetricsServiceV2.destroy();
|
services.clientMetricsServiceV2.destroy();
|
||||||
services.proxyService.destroy();
|
|
||||||
services.addonService.destroy();
|
services.addonService.destroy();
|
||||||
await db.destroy();
|
await db.destroy();
|
||||||
};
|
};
|
||||||
|
@ -118,6 +118,7 @@ export const scheduleServices = async (
|
|||||||
featureToggleService,
|
featureToggleService,
|
||||||
versionService,
|
versionService,
|
||||||
lastSeenService,
|
lastSeenService,
|
||||||
|
proxyService,
|
||||||
} = services;
|
} = services;
|
||||||
|
|
||||||
if (await maintenanceService.isMaintenanceMode()) {
|
if (await maintenanceService.isMaintenanceMode()) {
|
||||||
@ -205,6 +206,12 @@ export const scheduleServices = async (
|
|||||||
hoursToMilliseconds(48),
|
hoursToMilliseconds(48),
|
||||||
'checkLatestVersion',
|
'checkLatestVersion',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
schedulerService.schedule(
|
||||||
|
proxyService.fetchFrontendSettings.bind(proxyService),
|
||||||
|
minutesToMilliseconds(2),
|
||||||
|
'fetchFrontendSettings',
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createServices = (
|
export const createServices = (
|
||||||
|
@ -53,18 +53,11 @@ export class ProxyService {
|
|||||||
|
|
||||||
private cachedFrontendSettings?: FrontendSettings;
|
private cachedFrontendSettings?: FrontendSettings;
|
||||||
|
|
||||||
private timer: NodeJS.Timeout | null;
|
|
||||||
|
|
||||||
constructor(config: Config, stores: Stores, services: Services) {
|
constructor(config: Config, stores: Stores, services: Services) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.logger = config.getLogger('services/proxy-service.ts');
|
this.logger = config.getLogger('services/proxy-service.ts');
|
||||||
this.stores = stores;
|
this.stores = stores;
|
||||||
this.services = services;
|
this.services = services;
|
||||||
|
|
||||||
this.timer = setInterval(
|
|
||||||
() => this.fetchFrontendSettings(),
|
|
||||||
minutesToMilliseconds(2),
|
|
||||||
).unref();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getProxyFeatures(
|
async getProxyFeatures(
|
||||||
@ -181,7 +174,7 @@ export class ProxyService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async fetchFrontendSettings(): Promise<FrontendSettings> {
|
async fetchFrontendSettings(): Promise<FrontendSettings> {
|
||||||
try {
|
try {
|
||||||
this.cachedFrontendSettings =
|
this.cachedFrontendSettings =
|
||||||
await this.services.settingService.get(frontendSettingsKey, {
|
await this.services.settingService.get(frontendSettingsKey, {
|
||||||
@ -201,11 +194,4 @@ export class ProxyService {
|
|||||||
}
|
}
|
||||||
return this.fetchFrontendSettings();
|
return this.fetchFrontendSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy(): void {
|
|
||||||
if (this.timer) {
|
|
||||||
clearInterval(this.timer);
|
|
||||||
this.timer = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user