2

I'm working on a Kubernetes CronJob that relies on knowing when the last successfull run happened in order to correctly notify users about new events since said run. That said, I need to know if I can rely on status.lastScheduleTime to be my "last successful run" timestamp.

When does it change? Does it depend on exit codes? Is it even a good idea to use it for that purpose?

Pedro Fracassi
  • 3,342
  • 2
  • 16
  • 33

1 Answers1

2

No, you can't rely on that as an indicator of a successful run. That value changes whenever your CronJob runs. It doesn't mean that it's your last successful run and it doesn't change depending on exit codes.

A CronJob essentially runs a Job with a name that is <cronjob name>-<unix epoch>. The epoch is in Unix/Linux what you would get from the date +%s command, for example, also that epoch is a timestamp that is slightly later than the timestamp of the lastScheduleTime (It's when the job resource gets created)

To find out if your last cron job ran successfully you can do something like the following.

You can get the last Job run/started name including its epoch with something like this:

$ kubectl get jobs | tail -1 | awk '{print $1}'

Then after that, you could check whether that job is successful with something like:

$ kubectl get job <job-name> -o=jsonpath='{.status.succeeded}'

Should return a 1.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Rico
  • 58,485
  • 12
  • 111
  • 141