From 1d1aa27ca34927b00791b413f60c6d1b293b30b1 Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Mon, 23 Oct 2023 15:11:38 +0200 Subject: [PATCH] refactor: proxy service scheduler (#5125) --- .../middleware/cors-origin-middleware.test.ts | 35 ++++++------------- src/lib/server-impl.ts | 1 - src/lib/services/index.ts | 7 ++++ src/lib/services/proxy-service.ts | 16 +-------- 4 files changed, 19 insertions(+), 40 deletions(-) diff --git a/src/lib/middleware/cors-origin-middleware.test.ts b/src/lib/middleware/cors-origin-middleware.test.ts index bc760eeabf..2e81ebdbda 100644 --- a/src/lib/middleware/cors-origin-middleware.test.ts +++ b/src/lib/middleware/cors-origin-middleware.test.ts @@ -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((resolve) => - process.nextTick(async () => { - const settings = await proxyService.getFrontendSettings(); - - expect(settings).toEqual({ - frontendApiOrigins: ['*'], - }); - resolve(); - }), - ); - proxyService.destroy(); + expect(settings).toEqual({ + frontendApiOrigins: ['*'], + }); }); diff --git a/src/lib/server-impl.ts b/src/lib/server-impl.ts index b705c142c1..bc90403645 100644 --- a/src/lib/server-impl.ts +++ b/src/lib/server-impl.ts @@ -60,7 +60,6 @@ async function createApp( metricsMonitor.stopMonitoring(); stores.clientInstanceStore.destroy(); services.clientMetricsServiceV2.destroy(); - services.proxyService.destroy(); services.addonService.destroy(); await db.destroy(); }; diff --git a/src/lib/services/index.ts b/src/lib/services/index.ts index b508e67bed..213093f9aa 100644 --- a/src/lib/services/index.ts +++ b/src/lib/services/index.ts @@ -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 = ( diff --git a/src/lib/services/proxy-service.ts b/src/lib/services/proxy-service.ts index 324b1de210..2e3bf55922 100644 --- a/src/lib/services/proxy-service.ts +++ b/src/lib/services/proxy-service.ts @@ -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 { + async fetchFrontendSettings(): Promise { 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; - } - } }