perf: Move image download out of transaction (#11528)

* perf: Move image download out of transaction

* loop
This commit is contained in:
Tom Moor
2026-02-23 22:14:03 -05:00
committed by GitHub
parent ff6a20ef38
commit 957ce69d2e
2 changed files with 20 additions and 18 deletions
+3
View File
@@ -3,6 +3,9 @@ import type { User } from "@server/models";
import type { APIContext } from "@server/types";
import { AuthenticationType } from "@server/types";
/**
* Factory to create a new API context.
*/
export function createContext({
user,
authType = AuthenticationType.APP,
+17 -18
View File
@@ -3,8 +3,8 @@ import documentCreator from "@server/commands/documentCreator";
import documentImporter from "@server/commands/documentImporter";
import { createContext } from "@server/context";
import { User } from "@server/models";
import { sequelize } from "@server/storage/database";
import FileStorage from "@server/storage/files";
import { sequelize } from "@server/storage/database";
import { BaseTask, TaskPriority } from "./base/BaseTask";
type Props = {
@@ -37,24 +37,23 @@ export default class DocumentImportTask extends BaseTask<Props> {
}: Props): Promise<DocumentImportTaskResponse> {
try {
const content = await FileStorage.getFileBuffer(key);
const user = await User.findByPk(userId, {
rejectOnEmpty: true,
});
const document = await sequelize.transaction(async (transaction) => {
const user = await User.findByPk(userId, {
rejectOnEmpty: true,
transaction,
});
// Run document conversion and image downloading outside a transaction
const ctx = createContext({ user, ip });
const ctx = createContext({ user, transaction, ip });
const { text, state, title, icon } = await documentImporter({
user,
fileName: sourceMetadata.fileName,
mimeType: sourceMetadata.mimeType,
content,
ctx,
});
const { text, state, title, icon } = await documentImporter({
user,
fileName: sourceMetadata.fileName,
mimeType: sourceMetadata.mimeType,
content,
ctx,
});
return documentCreator(ctx, {
const document = await sequelize.transaction(async (transaction) =>
documentCreator(createContext({ ...ctx.context, transaction }), {
sourceMetadata,
title,
icon,
@@ -63,8 +62,8 @@ export default class DocumentImportTask extends BaseTask<Props> {
publish,
collectionId,
parentDocumentId,
});
});
})
);
return { documentId: document.id };
} catch (err) {
return { error: err.message };