1. 程式人生 > >Laravel redis佇列

Laravel redis佇列

1、配置.env

QUEUE_DRIVER=redis

 2、建立佇列

php artisan make:job MailuserNewPost

app/Jobs/MailuserNewPost.php

...
public $user;

    public function __construct(User $user)
    {
    $this->user=$user;
    }


    public function handle()
    {
        \Log::info('佇列佇列佇列2'.$this->user->name);
    }

   public function failed(\Exception $exception)
   {
       \Log::error('失敗');
    }

....

3、派發任務

在web.php裡測試

use App\Models\User;
use App\Jobs\MailuserNewPost;

Route::get('/queue',function (){
    $users=User::all();


    foreach ($users as $user){
        dispatch(new MailuserNewPost($user));
        //dd($user->name);
    }
    dd('done');
});

訪問路由,派發任務

3、

 composer require predis/predis ~1.0

 

composer require laravel/horizon
php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"
artisan queue:work --timeout=10 --tries=5

4、開啟/horizon面板