Laravel Job: Common problem – Queue stuck, not responding to “artisan queue:restart”

Laravel queue is quite easy to setup and use.

But how it actually work is seem like a mystery and undocumented

There are some pitfall that beginner usually fall into, the common one is stucking

1. Job opened a resource that does not timeout properly

A typical example is Guzzle / cURL with default settings.
Laravel queue worker works in PHP CLI that have max_execution_time = 0 (Unlimited)

Guzzle without timeout easily stuck queue forever. Add timeout parameters to any calls within your Job to prevent this problem.

<?php
$httpClient = new \GuzzleHttp\Client();

$response = $httpClient->request('GET', 'http://...', [
    'timeout' => 30,
    'read_timeout' => 30,
    'connect_timeout' => 30,
])

2.  Job opened resources/used memory that does not release properly

A typical example is functions calling TCPDF, PHPExcel, PHPSpreadSheet… memory demanding libraries
Laravel queue worker does not restart PHP process after each Job executed.

If memory is not freed properly, the PHP process quickly run out-of-memory and slow-down & freeze may happen.
It is a well-known memory leak problem. Refer to the libraries docs to see how to de-initialize them properly

3. Laravel Job “timeout” does not work without PHP 7.1+ and pcntl extension.

This is not documented in Laravel 5.3 Docs,
and ambiguously documented as “optimized for PHP 7.1+ and the pcntl PHP extension” in Laravel 5.4+Docs

In fact, it does not work at all without meeting that requirement, see: https://github.com/laravel/framework/issues/19584

pcntl extension is however, does not work nor included in any Windows version of PHP (Including XAMPP, Official Binary Releases… etc)

You should not rely on this feature unless you meets all those requirements.

One Reply to “Laravel Job: Common problem – Queue stuck, not responding to “artisan queue:restart””

  1. What’s up colleagues, how is the whole thing, and what you wish for to say on the topic of this piece of writing, in my view its genuinely remarkable in support of me.|

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.