-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature request: GetPriorOccurrence #24
Comments
Please describe the idea behind this feature – we need to understand when it's required, because usually we are iterating forward. |
I have 2 use cases for you. In my case I'd like to show the user a percentage countdown or slider of how much time is left until the next run. Previous run is not stored anywhere (and of course at the first occurrence it is not possible to do otherwise than with such a function). So, when entering an expression, for example, 0 10 * * * if it is 18:00, the user would visually see that the length of the last interval is 24 hours and 33% of it has already elapsed. I can't do this if I don't have a previous date (I never have it on the first occurrence). I've actually tried different options (here and in NCrontab) but if there is no internal function that returns GetLastOccurrence or GetLastOccurrences (number) or GetPriorOccurrence or GetPreviousOccurrence, then calculating the previous value will be really difficult. I'm even willing to go to the first cron library that will allow me to do this 😄 Another thing is that I have data in the log when my process actually started. I would like to compare the last 5 planned launches against the actual launches. If I can't tell when the last N scheduled occurrences were according to the given expression, I can only say that it started then. But I don't know if it should have started then. |
This sounds like a domain issue to me - what happens if the process is interrupted, so it didn't complete successfully, or the process never stared? Is it still "correct" to display the last occurrence even if it didn't happen? IMO I would store the last run in a database and show that instead of trying to backwards-calculate when something could have happened. |
@celluj34 If the process is interrupted, then nothing bad happens 😸 - user still visually shows that last planned launch was scheduled for time X, the last actually triggered run (which of course is recorded in the database) was recorded at time Y and the next run is planned in time Z. Yes, sure, I could also store all scheduled launches in the database, and whenever the user changes the expression all data about plans in the future are deleted, rescheduled and saved again, but it seems to me like a useless work to show the user one date (the last scheduled start time). I'm not saying this can't be simulated, but why is it such a problem (even if everyone who requires this has a fundamental bug in the application logic) to provide this functionality? |
I had a need for this using the same cron expression
I've been exploring something similar. I think it's out of scope of this library, but if you're looking for some code that evaluates an actual cron expression and returns the previous expected execution time, you can use this. May be a little ugly, but I prefer it over just subtracting 60 days.
Example Usage:
|
Thanks everyone for your considerations and detailed descriptions. I've also re-checked other implementation like We've written Cronos because there was no Cron-related package in .NET (and looks like in other programming languages too, because I've found nothing) which supports And it looks like that So I think we've finished innovating with Cronos and will step back and let others to implement this feature. Hope for understanding. |
This was the workaround that worked best for me. In my case a default of 1 month ago was sufficient, a more general use case probably wants a null default. `
` |
@RZeni Won't this give the wrong result if not all schedules have the same distance. Ie This illustrates that a solution is not trivial and the hack solution @mcshaz already mentions is not that bad. You just have to account for schedules that might only run once a year so probably use I also agree with @celluj34 that this feels like a domain issue and should not be part of a generic cronexpression library. |
Would you please consider returning the return the DateTime when a task would have been due going back in time.
I am currently using the real hack
The text was updated successfully, but these errors were encountered: