Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dagfooyo's avatar

Queued tasks are executing as part of the controller, causing app to freeze

OK I'm new to job queues in Laravel but I've read all the documentation and I thought I understood them.

My app needs to queue hundreds of jobs to run one-after-another in the background.

I'm testing with dummy jobs first that just log their status, and sometimes error out. Everything seemed to be working. When I ran it, the logs filled right up with hundreds of messages in the correct order.

Since each actual task will take between 5-10 seconds to complete, I added in a simulated delay by putting a sleep() command in the handle function. So I pressed "go" and the app... just... hung. Because it wasn't queueing the tasks after all. It was trying to execute ALL of them before it would load the next page! What am I doing wrong here? Here's the code that calls the job, from inside a controller:

foreach($to_publish as $info){
    Publish::dispatch($info);
  }

It should be adding it to the queue and running it in the background, right? I don't get it.

Here's the test handle() function I created in the Publish job:

public function handle()
{
  try{
    if(rand(0,20) < 3){
      $x = 1/0;
    }
    sleep(1);
    $this->my_log('info' , 'Published successfully');
  } catch (Exception $e) {
    $m = $e->getMessage();
    $this->my_log('error', $m);
  }
}

What did I miss? What am I doing wrong? There's a lot of documentation about setting up workers to handle the tasks but they seemed to just be running so I thought it was setting one up automatically.

0 likes
11 replies
Nash's avatar
Nash
Best Answer
Level 20

What queue driver are you using in config/queue.php? Check QUEUE_DRIVER in your .env file and make sure it's not set to sync because that will execute the jobs immediately.

1 like
dagfooyo's avatar

You were right - it was set to 'sync'. I just set it to "database" which is what I thought I had it set to in the first place. However it's still hanging and none of the jobs are getting added to the jobs table.

I ran php artisan config:clear and it didn't help.

Snapey's avatar

just check its 'taken' by going into tinker and running config('queue')

dagfooyo's avatar

It wasn't. And I realized my mistake - I'd changed the default in queue.php but I'd forgotten to change it in .env so it was still pulling that value. Still a bit new to Laravel. It's working now. Thanks for the help!

dagfooyo's avatar

Now that I've got it set up to use the database queue, it populates the jobs table but then it never processes any of them. When start a worker using php artisan queue:work (or listen, or any variation of the same) it still doesn't do anything. I can't get any jobs to process at all.

Nash's avatar

Try php artisan queue:restart and then try to run the queue worker again (queue:work, listen etc.).

Are you sure that the code in your jobs is actually working or could the jobs be failing? Also, do you have a table for failed jobs?

php artisan queue:failed-table
php artisan migrate

https://laravel.com/docs/5.6/queues#dealing-with-failed-jobs

dagfooyo's avatar

I tried queue:restart, no dice. The job queue worked fine when it was set to sync, ran through instantaneously.

Yes, I created a failed jobs table back when I was first setting up the queue. None of the tasks are making it to the failed jobs table. None of them are processing at all, they're just sitting there in the jobs table.

Snapey's avatar

should the jobs process immediately or is there any sort of delay?

dagfooyo's avatar

They should process immediately. I removed the delay.

dagfooyo's avatar

I've now commented all the code of my job out and just have it logging how far it gets and it's logging that the constructor is running and nothing after that point. And now it's not even showing up in the job queue. I feel like I'm making negative progress here.

dagfooyo's avatar

Weird. I just ran it again without changing anything and all the jobs processed. Must've been ghosts or gremlins or something. I don't even know.

Please or to participate in this conversation.