Files
outline/server/queues/tasks/UpdateTeamsAttachmentsSizeTask.ts
Tom Moor d3182686ed Add search_queries cleanup job to match event log retention (#13035)
* Add search_query cleanup job to match event log retention

* fix: findAllInBatches to use key based pagination rather than offset
2026-07-18 12:39:55 -04:00

62 lines
1.6 KiB
TypeScript

import { subDays } from "date-fns";
import { Op } from "sequelize";
import Logger from "@server/logging/Logger";
import { Attachment } from "@server/models";
import { sequelize } from "@server/storage/database";
import { TaskPriority } from "./base/BaseTask";
import { CronTask, TaskInterval } from "./base/CronTask";
import UpdateTeamAttachmentsSizeTask from "./UpdateTeamAttachmentsSizeTask";
type Props = {
limit: number;
};
export default class UpdateTeamsAttachmentsSizeTask extends CronTask {
public async perform({ limit }: Props) {
Logger.info(
"task",
`Recalculating attachment sizes for upto ${limit} teams…`
);
// Find unique attachment teamIds created in the last day, update only
// those teams' approximate attachment sizes
await Attachment.findAllInBatches<Attachment>(
{
attributes: [
[sequelize.fn("DISTINCT", sequelize.col("teamId")), "teamId"],
],
where: {
createdAt: {
[Op.gt]: subDays(new Date(), 1),
},
},
// A unique order is required for keyset pagination; the primary key
// cannot be used with a DISTINCT select.
order: [["teamId", "ASC"]],
batchLimit: 100,
raw: true,
},
async (rows) => {
const teamIds = rows.map((row) => row.teamId);
for (const teamId of teamIds) {
await new UpdateTeamAttachmentsSizeTask().schedule({ teamId });
}
}
);
}
public get cron() {
return {
interval: TaskInterval.Day,
};
}
public get options() {
return {
attempts: 1,
priority: TaskPriority.Background,
};
}
}