Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client instance service #5126

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: proxy service scheduler
kwasniew committed Oct 23, 2023
commit c5301d1a4947671aeb8e7fa4c4a137d91badb5a1
35 changes: 11 additions & 24 deletions src/lib/middleware/cors-origin-middleware.test.ts
Original file line number Diff line number Diff line change
@@ -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));

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
@@ -60,7 +60,6 @@ async function createApp(
metricsMonitor.stopMonitoring();
stores.clientInstanceStore.destroy();
services.clientMetricsServiceV2.destroy();
services.proxyService.destroy();
services.addonService.destroy();
await db.destroy();
};
7 changes: 7 additions & 0 deletions src/lib/services/index.ts
Original file line number Diff line number Diff line change
@@ -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 = (
16 changes: 1 addition & 15 deletions src/lib/services/proxy-service.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
}