1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

refactor: proxy service scheduler (#5125)

This commit is contained in:
Mateusz Kwasniewski 2023-10-23 15:11:38 +02:00 committed by GitHub
parent 314a08b4e6
commit 1d1aa27ca3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 40 deletions

View File

@ -4,7 +4,12 @@ import { createTestConfig } from '../../test/config/test-config';
import FakeEventStore from '../../test/fixtures/fake-event-store';
import { randomId } from '../util/random-id';
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 { frontendSettingsKey } from '../../lib/types/settings/frontend-settings';
import { minutesToMilliseconds } from 'date-fns';
@ -55,7 +60,6 @@ test('corsOriginMiddleware origin validation', async () => {
userName,
),
).rejects.toThrow('Invalid origin: a');
proxyService.destroy();
});
test('corsOriginMiddleware without config', async () => {
@ -82,7 +86,6 @@ test('corsOriginMiddleware without config', async () => {
expect(await proxyService.getFrontendSettings(false)).toEqual({
frontendApiOrigins: [],
});
proxyService.destroy();
});
test('corsOriginMiddleware with config', async () => {
@ -109,12 +112,9 @@ test('corsOriginMiddleware with config', async () => {
expect(await proxyService.getFrontendSettings(false)).toEqual({
frontendApiOrigins: ['*'],
});
proxyService.destroy();
});
test('corsOriginMiddleware with caching enabled', async () => {
jest.useFakeTimers();
const { proxyService } = createSettingService([]);
const userName = randomId();
@ -133,24 +133,11 @@ test('corsOriginMiddleware with caching enabled', async () => {
frontendApiOrigins: [],
});
jest.advanceTimersByTime(minutesToMilliseconds(2));
await proxyService.fetchFrontendSettings(); // called by the scheduler service
jest.useRealTimers();
const settings = await proxyService.getFrontendSettings();
/*
This is needed because it is not enough to fake time to test the
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();
expect(settings).toEqual({
frontendApiOrigins: ['*'],
});
});

View File

@ -60,7 +60,6 @@ async function createApp(
metricsMonitor.stopMonitoring();
stores.clientInstanceStore.destroy();
services.clientMetricsServiceV2.destroy();
services.proxyService.destroy();
services.addonService.destroy();
await db.destroy();
};

View File

@ -118,6 +118,7 @@ export const scheduleServices = async (
featureToggleService,
versionService,
lastSeenService,
proxyService,
} = services;
if (await maintenanceService.isMaintenanceMode()) {
@ -205,6 +206,12 @@ export const scheduleServices = async (
hoursToMilliseconds(48),
'checkLatestVersion',
);
schedulerService.schedule(
proxyService.fetchFrontendSettings.bind(proxyService),
minutesToMilliseconds(2),
'fetchFrontendSettings',
);
};
export const createServices = (

View File

@ -53,18 +53,11 @@ export class ProxyService {
private cachedFrontendSettings?: FrontendSettings;
private timer: NodeJS.Timeout | null;
constructor(config: Config, stores: Stores, services: Services) {
this.config = config;
this.logger = config.getLogger('services/proxy-service.ts');
this.stores = stores;
this.services = services;
this.timer = setInterval(
() => this.fetchFrontendSettings(),
minutesToMilliseconds(2),
).unref();
}
async getProxyFeatures(
@ -181,7 +174,7 @@ export class ProxyService {
);
}
private async fetchFrontendSettings(): Promise<FrontendSettings> {
async fetchFrontendSettings(): Promise<FrontendSettings> {
try {
this.cachedFrontendSettings =
await this.services.settingService.get(frontendSettingsKey, {
@ -201,11 +194,4 @@ export class ProxyService {
}
return this.fetchFrontendSettings();
}
destroy(): void {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
}
}