laravel 任务队列_Laravel5.5之事件监听、任务调度、队列
流程:
1.1 創建event
php artisan make:event UserLoginLoginController.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=UserLogin1.2.2 方式二:推薦如下方式:自動生成事件和監聽
10年架構師領你架構-成長之路-(附面試題(含答案))
程序猿的生活:(騰訊T3-T4)打造互聯網PHP架構師教程目錄大全,只要你看完,薪資立馬提升2倍(持續更新)?zhuanlan.zhihu.com//應用程序的事件監聽器映射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
感謝大家一直來支持,這是我準備的1000粉絲福利
程序猿的生活:【1000粉絲福利】10年架構師分享PHP進階架構資料,助力大家都能30K?zhuanlan.zhihu.com2.2 crontab方式
2.2 command方式
生成命令:php artisan make:command SayHello
<?phpnamespace 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
大廠2000道面試題(含答案)
程序猿的生活:PHP面試題匯總,看完這些面試題助力你面試成功,工資必有20-25K?zhuanlan.zhihu.com三、隊列任務
3.1 驅動的必要設置
QUEUE_DRIVER=database
如:數據庫驅動
php artisan queue:tablephp artisan migrate3.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:workTips:要注意,一旦 queue:work 命令開始,它將一直運行,直到你手動停止或者你關閉控制臺
處理單一任務:你可以使用 --once 選項來指定僅對隊列中的單一任務進行處理
php artisan queue:work --once拓展:使用 Beanstalkd 管理隊列,Supervisor 則是用來監聽隊列的任務,并在隊列存在任務的情況下自動幫我們去執行,免去手動敲 php artisan 的命令,保證自己的隊列可以正確執行
致謝
謝謝你看到這里,有什么問題可以在評論區留言交流,謝謝!
喜歡我的文章就關注我吧,持續更新中.....
以上內容希望幫助到大家,很多PHPer在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那里入手去提升,對此我整理了一些資料,包括但不限于:分布式架構、高可擴展、高性能、高并發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階干貨需要的可以免費分享給大家,需要的可以點擊進入暗號:知乎。
原文鏈接:segmentfault.com
總結
以上是生活随笔為你收集整理的laravel 任务队列_Laravel5.5之事件监听、任务调度、队列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hive mysql性能_Hive数据库
- 下一篇: 宋浩 概率统计 笔记_推论统计分析学习笔