fix(cron): fix scheduling bugs and add destroy method#97
Open
hakanesnn wants to merge 6 commits intoCommunityOx:mainfrom
Open
fix(cron): fix scheduling bugs and add destroy method#97hakanesnn wants to merge 6 commits intoCommunityOx:mainfrom
hakanesnn wants to merge 6 commits intoCommunityOx:mainfrom
Conversation
…scheduling getNextTime only checked if the current day matched, causing cron jobs with future dates or hour ranges to silently stop. Rewrote it to scan up to 366 days ahead, checking each candidate day/hour/minute against the cron fields via a new matchesField helper function. Fixes hour range expressions (e.g. 0 17-23 * * sun) only firing once, and future day expressions (e.g. 0 0 01 01 *) never firing.
getAbsoluteNextTime now delegates to the rewritten getNextTime instead of duplicating time calculation logic. Removes the now-unused getTimeUnit function and maxUnits table.
The daily midnight job previously restarted all inactive tasks, including ones intentionally stopped by the user. Added a manualStop flag to distinguish user-initiated stops from internal ones, so only internally stopped tasks get rescheduled.
task:destroy() stops the task and removes it from the internal tasks table, allowing garbage collection. Also switched the midnight reschedule loop to use pairs() since destroyed tasks leave nil gaps.
day=-1 returns the second-to-last day of the month, not the last. Changed to day=0 which correctly returns the last day.
#tasks+1 is unreliable for sparse tables after destroy() leaves nil gaps. Replaced with a monotonically incrementing counter to guarantee unique IDs.
FjamZoo
approved these changes
Apr 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes
day=0. Previously returned 30 for January, 27 for February, etc.0 17-23 * * *) firing only once and stopping (Issue: [Bug] Cron - Day ahead or more cron doesn't run #77).0 0 01 01 *) failing to schedule if the target date is not today (Issue: [Bug] Cron not working properly #95).Improvements
#tasks+1to prevent ID collisions when tasks are destroyed.getAbsoluteNextTimeby delegating logic to the updatedgetNextTime.Test Code
Old code:
New code: