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('', [
   '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.

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.

Join the mailing list

If you like this article, you may want to consider joining the mailing list to receive new content once it is posted.
This site was built using Wink. Follow the RSS Feed.