Tìm hiểu Queue trong Laravel
Laravel Queue là gì?
Một hàng đợi (queue) là một danh sách những việc cần làm (job) được quản lý theo thứ tự. Khi chúng ta muốn thêm một công việc (job) vào hàng đợi, job phải implement interface Illuminate\Contracts\Queue\ShouldQueue.

Laravel Queue driver được sử dụng để quản lý các job như thêm job vào hàng đợi, lấy job ra khỏi hàng đợi. Laravel có thể làm việc với nhiều các driver khác nhau như database, Redis, Amazon SQS…
Hàng đợi cho phép bạn trì hoãn một công việc mất nhiều thời gian đến một thời điểm nào nó mới xử lý. Laravel cung cấp một API thống nhất cho rất nhiều các hàng đợi ở backend khác nhau. Để hiểu nhanh các khái niệm mới chúng ta hãy bắt đầu bằng ví dụ: Bạn hãy tưởng tượng website của bạn có nhiều người vào xem và có thể đăng ký tài khoản, người đó đang ở trang đăng ký và nhập các thông tin vào form đăng ký. Khi đó chúng ta muốn thực hiện các công việc sau:
- Kiểm tra thông tin nhập
- Gửi một email
Một số thiết lập cho queue.
Thực hiện tạo một job mới bằng câu lệnh:
php artisan make:job SendEmailJob<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendMailable;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
// function khởi tạo
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Mail::to('mail@appdividend.com')->send(new SendMailable());
}
}
Trong file gửi job mới được tạo ra từ EmailController. Code chức năng sẽ như sau.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Jobs\SendEmailJob;
class EmailController extends Controller
{
public function sendEmail()
{
dispatch(new SendEmailJob());
echo 'email sent';
}
}
public function sendEmail()
{
$emailJob = (new SendEmailJob())->delay(Carbon::now()->addSeconds(3));
dispatch($emailJob);
echo 'email sent';
}
Mở terminal và gõ lệnh sau để start queue
php artisan queue:work[2017-12-21 13:58:14] Processing: App\Jobs\SendEmailJob
[2017-12-21 13:58:19] Processed: App\Jobs\SendEmailJob
kiểm tra trong mailtrap, bạn sẽ nhìn thấy email đã được gửi.
-------------ảnh-----------------------------------
Chúng ta có thể tạo ra bao nhiêu job nếu chúng ta muốn nhưng phải đảm bảo queue đang hoạt động và tất cả config là chính xác.
Thiết lập thời gian timeout của job trong queue
Bạn có thể thiết lập thời gian timeout của các job bằng cách sử dụng câu lệnh artisan
php artisan queue:work --timeout=60<?phpnamespace App\Jobs;
class SendWelcome implements ShouldQueue{ /** * Số giây job có thể chạy trước khi timeout * * @var int */ public $timeout = 60;}php artisan queue:workphp artisan queue:restartphp artisan queue:work --sleep=3$ yum install python-setuptools$ easy_install supervisorCài đặt supervisor trên Ubuntu
#sudo apt-get install supervisor[program:mail-worker]process_name=%(program_name)s_%(process_num)02dcommand=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=4autostart=trueautorestart=trueuser=forgenumprocs=6redirect_stderr=truestdout_logfile=/home/forge/app.com/worker.log$supervisorctl reread$supervisorctl update$supervisorctl start laravel-worker:*This is h1
content 1
This is h2
content 2
This is h3
content 3
This is h4
content 4
This is h5
content 5
This is h6
content 6
Đây là
blockqoute nhé
oke abc
Bình luận