欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          解析Laravel5.5之事件監(jiān)聽(tīng)、任務(wù)調(diào)度、隊(duì)列

          下面由laravel教程欄目給大家介紹Laravel5.5之事件監(jiān)聽(tīng)、任務(wù)調(diào)度、隊(duì)列,希望對(duì)需要的朋友有所幫助!

          Laravel5.5之事件監(jiān)聽(tīng)、任務(wù)調(diào)度、隊(duì)列

          一、事件監(jiān)聽(tīng)

          流程:

          解析Laravel5.5之事件監(jiān)聽(tīng)、任務(wù)調(diào)度、隊(duì)列

          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)聽(tīng)

          //應(yīng)用程序的事件監(jiān)聽(tīng)器映射  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)聽(tīng)器: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方式

          解析Laravel5.5之事件監(jiān)聽(tīng)、任務(wù)調(diào)度、隊(duì)列

          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()     {         //書(shū)寫(xiě)處理邏輯         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ù)庫(kù)驅(qū)動(dòng)

          php artisan queue:table  php artisan migrate

          3.2 創(chuàng)建任務(wù)

          生成任務(wù)類(lèi):

          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ù)

          你寫(xiě)好任務(wù)類(lèi)后,就能通過(guò) dispatch 輔助函數(shù)來(lái)分發(fā)它了。唯一需要傳遞給 dispatch 的參數(shù)是這個(gè)任務(wù)類(lèi)的實(shí)例:
          利用模型工廠生成30個(gè)用戶:

          解析Laravel5.5之事件監(jiān)聽(tīng)、任務(wù)調(diào)度、隊(duì)列

              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ù)庫(kù)表jobs生成5個(gè)隊(duì)列任務(wù):

          解析Laravel5.5之事件監(jiān)聽(tīng)、任務(wù)調(diào)度、隊(duì)列

          3.4 運(yùn)行隊(duì)列處理器

          php artisan queue:work

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

          處理單一任務(wù):你可以使用 --once 選項(xiàng)來(lái)指定僅對(duì)隊(duì)列中的單一任務(wù)進(jìn)行處理

          php artisan queue:work --once

          解析Laravel5.5之事件監(jiān)聽(tīng)、任務(wù)調(diào)度、隊(duì)列

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

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)