Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Queues In Laravel

I'm trying to implement Queuing, but the result is not async And I have Applied the following

config/queue.php
'default' => env('QUEUE_DRIVER', 'database'),
    'connections' => [

    'sync' => [
        'driver' => 'sync',
    ],

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'expire' => 60,
    ],
   ] 

and then applied the following commands php artisan queue:table php artisan migrate

and then run

php artisan queue:listen

and here is the functionality

SomethingController.php
   $model1 = new \App\Model1;
public function store(){
     Log::debug('before dispatch');
     $this->dispatch(new SendGiftCommand($model1));
     Log::debug('before dispatch');
     return true;
}



SendGiftCommand.php
{
    Log::debug('handle');
    SendGift::SendGiftAgedBased($this->model1);
    sleep(4);
    Log::debug('SendGiftCommand end');
}
SendGift.php
public static function SendGiftAgedBased(Model1 $model1){
  Log::debug('inside SendGiftAgedBased');
} 

even the process has worked but its not async, and it waits for the command to finish to return the response in the controller

And I git the Logs in this order

 [2015-12-09 16:28:42] local.DEBUG: before dispatch  
 [2015-12-09 16:28:42] local.DEBUG: handle  
 [2015-12-09 16:28:42] local.DEBUG: inside SendGiftAgedBased   
 [2015-12-09 16:28:46] local.DEBUG: SendGiftCommand end  
 [2015-12-09 16:28:46] local.DEBUG: after dispatch 

should it be working on Localhost ?

like image 347
user2099451 Avatar asked Dec 09 '15 14:12

user2099451


People also ask

How do I make a queue Job asynchronous in Laravel?

For a Laravel queue job to appear as an asynchronous process in your API, you must add the CloudCreativity\LaravelJsonApi\Queue\ClientDispatchable trait to it and use this to dispatch the job.

What is async Laravel?

Laravel Async is a package that provides a straightforward way to run code asynchronous and parallel based on the Spatie Async wrapper for the Laravel application. You can install the Laravel Async package by using Composer: The package will automatically register itself.

How to Handle switches between processes in Laravel?

Switching between processes (context switching) also has an overhead. You could use Laravel queues and start a fixed number of workers (processes) and keep them alive to process your tasks. That way you won't have to create new processes each time you want to run something async.

How do I monitor a queue in Laravel?

Monitoring Your Queues If your queue receives a sudden influx of jobs, it could become overwhelmed, leading to a long wait time for jobs to complete. If you wish, Laravel can alert you when your queue job count exceeds a specified threshold. To get started, you should schedule the queue:monitorcommand to run every minute.


1 Answers

I had the same problem with jobs not being asynchronous and this worked for me :

  1. Edit .env and change the QUEUE_DRIVER setting from sync to database (editing config/queue.php wasn't enough)
  2. Restart your processes
like image 101
Jocelyn Avatar answered Oct 13 '22 16:10

Jocelyn