Improve datasource creation resilience to missing cache
This commit is contained in:
@ -28,11 +28,45 @@ export class WorkspaceDatasourceFactory {
|
|||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
cacheVersion: string | null,
|
cacheVersion: string | null,
|
||||||
): Promise<WorkspaceDataSource> {
|
): Promise<WorkspaceDataSource> {
|
||||||
cacheVersion ??=
|
const desiredCacheVersion =
|
||||||
|
cacheVersion ??
|
||||||
|
(await this.workspaceCacheVersionService.getVersion(workspaceId));
|
||||||
|
|
||||||
|
if (!desiredCacheVersion) {
|
||||||
|
throw new Error('Cache version not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const latestCacheVersion =
|
||||||
await this.workspaceCacheVersionService.getVersion(workspaceId);
|
await this.workspaceCacheVersionService.getVersion(workspaceId);
|
||||||
|
|
||||||
if (!cacheVersion) {
|
if (latestCacheVersion !== desiredCacheVersion) {
|
||||||
throw new Error('Cache version not found');
|
throw new Error('Cache version mismatch');
|
||||||
|
}
|
||||||
|
|
||||||
|
let cachedObjectMetadataCollection =
|
||||||
|
await this.workspaceCacheStorageService.getObjectMetadataCollection(
|
||||||
|
workspaceId,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!cachedObjectMetadataCollection) {
|
||||||
|
const freshObjectMetadataCollection =
|
||||||
|
await this.objectMetadataRepository.find({
|
||||||
|
where: { workspaceId },
|
||||||
|
relations: [
|
||||||
|
'fields.object',
|
||||||
|
'fields',
|
||||||
|
'fields.fromRelationMetadata',
|
||||||
|
'fields.toRelationMetadata',
|
||||||
|
'fields.fromRelationMetadata.toObjectMetadata',
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.workspaceCacheStorageService.setObjectMetadataCollection(
|
||||||
|
workspaceId,
|
||||||
|
freshObjectMetadataCollection,
|
||||||
|
);
|
||||||
|
|
||||||
|
cachedObjectMetadataCollection = freshObjectMetadataCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
const workspaceDataSource = await workspaceDataSourceCacheInstance.execute(
|
const workspaceDataSource = await workspaceDataSourceCacheInstance.execute(
|
||||||
@ -47,38 +81,12 @@ export class WorkspaceDatasourceFactory {
|
|||||||
throw new Error('Data source metadata not found');
|
throw new Error('Data source metadata not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const latestCacheVersion =
|
if (!cachedObjectMetadataCollection) {
|
||||||
await this.workspaceCacheVersionService.getVersion(workspaceId);
|
throw new Error('Object metadata collection not found');
|
||||||
|
|
||||||
if (latestCacheVersion !== cacheVersion) {
|
|
||||||
throw new Error('Cache version mismatch');
|
|
||||||
}
|
|
||||||
|
|
||||||
let objectMetadataCollection =
|
|
||||||
await this.workspaceCacheStorageService.getObjectMetadataCollection(
|
|
||||||
workspaceId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!objectMetadataCollection) {
|
|
||||||
objectMetadataCollection = await this.objectMetadataRepository.find({
|
|
||||||
where: { workspaceId },
|
|
||||||
relations: [
|
|
||||||
'fields.object',
|
|
||||||
'fields',
|
|
||||||
'fields.fromRelationMetadata',
|
|
||||||
'fields.toRelationMetadata',
|
|
||||||
'fields.fromRelationMetadata.toObjectMetadata',
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.workspaceCacheStorageService.setObjectMetadataCollection(
|
|
||||||
workspaceId,
|
|
||||||
objectMetadataCollection,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const entities = await Promise.all(
|
const entities = await Promise.all(
|
||||||
objectMetadataCollection.map((objectMetadata) =>
|
cachedObjectMetadataCollection.map((objectMetadata) =>
|
||||||
this.entitySchemaFactory.create(workspaceId, objectMetadata),
|
this.entitySchemaFactory.create(workspaceId, objectMetadata),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@ -634,7 +634,7 @@ export class WorkspaceRepository<
|
|||||||
);
|
);
|
||||||
|
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Object metadata for object "${objectMetadataName}" is missing` +
|
`Object metadata for object "${objectMetadataName}" is missing ` +
|
||||||
`in workspace "${this.internalContext.workspaceId}" ` +
|
`in workspace "${this.internalContext.workspaceId}" ` +
|
||||||
`with object metadata collection length: ${objectMetadataCollection?.length}`,
|
`with object metadata collection length: ${objectMetadataCollection?.length}`,
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
|
||||||
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service';
|
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service';
|
||||||
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator';
|
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator';
|
||||||
@ -8,6 +8,8 @@ import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/worksp
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class WorkspaceCacheStorageService {
|
export class WorkspaceCacheStorageService {
|
||||||
|
private readonly logger = new Logger(WorkspaceCacheStorageService.name);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@InjectCacheStorage(CacheStorageNamespace.WorkspaceSchema)
|
@InjectCacheStorage(CacheStorageNamespace.WorkspaceSchema)
|
||||||
private readonly workspaceSchemaCache: CacheStorageService,
|
private readonly workspaceSchemaCache: CacheStorageService,
|
||||||
@ -24,7 +26,11 @@ export class WorkspaceCacheStorageService {
|
|||||||
await this.workspaceCacheVersionService.getVersion(workspaceId);
|
await this.workspaceCacheVersionService.getVersion(workspaceId);
|
||||||
|
|
||||||
if (!latestVersion || currentVersion !== latestVersion) {
|
if (!latestVersion || currentVersion !== latestVersion) {
|
||||||
// Invalidate cache if version mismatch is detected
|
// Invalidate cache if version mismatch is detected"
|
||||||
|
this.logger.log(
|
||||||
|
`Cache version mismatch detected for workspace ${workspaceId}. Current version: ${currentVersion}. Latest version: ${latestVersion}. Invalidating cache...`,
|
||||||
|
);
|
||||||
|
|
||||||
await this.invalidateCache(workspaceId);
|
await this.invalidateCache(workspaceId);
|
||||||
|
|
||||||
// If the latest version is not found, increment the version
|
// If the latest version is not found, increment the version
|
||||||
|
|||||||
Reference in New Issue
Block a user