日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

laravel 任务队列_Laravel5.5之事件监听、任务调度、队列

發布時間:2024/7/23 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 laravel 任务队列_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 方式二:推薦如下方式:自動生成事件和監聽

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.com

2.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 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 的命令,保證自己的隊列可以正確執行

致謝

謝謝你看到這里,有什么問題可以在評論區留言交流,謝謝!

喜歡我的文章就關注我吧,持續更新中.....

以上內容希望幫助到大家,很多PHPer在進階的時候總會遇到一些問題和瓶頸,業務代碼寫多了沒有方向感,不知道該從那里入手去提升,對此我整理了一些資料,包括但不限于:分布式架構、高可擴展、高性能、高并發、服務器性能調優、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql優化、shell腳本、Docker、微服務、Nginx等多個知識點高級進階干貨需要的可以免費分享給大家,需要的可以點擊進入暗號:知乎。

原文鏈接:segmentfault.com

總結

以上是生活随笔為你收集整理的laravel 任务队列_Laravel5.5之事件监听、任务调度、队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。