Skip to content

Commit

Permalink
feat: support run document index tassk in background (#130)
Browse files Browse the repository at this point in the history
* feat: support run document index tassk in background

* fix
  • Loading branch information
Mini256 authored May 11, 2024
1 parent 477b63e commit 6ac0c8d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ddl/0-initial-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ CREATE TABLE `document_index_task` (
index_id INT NOT NULL,
document_id INT NOT NULL,
type ENUM('CREATE_INDEX', 'REINDEX') NOT NULL ,
status ENUM('CREATED', 'PENDING', 'INDEXING', 'SUCCEED', 'FAILED') NOT NULL ,
status ENUM('CREATED', 'PENDING', 'INDEXING', 'SUCCEED', 'FAILED', 'CANCELED') NOT NULL ,
info JSON NULL NOT NULL ,
message TEXT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
Expand Down
12 changes: 10 additions & 2 deletions src/app/api/v1/documents/index/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ const IndexDocumentsOptionsSchema = z.object({
.int()
.array()
.min(1, 'Must provide at least one document'),
indexName: z.string()
indexName: z.string(),
runInBackground: z.boolean().default(false)
});

export const POST = defineHandler({
auth: 'admin',
body: IndexDocumentsOptionsSchema
}, async ({ body}) => {
const { documentIds, indexName } = body;
const { documentIds, indexName, runInBackground } = body;

const index = await getIndexByNameOrThrow(indexName);
const documentIdStr = documentIds.map((id) => `#${id}`).join(', ')
Expand All @@ -29,6 +30,12 @@ export const POST = defineHandler({
const taskIdStr = taskIds.map((id) => `#${id}`).join(', ')
console.log(`Create document index tasks ${taskIdStr}.`);

if (runInBackground) {
return {
taskIds
}
}

// Execute document index tasks.
const results = await Promise.allSettled(
taskIds.map(taskId => service.runDocumentIndexTask(taskId))
Expand All @@ -48,6 +55,7 @@ export const POST = defineHandler({
});

return {
taskIds,
succeed,
failed
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/db/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface DocumentIndexTask {
info: Json;
message: string | null;
started_at: Date | null;
status: "CREATED" | "FAILED" | "INDEXING" | "PENDING" | "SUCCEED";
status: "CANCELED" | "CREATED" | "FAILED" | "INDEXING" | "PENDING" | "SUCCEED";
type: "CREATE_INDEX" | "REINDEX";
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/knowledge-graph/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class KnowledgeGraphClient {
},
body: JSON.stringify(doc)
});
if (res.ok) {
if (!res.ok) {
throw new Error(`Failed to call build knowledge graph index API for doc (url: ${doc.uri}): ${res.statusText}`);
}
const data = await res.json();
Expand Down

0 comments on commit 6ac0c8d

Please sign in to comment.