下面由laravel教程欄目給大家介紹Laravel5.5之事件監聽、任務調度、隊列,希望對需要的朋友有所幫助!
Laravel5.5之事件監聽、任務調度、隊列
一、事件監聽
流程:
1.1 創建event
php artisan make:event UserLogin
LoginController.php
/** * The user has been authenticated. * * @param IlluminateHttpRequest $request * @param mixed $user * @return mixed */ protected function authenticated(Request $request, $user) { event(new UserLogin($user)); }
1.2 創建listener
1.2.1 方式一:手動創建
php artisan make:listener EmailAdminUserLogin --event=UserLogin
1.2.2 方式二:推薦如下方式:自動生成事件和監聽
//應用程序的事件監聽器映射 class EventServiceProvider extends ServiceProvider { /** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'AppEventsUserLogin' => [ 'AppListenersUserLoginEmailAdminUserLogin', 'AppListenersUserLoginTraceUser', 'AppListenersUserLoginAddUserLoginCounter', ], 'AppEventsUserLogout' => [ 'AppListenersUserLogoutEmailAdminUserLogout', 'AppListenersUserLogoutTraceUser', ], ]; /** * Register any events for your application. * * @return void */ public function boot() { parent::boot(); Event::listen('event.*', function ($eventName, array $data) { // }); } }
生成事件 & 監聽器:php artisan event:generate
二、Laravel 的任務調度(計劃任務)功能 Task Scheduling
2.1 call方式
protected function schedule(Schedule $schedule) { $schedule->call(function (){ Log::info('我是call方法實現的定時任務'); })->everyMinute(); }
執行:php artisan schedule:run
2.2 crontab方式
2.2 command方式
生成命令:php artisan make:command SayHello
<?php namespace AppConsoleCommands; use IlluminateConsoleCommand; class SayHello extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'message:hi'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { //書寫處理邏輯 Log::info('早上好,用戶'); } }
Kernel.php
protected function schedule(Schedule $schedule) { $schedule->command('message:hi') ->everyMinute(); }
執行:php artisan schedule:run
三、隊列任務
3.1 驅動的必要設置
QUEUE_DRIVER=database
如:數據庫驅動
php artisan queue:table php artisan migrate
3.2 創建任務
生成任務類:
php artisan make:job SendReminderEmail
class SendReminderEmail implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $user; /** * Create a new job instance. * * @param User $user */ public function __construct(User $user) { $this->user = $user; } /** * Execute the job. * * @return void */ public function handle() { Log::info('send reminder email to user' . $this->user->email); } }
3.3 分發任務
你寫好任務類后,就能通過 dispatch
輔助函數來分發它了。唯一需要傳遞給 dispatch
的參數是這個任務類的實例:
利用模型工廠生成30個用戶:
public function store(Request $request) { $users = User::where('id','>',24)->get(); foreach ($users as $user){ $this->dispatch(new SendReminderEmail($user)); } return 'Done'; }
Route::get('/job', 'UserController@store');
數據庫表jobs
生成5個隊列任務:
3.4 運行隊列處理器
php artisan queue:work
Tips:要注意,一旦 queue:work
命令開始,它將一直運行,直到你手動停止或者你關閉控制臺
處理單一任務:你可以使用 --once
選項來指定僅對隊列中的單一任務進行處理
php artisan queue:work --once
拓展:使用 Beanstalkd
管理隊列,Supervisor
則是用來監聽隊列的任務,并在隊列存在任務的情況下自動幫我們去執行,免去手動敲 php artisan
的命令,保證自己的隊列可以正確執行
《