From 553c5c4927eddcd49839a55e054e18a28f04773f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Tue, 19 Aug 2025 17:46:14 +0200 Subject: [PATCH] bug fixes: only use environment in events with environment --- src/lib/features/events/event-store.ts | 43 ++++++++++--------- .../configuration-revision-service.ts | 14 +++--- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/lib/features/events/event-store.ts b/src/lib/features/events/event-store.ts index ed49723d70..c19dff5bb3 100644 --- a/src/lib/features/events/event-store.ts +++ b/src/lib/features/events/event-store.ts @@ -191,21 +191,26 @@ export class EventStore implements IEventStore { } } - private typeIsInteresting = (builder: Knex.QueryBuilder) => - builder - .andWhere((inner) => - inner - .whereNotNull('feature_name') - .whereNotIn('type', [FEATURE_CREATED, FEATURE_TAGGED]) - .whereNot('type', 'LIKE', 'change-%'), - ) - .orWhereIn('type', [ - SEGMENT_UPDATED, - FEATURE_IMPORT, - FEATURES_IMPORTED, - SEGMENT_CREATED, - SEGMENT_DELETED, - ]); + private typeIsInteresting = + (environment?: string) => (builder: Knex.QueryBuilder) => + builder + .andWhere((inner) => { + inner + .whereNotNull('feature_name') + .whereNotIn('type', [FEATURE_CREATED, FEATURE_TAGGED]) + .whereNot('type', 'LIKE', 'change-%'); + if (environment) { + inner.where('environment', environment); + } + return inner; + }) + .orWhereIn('type', [ + SEGMENT_UPDATED, + FEATURE_IMPORT, + FEATURES_IMPORTED, + SEGMENT_CREATED, + SEGMENT_DELETED, + ]); async getMaxRevisionId( largerThan: number = 0, @@ -214,13 +219,9 @@ export class EventStore implements IEventStore { const stopTimer = this.metricTimer('getMaxRevisionId'); const query = this.db(TABLE) .max('id') - .where(this.typeIsInteresting) + .where(this.typeIsInteresting(environment)) .andWhere('id', '>=', largerThan); - if (environment) { - query.where('environment', environment); - } - const row = await query.first(); stopTimer(); return row?.max ?? 0; @@ -233,7 +234,7 @@ export class EventStore implements IEventStore { .from(TABLE) .where('id', '>', start) .andWhere('id', '<=', end) - .andWhere(this.typeIsInteresting) + .andWhere(this.typeIsInteresting()) .orderBy('id', 'asc'); const rows = await query; diff --git a/src/lib/features/feature-toggle/configuration-revision-service.ts b/src/lib/features/feature-toggle/configuration-revision-service.ts index 7409101006..85897a7b1e 100644 --- a/src/lib/features/feature-toggle/configuration-revision-service.ts +++ b/src/lib/features/feature-toggle/configuration-revision-service.ts @@ -18,7 +18,7 @@ export default class ConfigurationRevisionService extends EventEmitter { private revisionId: number; - private maxRevisionId: Record = {}; + private maxRevisionId: Map = new Map(); private flagResolver: IFlagResolver; @@ -73,10 +73,10 @@ export default class ConfigurationRevisionService extends EventEmitter { async updateMaxEnvironmentRevisionId(environment: string): Promise { const envRevisionId = await this.eventStore.getMaxRevisionId( - this.revisionId, + this.maxRevisionId[environment], environment, ); - if (this.maxRevisionId[environment] < envRevisionId) { + if (this.maxRevisionId[environment] ?? 0 < envRevisionId) { this.maxRevisionId[environment] = envRevisionId; } @@ -93,8 +93,12 @@ export default class ConfigurationRevisionService extends EventEmitter { ); if (this.revisionId !== revisionId) { this.logger.debug( - 'Updating feature configuration with new revision Id', - revisionId, + `Updating feature configuration with new revision Id ${revisionId} and all envs: ${Object.keys(this.maxRevisionId).join(', ')}`, + ); + await Promise.allSettled( + Object.keys(this.maxRevisionId).map((environment) => + this.updateMaxEnvironmentRevisionId(environment), + ), ); this.revisionId = revisionId; if (emit) {