Files
outline/server/queues/tasks/UploadAttachmentFromUrlTask.ts
Tom Moor 42959d66db chore: Add cron task partitioning (#10736)
* wip

* Implementation complete

* tidying

* test

* Address feedback

* Remove duplicative retry logic from UpdateDocumentsPopularityScoreTask.
Now that we're split across many runs this is not neccessary

* Refactor to subclass, config to instance

* Refactor BaseTask to named export

* fix: Missing partition

* tsc

* Feedback
2025-11-27 16:57:52 +01:00

55 lines
1.4 KiB
TypeScript

import { createContext } from "@server/context";
import { Attachment } from "@server/models";
import FileStorage from "@server/storage/files";
import { BaseTask, TaskPriority } from "./base/BaseTask";
import { sequelize } from "@server/storage/database";
type Props = {
/** The ID of the attachment */
attachmentId: string;
/** The remote URL to upload */
url: string;
};
/**
* A task that uploads the provided url to a known attachment.
*/
export default class UploadAttachmentFromUrlTask extends BaseTask<Props> {
public async perform(props: Props) {
const attachment = await Attachment.findByPk(props.attachmentId, {
rejectOnEmpty: true,
include: [{ association: "user" }],
});
try {
const res = await FileStorage.storeFromUrl(
props.url,
attachment.key,
attachment.acl
);
if (res?.url) {
await sequelize.transaction(async (transaction) => {
const ctx = createContext({ user: attachment.user, transaction });
await attachment.updateWithCtx(ctx, {
url: res.url,
size: res.contentLength,
contentType: res.contentType,
});
});
}
} catch (err) {
return { error: err.message };
}
return {};
}
public get options() {
return {
attempts: 3,
priority: TaskPriority.Normal,
};
}
}