Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I handle jobs timeout in Laravel?

I'm using Laravel 5.5 and i have some jobs that are checking emails with IMAP. Sometimes that can take too long, or lets say that user mistakes port or username, it would take too long for IMAP server to respond. So I came up with idea that i can restrict my jobs to some period, but when that period expires, not only that my job isn't moved to the failed jobs, but also my worker is killed. What am I missing? Is there any other way to do this? I would like to make some changes in the DB if the job expires. Any idea? Thank you in advance

like image 750
Nikola Avatar asked Feb 08 '18 15:02

Nikola


People also ask

How do I retry failed jobs in Laravel?

You can even retry all jobs at once by executing the php artisan queue:retry --all command. Alternatively, if you want to retry failed jobs from a certain queue, you can execute the php artisan queue:retry --queue=high-priority command.

Are Laravel jobs asynchronous?

php - Laravel Jobs are not asynchronous - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What is the difference between job and queue in Laravel?

Jobs and QueuesThe line itself is the Queue, and each customer in the line is a Job. In order to process Jobs in the Queue you need command line processes or daemons. Think of launching a queue daemon on the command line as adding a new bank teller to the pool of available bank tellers.


1 Answers

First, you need to manage your workers with Supervisor. If they are killed, supervisor will restart them again.
https://laravel.com/docs/5.5/queues#supervisor-configuration

After, you need to read about job expirations and timeouts in Laravel docs.
https://laravel.com/docs/5.5/queues#job-expirations-and-timeouts

To solve your problem you need to increase the --timeout of your workers. You can try with 3600 seconds. You need to increase the job expiration too (retry_after value in your queues on config/queues.php). Try with 3550 seconds. (If your --timeout option is longer than your retry_after configuration value, your jobs may be processed twice.) If you see the problem occurs again, you can increase the timeouts values.

Is very important that your code have try/catch to catch exceptions to exit if you catch an issue and don't wait 3550 seconds to release the job and the worker.

like image 111
Sangar82 Avatar answered Sep 30 '22 04:09

Sangar82