Checkpoint: Ajout des modes de fréquence quotidien, hebdomadaire (avec sélecteur de jour Lun-Dim), mensuel (avec choix du jour 1-28) et intervalle (1h à 24h). Le cron est rechargé immédiatement après sauvegarde pour tous les paramètres de planification.

This commit is contained in:
Manus
2026-06-29 17:07:28 -04:00
parent 1f1a479029
commit cffb3d4201
5 changed files with 120 additions and 41 deletions

View File

@@ -35,27 +35,46 @@ let cronJob: ReturnType<typeof cron.schedule> | null = null;
export async function scheduleDailyImport() {
const importTime = (await getSetting("import_time")) || "06:00";
const fetchMode = (await getSetting("fetch_mode")) || "scheduled";
const fetchMode = (await getSetting("fetch_mode")) || "daily";
const fetchIntervalMinutes = parseInt((await getSetting("fetch_interval_minutes")) || "1440", 10);
const fetchWeekDay = (await getSetting("fetch_week_day")) || "1"; // 0=dim, 1=lun, ..., 6=sam
const fetchMonthDay = (await getSetting("fetch_month_day")) || "1"; // 1-28
const [hour, minute] = importTime.split(":").map(Number);
const h = hour ?? 6;
const m = minute ?? 0;
let cronExpr: string;
let cronLabel: string;
if (fetchMode === "interval") {
const intervalMin = Math.max(60, fetchIntervalMinutes);
if (intervalMin < 60) {
cronExpr = `*/${intervalMin} * * * *`;
} else if (intervalMin % 60 === 0) {
if (intervalMin % 60 === 0) {
const hours = intervalMin / 60;
cronExpr = hours === 24 ? `0 0 * * *` : `0 */${hours} * * *`;
cronExpr = hours === 24 ? `0 ${m} ${h} * * *` : `0 ${m} */${hours} * * *`;
} else {
cronExpr = `*/${intervalMin} * * * *`;
cronExpr = `0 */${intervalMin} * * * *`;
}
console.log(`[Cron] Mode intervalle — toutes les ${intervalMin} minutes (${cronExpr})`);
const hours = intervalMin / 60;
cronLabel = `Mode intervalle — toutes les ${hours >= 1 ? hours + "h" : intervalMin + "min"} (${cronExpr})`;
} else if (fetchMode === "weekly") {
// hebdomadaire : le jour choisi à l'heure choisie
cronExpr = `0 ${m} ${h} * * ${fetchWeekDay}`;
const jours = ["dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"];
cronLabel = `Mode hebdomadaire — chaque ${jours[parseInt(fetchWeekDay)] ?? "lundi"} à ${importTime} (${cronExpr})`;
} else if (fetchMode === "monthly") {
// mensuel : le jour du mois choisi à l'heure choisie
const day = Math.min(28, Math.max(1, parseInt(fetchMonthDay)));
cronExpr = `0 ${m} ${h} ${day} * *`;
cronLabel = `Mode mensuel — le ${day} de chaque mois à ${importTime} (${cronExpr})`;
} else {
const [hour, minute] = importTime.split(":").map(Number);
cronExpr = `0 ${minute ?? 0} ${hour ?? 6} * * *`;
console.log(`[Cron] Mode heure fixe — tous les jours à ${importTime} (${cronExpr})`);
// daily (défaut)
cronExpr = `0 ${m} ${h} * * *`;
cronLabel = `Mode quotidien — tous les jours à ${importTime} (${cronExpr})`;
}
console.log(`[Cron] ${cronLabel}`);
if (cronJob) {
cronJob.stop();
cronJob = null;