Files
HiveGrid-Wireframes/hivegrid-cloud/apps/worker/src/jobs/task-archive-retention-cron.ts
T

53 lines
2.1 KiB
TypeScript

// P4 Tasks (AK-P4b) §7 — 30-day auto-archive cron (DoD 5).
//
// Daily maintenance job that archives completed tasks (`status = done`,
// `completedAt <= now - 30d`, not yet archived) by setting `archivedAt`. The
// execution logic lives in the maintenance handler (handlers.ts) which holds the
// Prisma client; this file only carries the config + the idempotent scheduler
// registration (BullMQ `upsertJobScheduler`, like the other retention crons).
export const TASK_ARCHIVE_RETENTION_JOB_ID = "task-archive-retention" as const;
export const TASK_ARCHIVE_RETENTION_EVERY_MS = 24 * 60 * 60 * 1000;
/** Completed tasks older than this are auto-archived. 30 days = 720 hours. */
export const TASK_ARCHIVE_RETENTION_DEFAULT_HOURS = 720;
export const TASK_ARCHIVE_RETENTION_DEFAULT_BATCH_SIZE = 1000;
export function resolveTaskArchiveRetentionCronConfig(env: NodeJS.ProcessEnv = process.env): {
olderThanHours: number;
take: number;
} {
return {
olderThanHours: parsePositiveInteger(env.TASK_ARCHIVE_RETENTION_HOURS, TASK_ARCHIVE_RETENTION_DEFAULT_HOURS),
take: parsePositiveInteger(env.TASK_ARCHIVE_RETENTION_BATCH_SIZE, TASK_ARCHIVE_RETENTION_DEFAULT_BATCH_SIZE),
};
}
export async function registerTaskArchiveRetentionCron(
queue: {
upsertJobScheduler: (schedulerId: string, options: { every: number }, template: { name: string; data?: Record<string, unknown> }) => Promise<unknown>;
},
env: NodeJS.ProcessEnv = process.env
): Promise<void> {
const config = resolveTaskArchiveRetentionCronConfig(env);
await queue.upsertJobScheduler(
TASK_ARCHIVE_RETENTION_JOB_ID,
{ every: TASK_ARCHIVE_RETENTION_EVERY_MS },
{
name: TASK_ARCHIVE_RETENTION_JOB_ID,
data: {
kind: "cleanup.run",
task: TASK_ARCHIVE_RETENTION_JOB_ID,
olderThanHours: config.olderThanHours,
take: config.take,
},
}
);
}
function parsePositiveInteger(value: string | undefined, fallback: number): number {
if (!value) return fallback;
const parsed = Number(value);
if (!Number.isFinite(parsed) || parsed <= 0) return fallback;
return Math.floor(parsed);
}