11

I'm trying to use Laravel 5.7 jobs queue to make some insertions/updates in my database and i problably made something wrong because when the job is called its seems to be blocking my application, therefore, not running asynchronously. My code is in the following structure:

.env

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

queue.php

'default' => env('QUEUE_CONNECTION', 'sync'),

'connections' => [

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

    'database' => [
        'driver' => 'database',
        'table' => 'jobs',
        'queue' => 'default',
        'retry_after' => 90,
    ],

job_caller.php

method_name(){ 
  InsereProspeccao::dispatch($path, $evento, $equipe)->onQueue('jobs');
  retur some_msg_to_user;
}

job_name.php

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class InsereProspeccao implements ShouldQueue{

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

  private $path = '';
  private $evento = '';
  private $equipe = '';


 public function __construct($path, $evento, $equipe){
     $this->path = $path;
     $this->evento = $evento;
     $this->equipe = $equipe;        
 }

   public function handle(){
      //all program logic
      //access DB for insert/update
   }

}

Obs.: I'M READING THE DOCUMENTATION, BUT I CANT FIND WHAT'S GOING WRONG !

Wall-E
  • 438
  • 1
  • 6
  • 18

2 Answers2

33

You are using QUEUE_CONNECTION=sync which basically has synchronous behavior.

Please follow below steps :

  • Run php artisan queue:table which will create a migration for jobs table autimatically

  • Run php artisan migrate which will create the table by running migration

  • Change QUEUE_CONNECTION=database and as per default, it will automatically take jobs table to manage the queues.

  • Run php artisan config:clear to clear application configuration cache

That should be good to go. Check documentation for more help.

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
  • 5
    The one important additional step is [running the queue worker](https://laravel.com/docs/5.7/queues#running-the-queue-worker) which is where most people seem to get a bit overwhelmed by. – apokryfos Feb 23 '19 at 08:11
  • well, @apokryfos. this one is new to me. So, when i deploy my project to a server i have to access it and install supervisor ? – Wall-E Feb 24 '19 at 19:27
  • There is no requirement as such to use supervisor, it would work without it as well. Its just to restart the queues if anything fails, but definitely good to have – Mihir Bhende Feb 24 '19 at 19:29
  • 1
    @Wally the queue worker is a background process so as a minimum you need to be able to run background processes on your server. Supervisor is optional but nice to use since it makes things easier. – apokryfos Feb 25 '19 at 10:00
  • @apokryfos, now how do i run this when i deploy my app ? Do i have to acess the server via SSL and let the worker listening ? – Wall-E Feb 25 '19 at 18:13
  • 1
    @Wally yes you would need to do that. If your server is linux based you can use something like [`screen`](https://linux.die.net/man/1/screen) to start the queue listener and then dettach the screen to allow it to run in the background. – apokryfos Feb 25 '19 at 19:37
  • 1
    You can also suffix the command with `&` so it runs in backgrund. like `php artisan queue:work &` – Mihir Bhende Feb 25 '19 at 21:18
  • I had running it with `artisan serve` , I had to restart it to make it takes the change on file .env – wnasich Oct 01 '19 at 16:15
  • Got two answers for the price of one question. Thanks @apokryfos. I had forgotten to start the queue worker. – th3coop Aug 13 '21 at 03:08
1

Try this : QUEUE_CONNECTION=database and it should be good to go.

You can also set up rabbitmq or other drivers, because their implementation is much more advanced and will be more production - ready. But database is a good start.

Giorgi Lagidze
  • 773
  • 4
  • 24