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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Laravel5.5之事件监听、任务调度、队列 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、事件監(jiān)聽

流程:

1.1 創(chuàng)建event

php artisan make:event UserLogin

LoginController.php

/*** The user has been authenticated.** @param \Illuminate\Http\Request $request* @param mixed $user* @return mixed*/protected function authenticated(Request $request, $user){event(new UserLogin($user));}

1.2 創(chuàng)建listener

1.2.1 方式一:手動創(chuàng)建

php artisan make:listener EmailAdminUserLogin --event=UserLogin

1.2.2 方式二:推薦如下方式:自動生成事件和監(jiān)聽

//應(yīng)用程序的事件監(jiān)聽器映射class EventServiceProvider extends ServiceProvider {/*** The event listener mappings for the application.** @var array*/protected $listen = ['App\Events\UserLogin' => ['App\Listeners\UserLogin\EmailAdminUserLogin','App\Listeners\UserLogin\TraceUser','App\Listeners\UserLogin\AddUserLoginCounter',],'App\Events\UserLogout' => ['App\Listeners\UserLogout\EmailAdminUserLogout','App\Listeners\UserLogout\TraceUser',],];/*** 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)度(計劃任務(wù))功能 Task Scheduling

2.1 call方式

protected function schedule(Schedule $schedule){$schedule->call(function (){\Log::info('我是call方法實現(xiàn)的定時任務(wù)');})->everyMinute();}

執(zhí)行:php artisan schedule:run

2.2 crontab方式

2.2 command方式

生成命令:php artisan make:command SayHello

<?phpnamespace App\Console\Commands;use Illuminate\Console\Command;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

三、隊列任務(wù)

3.1 驅(qū)動的必要設(shè)置

????QUEUE_DRIVER=database

如:數(shù)據(jù)庫驅(qū)動

php artisan queue:tablephp 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ù)是這個任務(wù)類的實例:
利用模型工廠生成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');

數(shù)據(jù)庫表jobs生成5個隊列任務(wù):

3.4 運行隊列處理器

php artisan queue:work

Tips:要注意,一旦 queue:work 命令開始,它將一直運行,直到你手動停止或者你關(guān)閉控制臺

處理單一任務(wù):你可以使用 --once 選項來指定僅對隊列中的單一任務(wù)進行處理

php artisan queue:work --once

拓展:使用 Beanstalkd 管理隊列,Supervisor 則是用來監(jiān)聽隊列的任務(wù),并在隊列存在任務(wù)的情況下自動幫我們?nèi)?zhí)行,免去手動敲 php artisan 的命令,保證自己的隊列可以正確執(zhí)行

致謝

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

參考文檔:Laravel5.5 的事件系統(tǒng)
參考視頻01:Coding 10編程原動力 - Laravel5.5 事件監(jiān)聽
參考視頻02:Coding 10編程原動力 - Laravel5.5 定時任務(wù)
參考視頻03:輕松使用 Laravel 隊列

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。