Osama Jetawe
Osama Jetawe

Reputation: 2705

Cron job auto retry if any job get failed

I have a cron job that run every 5 Hours. It calls a PHP script , this script will do a call to an external API to sync some data.

The problem is sometimes I'm getting timeout from the API and the job will fail.

Are there any mechanisms to let cron tab do auto retry or auto recover the jobs that are failed?

I have tried to do an extra job and call it in case of any failures manually.

What is the best approach to do so?

Upvotes: 8

Views: 13619

Answers (3)

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102408

What you can do is something like this:

https://crontab.guru/#1_0-23_13__

1 0-23 13 * *

Start the job past 1 minute at every hour on the 13th of each month.

“At minute 1 past every hour from 0 through 23 on day-of-month 13.”

...then in your code you'd have some logic to detect if the process\script already ran correclty... if yes, skip the run attempt; otherwise let it run and then have a flag set to check against on the subsequent attempt run.

Hope you get the idea.

Upvotes: 1

Karol Gasienica
Karol Gasienica

Reputation: 2904

Cron does only run once at specific time or every minutes/hours/days etc. It doesn't check the return code. So it's not that easy peasy lemon squeezy at all...

In my opinion you have a few options how to do it:

  • Create a some kind of scheduler where you can write your CRON job again if it fails, in this case you will need one more CRON job to read you scheduler and run proper command. Scheduler can be database / file / NoSQL based. In scheduler you can have flag like (bool) executed which will let scheduler know which tasks are already done.

  • Use queues (f.ex. Rabbit) to call it self again when fail.

  • Use framework, I'm using Symfony to manage own created commands to execute them (check second link below) based on database, using also enqueue/enqueue-bundle package to manage queues in Symfony.

I think if you are not so advanced with PHP I'd recommend to go for self made scheduler based on database (MySQL / PostgreSQL / NoSQL) with Symfony (check second link below). In this case you just have to SELECT all non executed record (commands) from database and just run them.


Lecture

Laravel - Queues, retrying failed jobs

Symfony - calling another commands in command

Queues package for PHP (incl. Symfony)

enqueue/enqueue-bundle

Upvotes: 5

Samir Sayyad
Samir Sayyad

Reputation: 119

You can use supervisord :

supervisord website

Or handle API timeout in code.

Upvotes: 0

Related Questions