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:
@@ -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;
|
||||
|
||||
@@ -372,8 +372,10 @@ export const appRouter = router({
|
||||
sharepoint_token: z.string().optional(),
|
||||
auth_mode: z.enum(["local", "free"]).optional(),
|
||||
import_time: z.string().optional(),
|
||||
fetch_mode: z.enum(["scheduled", "interval"]).optional(),
|
||||
fetch_mode: z.enum(["daily", "weekly", "monthly", "interval", "scheduled"]).optional(),
|
||||
fetch_interval_minutes: z.string().optional(),
|
||||
fetch_week_day: z.string().optional(),
|
||||
fetch_month_day: z.string().optional(),
|
||||
})
|
||||
)
|
||||
.mutation(async ({ input }) => {
|
||||
@@ -383,7 +385,7 @@ export const appRouter = router({
|
||||
}
|
||||
await setSettings(toSave);
|
||||
// Recharger le cron si la planification a changé
|
||||
if (toSave.import_time || toSave.fetch_mode || toSave.fetch_interval_minutes) {
|
||||
if (toSave.import_time || toSave.fetch_mode || toSave.fetch_interval_minutes || toSave.fetch_week_day || toSave.fetch_month_day) {
|
||||
await scheduleDailyImport();
|
||||
}
|
||||
return { success: true };
|
||||
|
||||
Reference in New Issue
Block a user