fix parsing of cron pattern

in some old instances, we had "00 00  * * *" (note double space
and only 5 components).
This commit is contained in:
Girish Ramakrishnan
2025-11-18 09:58:38 +01:00
parent 2cb7b4d1ea
commit c5f97e8bb0
4 changed files with 63 additions and 71 deletions
+48
View File
@@ -671,6 +671,50 @@ const cronDays = [
// generates 24h time sets (instead of american 12h) to avoid having to translate everything to locales eg. 12:00
const cronHours = Array.from({ length: 24 }).map(function (v, i) { return { id: i, name: (i < 10 ? '0' : '') + i + ':00' }; });
function prettySchedule(pattern) {
if (!pattern) return '';
const tmp = pattern.trim().split(/\s+/); // remove extra spaces between tokens, which is valid cron
if (tmp.length === 1) return pattern.charAt(0).toUpperCase() + pattern.slice(1); // case for 'never' - capitalize
if (tmp.length === 5) tmp.unshift('0'); // if seconds is missing, add it
if (tmp.length !== 6) return `Unrecognized pattern - ${pattern}`;
const hours = tmp[2].split(',').sort((a, b) => Number(a) - Number(b));
const days = tmp[5].split(',').sort((a, b) => Number(a) - Number(b));
try {
const prettyDay = (days.length === 7 || days[0] === '*') ? 'Every day' : days.map((day) => { return cronDays[parseInt(day, 10)].name.substr(0, 3); }).join(', ');
const prettyHour = (hours.length === 24 || hours[0] === '*') ? 'hourly' : hours.map((hour) => { return cronHours[parseInt(hour, 10)]; }).sort((a,b) => a.id - b.id).map(h => h.name).join(', ');
return `${prettyDay} @ ${prettyHour}`;
} catch (error) {
console.error('Unable to build pattern.', error);
return `Unrecognized pattern - ${pattern}`;
}
};
function parseSchedule(pattern) {
const tmp = pattern.trim().split(/\s+/); // remove extra spaces between tokens, which is valid cron
if (tmp.length === 1) return console.error(`Never pattern should not be passed here - ${pattern}`);
if (tmp.length === 5) tmp.unshift('0'); // if seconds is missing, add it
if (tmp.length !== 6) return console.error(`Unrecognized pattern - ${pattern}`);
const tmpHours = tmp[2].split(',');
const tmpDays = tmp[5].split(',');
let days, hours;
if (tmpDays[0] === '*') days = cronDays.map((day) => { return day.id; });
else days = tmpDays.map((day) => { return parseInt(day, 10); });
if (tmpHours[0] === '*') hours = cronHours.map(h => h.id);
else hours = tmpHours.map((hour) => { return parseInt(hour, 10); });
return { days, hours };
}
function getColor(numOfSteps, step) {
const deg = 360/numOfSteps;
return `hsl(${deg*step} 70% 50%)`;
@@ -693,6 +737,8 @@ export {
cronDays,
cronHours,
getColor,
prettySchedule,
parseSchedule
};
// default export
@@ -712,4 +758,6 @@ export default {
cronDays,
cronHours,
getColor,
prettySchedule,
parseSchedule
};