下面由laravel教程欄目給大家介紹Laravel5.5之事件監(jiān)聽、任務(wù)調(diào)度、隊(duì)列,希望對(duì)需要的朋友有所幫助!
Laravel5.5之事件監(jiān)聽、任務(wù)調(diào)度、隊(duì)列
一、事件監(jiān)聽
流程:
1.1 創(chuàng)建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 創(chuàng)建listener
1.2.1 方式一:手動(dòng)創(chuàng)建
php artisan make:listener EmailAdminUserLogin --event=UserLogin
1.2.2 方式二:推薦如下方式:自動(dòng)生成事件和監(jiān)聽
//應(yīng)用程序的事件監(jiān)聽器映射 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) { // }); } }
生成事件 & 監(jiān)聽器:php artisan event:generate
二、Laravel 的任務(wù)調(diào)度(計(jì)劃任務(wù))功能 Task Scheduling
2.1 call方式
protected function schedule(Schedule $schedule) { $schedule->call(function (){ Log::info('我是call方法實(shí)現(xiàn)的定時(shí)任務(wù)'); })->everyMinute(); }
執(zhí)行: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(); }
執(zhí)行:php artisan schedule:run
三、隊(duì)列任務(wù)
3.1 驅(qū)動(dòng)的必要設(shè)置
QUEUE_DRIVER=database
如:數(shù)據(jù)庫驅(qū)動(dòng)
php artisan queue:table php artisan migrate
3.2 創(chuàng)建任務(wù)
生成任務(wù)類:
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 分發(fā)任務(wù)
你寫好任務(wù)類后,就能通過 dispatch
輔助函數(shù)來分發(fā)它了。唯一需要傳遞給 dispatch
的參數(shù)是這個(gè)任務(wù)類的實(shí)例:
利用模型工廠生成30個(gè)用戶:
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');
數(shù)據(jù)庫表jobs
生成5個(gè)隊(duì)列任務(wù):
3.4 運(yùn)行隊(duì)列處理器
php artisan queue:work
Tips:要注意,一旦 queue:work
命令開始,它將一直運(yùn)行,直到你手動(dòng)停止或者你關(guān)閉控制臺(tái)
處理單一任務(wù):你可以使用 --once
選項(xiàng)來指定僅對(duì)隊(duì)列中的單一任務(wù)進(jìn)行處理
php artisan queue:work --once
拓展:使用 Beanstalkd
管理隊(duì)列,Supervisor
則是用來監(jiān)聽隊(duì)列的任務(wù),并在隊(duì)列存在任務(wù)的情況下自動(dòng)幫我們?nèi)?zhí)行,免去手動(dòng)敲 php artisan
的命令,保證自己的隊(duì)列可以正確執(zhí)行
《
相關(guān)推薦
- RakSmart服務(wù)器成本優(yōu)化策略
- 2025年國內(nèi)免費(fèi)AI工具推薦:文章生成與圖像創(chuàng)作全攻略
- 自媒體推廣實(shí)時(shí)監(jiān)控從服務(wù)器帶寬到用戶行為解決方法
- 從流量變現(xiàn)到信任變現(xiàn):個(gè)人站長的私域運(yùn)營方法論
- AI時(shí)代,個(gè)人站長如何用AI工具實(shí)現(xiàn)“一人公司”
- 個(gè)人站長消亡論?從“消失”到“重生”的三大破局路徑
- raksmart法蘭克福云服務(wù)器延遲高嗎?
- 常見的海外站群服務(wù)器有哪些?地區(qū)選擇與核心優(yōu)勢(shì)解析