From a34c3ea200f8b9ed154105f842b3531491304675 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 19 May 2025 04:39:38 +0000 Subject: [PATCH] feat(server): auto index all workspaces to indexer (#12205) close CLOUD-207 ## Summary by CodeRabbit - **New Features** - Introduced automated periodic indexing of workspaces with a new job type and a scheduled cron job running every 30 seconds. - Added a unique sequential identifier (`sid`) and an "indexed" flag to workspaces to track indexing status. - **Improvements** - Enhanced workspace indexing to handle missing workspaces and snapshots distinctly and selectively index documents. - Added ability to query workspaces after a given identifier with result limits. - **Bug Fixes** - Improved error handling and logging during workspace indexing operations. - **Tests** - Expanded test coverage for workspace indexing and auto-indexing, including scheduling and edge cases. - **Chores** - Updated data models and schema to support new workspace fields and indexing features. - Enhanced mock data utilities to allow custom timestamps. - Improved type safety and flexibility in document snapshot retrieval. --- .../migration.sql | 14 +++ packages/backend/server/schema.prisma | 3 + .../src/__tests__/mocks/doc-snapshot.mock.ts | 3 +- .../server/src/base/job/queue/executor.ts | 7 +- packages/backend/server/src/models/doc.ts | 11 ++- .../backend/server/src/models/workspace.ts | 15 ++- .../src/plugins/indexer/__tests__/job.spec.ts | 76 +++++++++++++++ .../backend/server/src/plugins/indexer/job.ts | 95 +++++++++++++++++-- 8 files changed, 211 insertions(+), 13 deletions(-) create mode 100644 packages/backend/server/migrations/20250509070728_add_workspace_sid_and_indexed/migration.sql diff --git a/packages/backend/server/migrations/20250509070728_add_workspace_sid_and_indexed/migration.sql b/packages/backend/server/migrations/20250509070728_add_workspace_sid_and_indexed/migration.sql new file mode 100644 index 000000000..43d84e34b --- /dev/null +++ b/packages/backend/server/migrations/20250509070728_add_workspace_sid_and_indexed/migration.sql @@ -0,0 +1,14 @@ +/* + Warnings: + + - A unique constraint covering the columns `[sid]` on the table `workspaces` will be added. If there are existing duplicate values, this will fail. + +*/ +-- AlterTable +ALTER TABLE "workspaces" ADD COLUMN "sid" INT GENERATED BY DEFAULT AS IDENTITY; + +-- CreateIndex +CREATE UNIQUE INDEX "workspaces_sid_key" ON "workspaces"("sid"); + +-- AlterTable +ALTER TABLE "workspaces" ADD COLUMN "indexed" BOOLEAN NOT NULL DEFAULT false; diff --git a/packages/backend/server/schema.prisma b/packages/backend/server/schema.prisma index 166f28941..ad4da0ad5 100644 --- a/packages/backend/server/schema.prisma +++ b/packages/backend/server/schema.prisma @@ -106,6 +106,8 @@ model VerificationToken { } model Workspace { + // NOTE: manually set this column type to identity in migration file + sid Int @unique @default(autoincrement()) id String @id @default(uuid()) @db.VarChar public Boolean createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) @@ -115,6 +117,7 @@ model Workspace { enableDocEmbedding Boolean @default(true) @map("enable_doc_embedding") name String? @db.VarChar avatarKey String? @map("avatar_key") @db.VarChar + indexed Boolean @default(false) features WorkspaceFeature[] docs WorkspaceDoc[] diff --git a/packages/backend/server/src/__tests__/mocks/doc-snapshot.mock.ts b/packages/backend/server/src/__tests__/mocks/doc-snapshot.mock.ts index 16a1bbb1a..ac8ae5295 100644 --- a/packages/backend/server/src/__tests__/mocks/doc-snapshot.mock.ts +++ b/packages/backend/server/src/__tests__/mocks/doc-snapshot.mock.ts @@ -11,6 +11,7 @@ export type MockDocSnapshotInput = { workspaceId: string; docId?: string; blob?: Uint8Array; + updatedAt?: Date; }; export type MockedDocSnapshot = Snapshot; @@ -32,7 +33,7 @@ export class MockDocSnapshot extends Mocker< workspaceId: input.workspaceId, blob: input.blob, createdAt: new Date(), - updatedAt: new Date(), + updatedAt: input.updatedAt ?? new Date(), createdBy: input.user.id, updatedBy: input.user.id, }, diff --git a/packages/backend/server/src/base/job/queue/executor.ts b/packages/backend/server/src/base/job/queue/executor.ts index 71e8e2e69..183c7fe2d 100644 --- a/packages/backend/server/src/base/job/queue/executor.ts +++ b/packages/backend/server/src/base/job/queue/executor.ts @@ -161,7 +161,12 @@ export class JobExecutor implements OnModuleDestroy { async handleJobReturn(job: Job, result: JOB_SIGNAL) { if (result === JOB_SIGNAL.Repeat || result === JOB_SIGNAL.Retry) { try { - await this.getQueue(job.name).add(job.name, job.data, job.opts); + await this.getQueue(namespace(job.name as JobName)).add( + job.name, + job.data, + job.opts + ); + this.logger.debug(`Added job [${job.name}] to queue, signal=${result}`); } catch (e) { this.logger.error(`Failed to add job [${job.name}]`, e); } diff --git a/packages/backend/server/src/models/doc.ts b/packages/backend/server/src/models/doc.ts index 148a9e0d8..8edc30249 100644 --- a/packages/backend/server/src/models/doc.ts +++ b/packages/backend/server/src/models/doc.ts @@ -171,15 +171,20 @@ export class DocModel extends BaseModel { }; } - async getSnapshot(workspaceId: string, docId: string) { - return await this.db.snapshot.findUnique({ + async getSnapshot