mirror of
https://github.com/outline/outline.git
synced 2026-06-13 11:25:03 +03:00
perf: Move image download out of transaction (#11528)
* perf: Move image download out of transaction * loop
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,14 +37,12 @@ export default class DocumentImportTask extends BaseTask<Props> {
|
||||
}: Props): Promise<DocumentImportTaskResponse> {
|
||||
try {
|
||||
const content = await FileStorage.getFileBuffer(key);
|
||||
|
||||
const document = await sequelize.transaction(async (transaction) => {
|
||||
const user = await User.findByPk(userId, {
|
||||
rejectOnEmpty: true,
|
||||
transaction,
|
||||
});
|
||||
|
||||
const ctx = createContext({ user, transaction, ip });
|
||||
// Run document conversion and image downloading outside a transaction
|
||||
const ctx = createContext({ user, ip });
|
||||
|
||||
const { text, state, title, icon } = await documentImporter({
|
||||
user,
|
||||
@@ -54,7 +52,8 @@ export default class DocumentImportTask extends BaseTask<Props> {
|
||||
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 };
|
||||
|
||||
Reference in New Issue
Block a user