Files
Tom MoorandClaude Opus 5.5 82be0626dc fix: Respect per-task cron limit across partitions (#14339)
* fix: Respect per-task cron limit across partitions

Adds an optional limit to TaskSchedule that caps the number of items a cron
task processes per run. Partitioned runs split the limit across partitions
instead of giving each partition the full request limit.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix: Cap cron partitions at the task limit so every UUID range is processed

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-26 01:01:28 +00:00

52 lines
1.4 KiB
TypeScript

import { Day, Hour, Second } from "@shared/utils/time";
import tasks from "@server/queues/tasks";
import { CronTask, TaskInterval } from "@server/queues/tasks/base/CronTask";
export default function init() {
async function run(schedule: TaskInterval) {
const partition = {
partitionIndex: 0,
partitionCount: 1,
};
const scheduledAt = Date.now();
for (const name in tasks) {
const TaskClass = tasks[name];
if (!(TaskClass.prototype instanceof CronTask)) {
continue;
}
const taskInstance = new TaskClass() as CronTask;
const { interval, limit, partitionWindow } = taskInstance.cron;
if (interval !== schedule) {
continue;
}
// Stagger the start of tasks that define a window within that window.
const delay = partitionWindow
? CronTask.getStaggerDelay(name, interval, partitionWindow)
: 0;
await taskInstance.schedule(
{
limit: limit ?? 10000,
partition,
scheduledAt,
},
{ delay }
);
}
}
setInterval(() => void run(TaskInterval.Day), Day.ms).unref();
setInterval(() => void run(TaskInterval.Hour), Hour.ms).unref();
// Just give everything time to startup before running the first time. Not
// _technically_ required to function.
setTimeout(() => {
void run(TaskInterval.Day);
void run(TaskInterval.Hour);
}, 5 * Second.ms).unref();
}