Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron job auto retry if any job get failed

Tags:

php

cron

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?

like image 882
Osama Jetawe Avatar asked Dec 31 '19 11:12

Osama Jetawe


1 Answers

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

like image 90
Karol Gasienica Avatar answered Sep 30 '22 11:09

Karol Gasienica