I have set up a cronjob to run every 5 minutes. Now, if I want to skip a job instance once if it fails twice. For example: if it fails at 8:45 am and then again at 8:50 am, it should skip the 8:55 am run and try again at 9 am. How to achieve this?
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: meta
namespace: meta-refresh-development
labels:
app: meta
spec:
schedule: "*/5 * * * *"
successfulJobsHistoryLimit: 1
jobTemplate:
spec:
template:
spec:
containers:
- name: meta
image: <docker-image>
imagePullPolicy: IfNotPresent
env:
- name: personal_access_token_name
valueFrom:
secretKeyRef:
key: personal_access_token_name
name: <secret-pod>
restartPolicy: Never
backoffLimit: 1
You must build that logic in the code. when first job runs initialize the counter and the status in a database. when the job runs next time increment the counter if the previous run was failed. When the counter becomes 2 then skip the run and reset the counter to zero.
By default k8s will stop running the job if it has >= 100 missed schedules. This value for the time being is not adjustable. You can set the CronJob "suspend" field to "true" in order to suspend the schedule job.