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 want to receive updates on what I'm up to, I host a newsletter on my website themsaid.com and would love to have you.

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 full-stack web developer working at Laravel. 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.