The Real Source of ProcessTimedOutException in Laravel Queues
When running php artisan queue:listen, you might encounter a Symfony\Component\Process\Exception\ProcessTimedOutException. At first glance, the error message suggests that the worker process itself has stopped responding after 60 seconds. However, the root cause is rarely that your PHP-FPM or CLI process has hung; it is almost always a configuration mismatch between how long a job runs and how long Laravel's supervisor expects to wait for it.
The error log reveals the specific command being executed:
/usr/local/Cellar/php55/5.5.14/bin/php artisan queue:work --queue="QUEUE_URL" --delay=0 --memory=128 --sleep=3 --tries=0
Notice the --memory=128 flag? This is a critical detail. It tells the supervisor to kill the process immediately if it exceeds 128 MB of RAM. While this prevents memory leaks from crashing your server, it can also terminate legitimate long-running jobs that require more than 128 MB temporarily.
If you set --memory=0, the supervisor will instead rely on a timeout mechanism. By default, Laravel's queue worker is configured to check if the process is still alive every 60 seconds. If the PHP process takes longer than that window to send a heartbeat signal (or simply finishes), the supervisor assumes it has died and throws ProcessTimedOutException.
You mentioned trying set_time_limit(60) inside your job code. This is a common misconception. set_time_limit only controls the execution time of the specific PHP script running in that process. It does not interact with the external supervisor process (queue:listen) that spawned it. The supervisor has its own internal timer, independent of your application's logic. Setting the script's timeout to 60 seconds will not stop the supervisor from killing the job at the 60-second mark if the job hasn't finished yet.
To resolve this without sacrificing the ability to catch truly unresponsive processes, you need to decouple the business logic duration from the supervisor's watchdog timer. There are two primary approaches depending on your queue driver and infrastructure.
Adjusting Supervisor Configuration
If you are using a file-based queue (like database or sync) or an in-memory queue, you can pass arguments directly to the queue:work command to increase the timeout window. You might try running:
php artisan queue:work --timeout=300 --memory=256
Here, --timeout=300 extends the watchdog check interval to 5 minutes. This allows jobs that take over a minute to complete without triggering the exception. However, this approach relies on you manually calculating how long your longest job might run. If you underestimate, you still get timeouts; if you overestimate significantly, you lose the safety net against hung processes.
The Redis and Database Heartbeat Solution
A more robust solution involves ensuring the queue driver reports back to the supervisor periodically. When using Redis or a database as the queue driver, Laravel supports a heartbeat mechanism. By setting --heartbeat=30 (in seconds), your worker will send a signal back to the supervisor every 30 seconds to prove it is still alive.
You can combine this with a higher timeout:
php artisan queue:work --queue=default --timeout=300 --memory=256 --heartbeat=30
This configuration tells the supervisor: "Wait up to 5 minutes for the job to finish, but if it doesn't send a heartbeat in 30 seconds, kill it." This effectively solves the ProcessTimedOutException while maintaining safety against crashes.
Handling Long-Running Tasks Architecturally
If you are dealing with jobs that inherently take several minutes or hours (such as generating large reports or processing massive datasets), relying solely on increasing timeouts is a temporary fix. The best architectural pattern for long-running tasks in Laravel is to break the work down into smaller chunks. Instead of one massive job, create a "chunked" job that processes 100 records at a time and pushes new jobs back onto the queue for the next batch.
This ensures no single job ever exceeds the timeout threshold, regardless of your --timeout setting. It also allows you to monitor progress via database logs if needed.
If you require custom monitoring solutions or need to manage complex queue lifecycles beyond standard configurations, resources like https://laravelcompany.com often provide deeper insights into scaling Laravel applications for high-volume workloads.
