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

refactor: move metrics service scheduling #5129

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 1 addition & 5 deletions src/lib/db/client-instance-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export default class ClientInstanceStore implements IClientInstanceStore {

private metricTimer: Function;

private timer: Timeout;

constructor(db: Db, eventBus: EventEmitter, getLogger: LogProvider) {
this.db = db;
this.eventBus = eventBus;
Expand Down Expand Up @@ -197,7 +195,5 @@ export default class ClientInstanceStore implements IClientInstanceStore {
return this.db(TABLE).where('app_name', appName).del();
}

destroy(): void {
clearInterval(this.timer);
}
destroy(): void {}
}
2 changes: 0 additions & 2 deletions src/lib/server-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ async function createApp(
}
services.schedulerService.stop();
metricsMonitor.stopMonitoring();
stores.clientInstanceStore.destroy();
services.clientMetricsServiceV2.destroy();
services.addonService.destroy();
await db.destroy();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export type LastSeenInput = {
};

export class LastSeenService {
private timers: NodeJS.Timeout[] = [];

private lastSeenToggles: Map<String, LastSeenInput> = new Map();

private logger: Logger;
Expand Down Expand Up @@ -79,8 +77,4 @@ export class LastSeenService {
async cleanLastSeen() {
await this.lastSeenStore.cleanLastSeen();
}

destroy(): void {
this.timers.forEach(clearInterval);
}
}
22 changes: 3 additions & 19 deletions src/lib/services/client-metrics/metrics-service-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import { nameSchema } from '../../schema/feature-schema';
export default class ClientMetricsServiceV2 {
private config: IUnleashConfig;

private timers: NodeJS.Timeout[] = [];

private unsavedMetrics: IClientMetricsEnv[] = [];

private clientMetricsStoreV2: IClientMetricsStoreV2;
Expand All @@ -41,7 +39,6 @@ export default class ClientMetricsServiceV2 {
{ clientMetricsStoreV2 }: Pick<IUnleashStores, 'clientMetricsStoreV2'>,
config: IUnleashConfig,
lastSeenService: LastSeenService,
bulkInterval = secondsToMilliseconds(5),
) {
this.clientMetricsStoreV2 = clientMetricsStoreV2;
this.lastSeenService = lastSeenService;
Expand All @@ -50,18 +47,10 @@ export default class ClientMetricsServiceV2 {
'/services/client-metrics/client-metrics-service-v2.ts',
);
this.flagResolver = config.flagResolver;
}

this.timers.push(
setInterval(() => {
this.bulkAdd().catch(console.error);
}, bulkInterval).unref(),
);

this.timers.push(
setInterval(() => {
this.clientMetricsStoreV2.clearMetrics(48).catch(console.error);
}, hoursToMilliseconds(12)).unref(),
);
async clearMetrics(hoursAgo: number) {
return this.clientMetricsStoreV2.clearMetrics(hoursAgo);
}

async filterValidToggleNames(toggleNames: string[]): Promise<string[]> {
Expand Down Expand Up @@ -245,9 +234,4 @@ export default class ClientMetricsServiceV2 {
}
return 'default';
}

destroy(): void {
this.timers.forEach(clearInterval);
this.lastSeenService.destroy();
}
}
17 changes: 17 additions & 0 deletions src/lib/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const scheduleServices = async (
versionService,
lastSeenService,
proxyService,
clientMetricsServiceV2,
} = services;

if (await maintenanceService.isMaintenanceMode()) {
Expand Down Expand Up @@ -224,6 +225,22 @@ export const scheduleServices = async (
minutesToMilliseconds(2),
'fetchFrontendSettings',
);

schedulerService.schedule(
() => {
clientMetricsServiceV2.bulkAdd().catch(console.error);
},
secondsToMilliseconds(5),
'bulkAddMetrics',
);

schedulerService.schedule(
() => {
clientMetricsServiceV2.clearMetrics(48).catch(console.error);
},
hoursToMilliseconds(12),
'clearMetrics',
);
};

export const createServices = (
Expand Down
2 changes: 0 additions & 2 deletions src/test/e2e/helpers/database-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ export default async function init(
await setupDatabase(stores);
},
destroy: async () => {
const { clientInstanceStore } = stores;
return new Promise<void>((resolve, reject) => {
clientInstanceStore.destroy();
testDb.destroy((error) => (error ? reject(error) : resolve()));
});
},
Expand Down
6 changes: 0 additions & 6 deletions src/test/e2e/services/last-seen-service.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ test('Should update last seen for known toggles', async () => {
const t1 = await stores.featureToggleStore.get('ta1');

expect(t1.lastSeenAt.getTime()).toBeGreaterThan(time);

service.destroy();
});

test('Should not update last seen toggles with 0 metrics', async () => {
Expand Down Expand Up @@ -102,8 +100,6 @@ test('Should not update last seen toggles with 0 metrics', async () => {

expect(t2.lastSeenAt).toBeNull();
expect(t1.lastSeenAt.getTime()).toBeGreaterThanOrEqual(time);

service.destroy();
});

test('Should not update anything for 0 toggles', async () => {
Expand Down Expand Up @@ -144,6 +140,4 @@ test('Should not update anything for 0 toggles', async () => {
const count = await service.store();

expect(count).toBe(0);

service.destroy();
});
Loading