Reputation: 620
Kubernetes have cronjob which can be used to schedule jobs periodically https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/
Is there a way to reset cronjob schedule, let's say it's running at 0
,5
,10
,15th
min of hour at every 5 mins schedule, now if i want to schedule a job manually at 17th
min, i can trigger one, but i also want now cronjob schedule to reset and next run should be at 22th
min, instead of 20th
min.
I have tried deleting and recreating job with same spec, but that doesn't help as well, somehow it ends up restoring schedule meta for the job specs.
Is there a trick to reset cronjob schedule?
Cron job running at every 5 min
0, 5, 10, 15th, 20th
Reset at 17th mins , next cadence should be
0, 5, 10, 15th, 17th, 22th
Upvotes: 1
Views: 1765
Reputation: 8952
I stumbled across this post today with this very same problem, and came up with the following solution. Note, this assumes you're using Helm for templating.
{{- $t := now | date "4.0" | float64 | ceil }}
{{- $k := .Values.cadence }}
{{- $offset := sub $t (mul $k (div $t $k)) }}
spec:
schedule: "{{ $offset }}-59/{{ $k }} * * * *"
This will, on each deployment, run the cron job at the top of the next minute and then again after .Values.cadence
number of minutes and so on. So to "reset" your cron schedule, you just redeploy.
Here's an interactive helm playground where you can see this at work with some explanations for each step.
I'd recommend opening up https://crontab.guru/ as well. You can check the results by "running helm" in this playground (if you just hit enter or spacebar somewhere in the template.yaml
or values.yaml
panel of the playground, it'll cause now
to be reevaluated) and copying the schedule it gives you into the crontab guru site, which will show you the next few scheduled runs.
E.g., say you deploy your cron job at 16:24:15.258
and suppose you want to run your cron job every 7 minutes. Helm would produce this chart:
spec:
schedule: "4-59/7 * * * *"
And if you plug that into crontab guru it will show your schedule:
next at 2023-12-20 16:25:00
then at 2023-12-20 16:32:00
then at 2023-12-20 16:39:00
then at 2023-12-20 16:46:00
then at 2023-12-20 16:53:00
For my problem, I'm running very short-lived cron jobs on a very short cadence (every X minutes). This won't work for cadences > 59 minutes -- for that, you'd need to modify the hour and minute parts of the cron schedule.
Note as well that since cron jobs can only be scheduled with minute-precision, you would have to wait up to 60 seconds for your first job to run after "resetting" the schedule in the worst case (i.e., you deployed at exactly 0 seconds into the current minute).
Upvotes: 0
Reputation: 2290
Automatically no ( actually maybe with a script that modify your cronjob manifest but I think that's ugly but do whatever you want)
However, if you want to modify it after every manual job execution (for example at min 17) go modify your cronjob like this for example:
2-57/5 * * * *
This way if you ran it manually a minute 17, the next will be 22,27 etc..
If another day you ran it manually at minute 24 for example do this:
4-59/5 * * * *
Etc...
Upvotes: 1