Replies: 6 comments
-
Hi! That's actually by design, both previousRun() and currentRun() only return the time of an actual trigger. There is currently not way to list previous matches, but there is an discussion started at #219 |
Beta Was this translation helpful? Give feedback.
-
I see. Any idea on how id compute the previous run then? or in the same vein, figure out if a job was run the same day? |
Beta Was this translation helpful? Give feedback.
-
You should be able to do something like import Cron from 'https://esm.run/croner@7.0.5';
const job = new Cron("0 0 11 * * *"); // 11:00:00 every day
const today = new Date(new Date().setHours(0,0,0,0)); // (hours, minutes, seconds, milliseconds)
const firstRunToday = job.nextRun(today);
const earlierTriggerToday = firstRunToday && firstRunToday < new Date();
const laterTriggerToday = job.nextRun() && (job.nextRun().setHours(0,0,0,0) == today.getTime());
console.log(earlierTriggerToday, laterTriggerToday); Edit (making a bit more modular): import Cron from 'https://esm.run/croner@7.0.5';
// Function to check if the cron job has triggered earlier today
function hasTriggeredEarlierToday(job) {
const today = new Date(new Date().setHours(0,0,0,0)); // Start of the current day
const firstRunToday = job.nextRun(today);
return firstRunToday && firstRunToday < new Date();
}
// Function to check if the cron job will trigger later today
function willTriggerLaterToday(job) {
const today = new Date(new Date().setHours(0,0,0,0)); // Start of the current day
const nextRun = job.nextRun();
return nextRun && (nextRun.setHours(0,0,0,0) == today.getTime());
}
// Example usage
const job = new Cron("0 0 11 * * *"); // 11:00:00 every day
const earlierTriggerToday = hasTriggeredEarlierToday(job);
const laterTriggerToday = willTriggerLaterToday(job);
console.log('Has triggered earlier today:', earlierTriggerToday);
console.log('Will trigger later today:', laterTriggerToday); |
Beta Was this translation helpful? Give feedback.
-
Thanks ! That's more or less what I was trying to achieve, do you thinks maybe a similar function could make its way into a future version ? I'm sure I'm not the only one having a use case for checking whether a cron expr is gonna run/has run on a specific date (in my case today). |
Beta Was this translation helpful? Give feedback.
-
Yep, I'll consider adding a feature like this, just got to figure out how to make is as usable as possible 👍 |
Beta Was this translation helpful? Give feedback.
-
I am also looking to do this. Because it's not available for the moment, I made this function (assuming it will be running later and it has already run before). Feel free to modify it and show us how you can optimize it. Far from perfect but it is what it is. /**
* Get the date of when the cron should have run assuming it should have run before and will be running later
*
* @param {string} cron The cron string used for the calculation
* @param {Date} from The date from where we start looking backwards
*/
function GetPreviousCronRun(cron, from) {
const hourInMs = (60 * 60 * 1000);
let previousHours = 1;
let date = from;
let savedDate;
/* While the date for the next running cron is later than the date we're looking for */
while (date >= from) {
/* Substract an hour to the date we're starting to look from */
date = new Date(new Date(from).getTime() - (hourInMs * previousHours));
date = new Date(Cron(cron).nextRun(date));
previousHours += 1;
}
savedDate = new Date(date);
/* While the date for the next running cron is later than the date we're looking for
* we refine the date, to have the closest one from the date */
while (date < from) {
date = new Date(Cron(cron).nextRun(date));
if (date < from) {
savedDate = new Date(date);
}
}
return (savedDate);
} |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
previousRun and currentRun return null all the time.
To Reproduce
Expected behavior
The actual date of when the previous run would have ran.
System:
Beta Was this translation helpful? Give feedback.
All reactions