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.

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:

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.

Leave a Reply

Your email address will not be published.

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