Skip to content

Commit

Permalink
refactor: proxy service scheduler (#5125)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Oct 23, 2023
1 parent 314a08b commit 1d1aa27
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 40 deletions.
35 changes: 11 additions & 24 deletions src/lib/middleware/cors-origin-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -55,7 +60,6 @@ test('corsOriginMiddleware origin validation', async () => {
userName,
),
).rejects.toThrow('Invalid origin: a');
proxyService.destroy();
});

test('corsOriginMiddleware without config', async () => {
Expand All @@ -82,7 +86,6 @@ test('corsOriginMiddleware without config', async () => {
expect(await proxyService.getFrontendSettings(false)).toEqual({
frontendApiOrigins: [],
});
proxyService.destroy();
});

test('corsOriginMiddleware with config', async () => {
Expand All @@ -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();
Expand All @@ -133,24 +133,11 @@ test('corsOriginMiddleware with caching enabled', async () => {
frontendApiOrigins: [],
});

jest.advanceTimersByTime(minutesToMilliseconds(2));

jest.useRealTimers();
await proxyService.fetchFrontendSettings(); // called by the scheduler service

/*
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();
const settings = await proxyService.getFrontendSettings();

expect(settings).toEqual({
frontendApiOrigins: ['*'],
});
resolve();
}),
);
proxyService.destroy();
expect(settings).toEqual({
frontendApiOrigins: ['*'],
});
});
1 change: 0 additions & 1 deletion src/lib/server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ async function createApp(
metricsMonitor.stopMonitoring();
stores.clientInstanceStore.destroy();
services.clientMetricsServiceV2.destroy();
services.proxyService.destroy();
services.addonService.destroy();
await db.destroy();
};
Expand Down
7 changes: 7 additions & 0 deletions src/lib/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const scheduleServices = async (
featureToggleService,
versionService,
lastSeenService,
proxyService,
} = services;

if (await maintenanceService.isMaintenanceMode()) {
Expand Down Expand Up @@ -205,6 +206,12 @@ export const scheduleServices = async (
hoursToMilliseconds(48),
'checkLatestVersion',
);

schedulerService.schedule(
proxyService.fetchFrontendSettings.bind(proxyService),
minutesToMilliseconds(2),
'fetchFrontendSettings',
);
};

export const createServices = (
Expand Down
16 changes: 1 addition & 15 deletions src/lib/services/proxy-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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, {
Expand All @@ -201,11 +194,4 @@ export class ProxyService {
}
return this.fetchFrontendSettings();
}

destroy(): void {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
}
}

0 comments on commit 1d1aa27

Please sign in to comment.