Laravel Queues in Action (2nd edition) is now available!

Always set a timeout for Guzzle requests inside a queued job

Updated: Jan 23, 2020 — 1 min Read#quick-dip

If you have a queued job that uses Guzzle to make a request, make sure you set a timeout value for the guzzle request:

(new \GuzzleHttp\Client())->get('http://domain.com', [
   'timeout' => 15
]);

If you don't set a proper timeout and the external endpoint doesn't respond, the worker will get stuck and further jobs won't get processed. Even if you have a timeout set on the worker or the job level, it will get ignored and the process will wait indefinitely.

The reason for that is cURL blocking the execution of your PHP code and thus it will not be able to handle the timeout signal sent to the worker.

Also make sure that the timeout set for the worker is more than the timeout set for the guzzle requests, otherwise the job will fail even if the guzzle request eventually succeeds.

Hey! 👋 If you find this content useful, consider sponsoring me on GitHub.

You can also follow me on Twitter, I regularly post about all things Laravel including my latest video tutorials and blog posts.

By Mohamed Said

Hello! I'm a former Laravel core team member & VP of Engineering at Foodics. In this publication, I share everything I know about Laravel's core, packages, and tools.

You can find me on Twitter and Github.

This site was built using Wink. Follow the RSS Feed.