Laravel Queues

azibom

Mohammad Reza

Posted on June 16, 2020

Laravel Queues

So when we use queues
When you got many request that need to have a time consuming process you can solve this problem by using message queues

lets start

Create queue table

php artisan queue:table
php artisan migrate
Enter fullscreen mode Exit fullscreen mode

Go to the QUEUE_CONNECTION and change it to some things like this

QUEUE_CONNECTION=database 
Enter fullscreen mode Exit fullscreen mode

Create a job

php artisan make:job SendMail
Enter fullscreen mode Exit fullscreen mode

Change your job like this

<?php

namespace App\Jobs;

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

class SendMail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    private $title;
    private $body;
    private $to;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($title, $body, $to)
    {
        $this->title = $title;
        $this->body = $body;
        $this->to = $to;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        echo "Start sending email".PHP_EOL;
        sleep(2);
        echo "Email sended to {$this->to}".PHP_EOL;
    }
}

Enter fullscreen mode Exit fullscreen mode

Call dispatcher in you controller or anywhere you want

SendMail::dispatch("hi","how are you","alex@gmail.com");
Enter fullscreen mode Exit fullscreen mode

Now you just need to start your queue with a command

php artisan queue:work
Enter fullscreen mode Exit fullscreen mode

Or

php artisan queue:listen
Enter fullscreen mode Exit fullscreen mode

And if you want to know what is the difference between those read this

"Alternatively, you may run the queue:listen command. When using the queue:listen command, you don't have to manually restart the worker when you want to reload your updated code or reset the application state; however, this command is not as efficient as queue:work:"

After that you should see sth like this

[2020-06-16 17:04:08][161] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:10][161] Processed:  App\Jobs\SendMail
[2020-06-16 17:04:10][162] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:12][162] Processed:  App\Jobs\SendMail
[2020-06-16 17:04:12][163] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:14][163] Processed:  App\Jobs\SendMail
[2020-06-16 17:04:14][164] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:16][164] Processed:  App\Jobs\SendMail
[2020-06-16 17:04:16][165] Processing: App\Jobs\SendMail
Start sending email
Email sended to alex@gmail.com
[2020-06-16 17:04:18][165] Processed:  App\Jobs\SendMail
Enter fullscreen mode Exit fullscreen mode

And done

Feel free to ask any questions

💖 💪 🙅 🚩
azibom
Mohammad Reza

Posted on June 16, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Laravel Queues
php Laravel Queues

June 16, 2020