Refactor runner dynamic cache invalidation (#16913)

# Introduction
closes https://github.com/twentyhq/core-team-issues/issues/1792
Refactoring the cache to invalidate to be more precise and prevent any
corrupted cache occurrences

## Next
The load dependency cache from the workspace migration, that's not
optimal it should be smart enough to infer it from the actions
themselves. For the moment their definition isn't granular enough and
inferring such info would be very dirty
This commit is contained in:
Paul Rastoin
2026-01-03 02:51:07 +01:00
committed by GitHub
parent 42c9ae1ebc
commit 9b38254256
8 changed files with 252 additions and 11 deletions
@@ -6,5 +6,6 @@ export type WorkspaceMigrationV2<
> = {
actions: TActions[];
workspaceId: string;
// TODO remove from workspaceMigration once we've refactored the actions to have metadata and action type grain
relatedFlatEntityMapsKeys?: (keyof AllFlatEntityMaps)[];
};
@@ -10,6 +10,9 @@ import {
import { LoggerService } from 'src/engine/core-modules/logger/logger.service';
import { WorkspaceManyOrAllFlatEntityMapsCacheService } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.service';
import { AllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/all-flat-entity-maps.type';
import { getMetadataFlatEntityMapsKey } from 'src/engine/metadata-modules/flat-entity/utils/get-metadata-flat-entity-maps-key.util';
import { getMetadataNameFromFlatEntityMapsKey } from 'src/engine/metadata-modules/flat-entity/utils/get-metadata-name-from-flat-entity-maps-key.util';
import { getMetadataRelatedMetadataNames } from 'src/engine/metadata-modules/flat-entity/utils/get-metadata-related-metadata-names.util';
import { FIND_ALL_CORE_VIEWS_GRAPHQL_OPERATION } from 'src/engine/metadata-modules/view/constants/find-all-core-views-graphql-operation.constant';
import { WorkspaceMetadataVersionService } from 'src/engine/metadata-modules/workspace-metadata-version/services/workspace-metadata-version.service';
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';
@@ -33,7 +36,7 @@ export class WorkspaceMigrationRunnerV2Service {
private getLegacyCacheInvalidationPromises({
workspaceMigration: { actions, workspaceId },
}: {
workspaceMigration: WorkspaceMigrationV2;
workspaceMigration: Omit<WorkspaceMigrationV2, 'relatedFlatEntityMapsKeys'>;
}): Promise<void>[] {
const asyncOperations: Promise<void>[] = [];
const shouldIncrementMetadataGraphqlSchemaVersion = actions.some(
@@ -178,8 +181,10 @@ export class WorkspaceMigrationRunnerV2Service {
) as (keyof AllFlatEntityMaps)[];
flatEntityMapsToInvalidate = [
...optimisticallyUpdatedFlatEntityMapsKeys,
...flatEntityMapsToInvalidate,
...new Set([
...optimisticallyUpdatedFlatEntityMapsKeys,
...flatEntityMapsToInvalidate,
]),
];
allFlatEntityMaps = {
@@ -195,7 +200,10 @@ export class WorkspaceMigrationRunnerV2Service {
const flatEntitiesCacheToInvalidate = [
...new Set([
...flatEntityMapsToInvalidate,
...(relatedFlatEntityMapsKeys ?? []),
...flatEntityMapsToInvalidate
.map(getMetadataNameFromFlatEntityMapsKey)
.flatMap(getMetadataRelatedMetadataNames)
.map(getMetadataFlatEntityMapsKey),
]),
];
@@ -207,18 +215,12 @@ export class WorkspaceMigrationRunnerV2Service {
const invalidationResults = await Promise.allSettled([
this.flatEntityMapsCacheService.invalidateFlatEntityMaps({
workspaceId,
flatMapsKeys: [
...new Set([
...flatEntityMapsToInvalidate,
...(relatedFlatEntityMapsKeys ?? []),
]),
],
flatMapsKeys: flatEntitiesCacheToInvalidate,
}),
...this.getLegacyCacheInvalidationPromises({
workspaceMigration: {
actions,
workspaceId,
relatedFlatEntityMapsKeys,
},
}),
]);