feat: support overriding the Prev time for a new job/entry#446
Open
dominicbarnes wants to merge 1 commit into
Open
feat: support overriding the Prev time for a new job/entry#446dominicbarnes wants to merge 1 commit into
dominicbarnes wants to merge 1 commit into
Conversation
SamPeng87
pushed a commit
to SamPeng87/cron
that referenced
this pull request
May 9, 2022
fufuok
added a commit
to fufuok/cron
that referenced
this pull request
Feb 17, 2023
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.
This PR adds a new capability to override the default "prev" time for a job during create, which has a number of useful applications.
First, it allows a schedule to be preserved across process restarts, keeping interval-based jobs from all being interrupted each time. The extra work needed to store/retrieve these timestamps is out of scope for this library, but this change allows that feature where it could not be done before. This could be considered a solution to #336.
Second, this can be a quick-and-dirty way to trigger jobs immediately, by simply choosing a "prev" time that is sufficiently far back in the past. It's a little janky, but there are tons of issues submitted about this very issue so I believe that utility would provide value. (this could be considered as an alternative fix for #436, #422, #406, #297, #396, #439, #356, #342, #336 and possibly others)
This is accomplished via a new
...EntryOptionparameter forCron.AddJob,Cron.AddFuncandCron.Schedule, which isn't technically backwards compatible, as the function signature does change, but most code shouldn't need to be altered. This pattern mirrorsNew(...Option) Cronin structure, rather than trying to do something new. Currently, the only available option isWithPrevthat setsEntry.Jobbefore scheduling the job for the first time, but there could be room to add additional configurations if needed.Additionally,
Cronnow considers the possibility thatEntry.Prevmay be nonzero, and will check that first, otherwise falling back on the current behavior of "now". In short, this is an opt-in feature, and should not change default behavior.