Search code examples
phplaravelqueuetimeoutjobs

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


Solution

  • 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.