How to prevent a Cronjob execution in Kubernetes if there is already a job running? concurrencyPolicy:Forbid stops the cron job execution altogether

11/4/2019

I need a cron job to run every 5 minutes. If an earlier cron job is still running, another cron job should not start. I tried setting concurrency policy to Forbid, but then the cron job does not run at all.

  1. Job gets launched every 5 minutes as expected, but it launches even if the earlier cron job has not completed yet
spec:
  concurrencyPolicy: Allow
  schedule: '*/5 * * * *'
  1. This is supposed to solve the problem, but the cron job never gets launched with this approach
spec:
  concurrencyPolicy: Forbid
  schedule: '*/5 * * * *'
  1. Setting the startingDeadlineSeconds to 3600, or even to 10, did not make a difference.
spec:
  concurrencyPolicy: Forbid
  schedule: '*/5 * * * *'
  startingDeadlineSeconds: 10

Could someone please help me here?

-- Satish Eddhu Thulasiraman
concurrency
cron
gcloud
kubernetes

1 Answer

11/4/2019

From kubernetes documentation

Concurrency Polic specifies how to treat concurrent executions of a job that is created by this cron job. The spec may specify only one of the following concurrency policies:

Allow (default): The cron job allows concurrently running jobs

Forbid: The cron job does not allow concurrent runs; if it is time for a new job run and the previous job run hasn’t finished yet, the cron job skips the new job run

Replace: If it is time for a new job run and the previous job run hasn’t finished yet, the cron job replaces the currently running job run with a new job run

In your case 'concurrencyPolicy: Forbid' should work. It will not allow new job to be run if the previous job is still running. The problem is not with concurrencyPolicy in your case.

It might be related to startingDeadlineSeconds. can you remove it and try

-- P Ekambaram
Source: StackOverflow