8

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 ?

3 Answers 3

12

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
Sign up to request clarification or add additional context in comments.

3 Comments

This helped me immensely -- no idea why this isn't in the documentation
Also, remember to run php artisan config:clear afterward
Is this database driver really asynchronous? I tried running it but doesn't look like it's running async operations. Every job is sequential and not in parallel.
2

In order for the job to be queued, the job class needs to implement Illuminate\Contracts\Queue\ShouldQueue interface - make sure it's true for your class.

You can find more info on queuing jobs here: http://laravel.com/docs/5.1/queues#writing-job-classes

5 Comments

It already implemented ShouldQueue , but should it act async on localhost ?
How do you know it's run synchronously? You have the queue:listen running so it might still be handled asynchronously but you're getting logs in correct order because it might be handled why your code is doing sleep(). Stop queue:listen process, dispatch the job without it running and see if the job gets executed.
thank you for help , I mean for me it seems to be sync, but I want it to be async ,, how to achieve that ?
When you stop "queue:listen" - does the job get handled?
yes , it works , but when the queue:listen works or not , its sync. while I want it to work async , I'm using the localhost , is that affect the use of queuing ?
0

Use this command to create the job:

   php artisan make:job SendGiftCommand --queued    

Refer this link for defining the job:

 https://laravel.com/docs/5.1/queues#writing-job-classes

Then pass declare the job and dispatch the job in this manner:

    $processGift = new sendGiftCommand($model1);
    $this->dispatch($processGift);

Also refer Supervisor Configuration on the above mentioned link further for continuously listening to the queue or automatically restarting the command queue:listen if they fail.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.