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(); }