Files
outline/server/queues/tasks/CleanupDeletedDocumentsTask.ts
Tom Moor 8b799966b7 perf: move cleanup reads to follower (#13616)
* perf: run cleanup scans on follower

* perf: move document cleanup reads to follower
2026-09-02 19:02:57 -04:00

55 lines
1.7 KiB
TypeScript

import { subDays } from "date-fns";
import { Op } from "sequelize";
import documentPermanentDeleter from "@server/commands/documentPermanentDeleter";
import Logger from "@server/logging/Logger";
import { Document } from "@server/models";
import { sequelizeReadOnly } from "@server/storage/database";
import { TaskPriority } from "./base/BaseTask";
import { Minute } from "@shared/utils/time";
import type { Props } from "./base/CronTask";
import { CronTask, TaskInterval } from "./base/CronTask";
export default class CleanupDeletedDocumentsTask extends CronTask {
public async perform({ limit, partition }: Props) {
Logger.info(
"task",
`Permanently destroying upto ${limit} documents older than 30 days…`
);
const documents = await sequelizeReadOnly.transaction((transaction) =>
Document.unscoped().findAll({
attributes: ["id", "teamId", "deletedAt", "content", "state", "text"],
where: {
deletedAt: {
[Op.lt]: subDays(new Date(), 30),
},
...this.getPartitionWhereClause("id", partition),
},
paranoid: false,
limit,
transaction,
})
);
const countDeletedDocument = await documentPermanentDeleter(documents);
Logger.info("task", `Destroyed ${countDeletedDocument} documents`);
}
public get options() {
return {
attempts: 3,
backoff: {
type: "exponential",
// Wait until the active partition window has ended before retrying.
delay: 15 * Minute.ms,
},
priority: TaskPriority.Background,
};
}
public get cron() {
return {
interval: TaskInterval.Hour,
partitionWindow: 15 * Minute.ms,
};
}
}