ホームページ  >  記事  >  PHPフレームワーク  >  Laravel5.5のイベント監視、タスクスケジューリング、キューの分析

Laravel5.5のイベント監視、タスクスケジューリング、キューの分析

藏色散人
藏色散人転載
2021-06-19 16:03:102281ブラウズ

次のチュートリアルコラムでは、Laravel5.5のイベント監視、タスクスケジューリング、キューについて紹介しますので、困っている方の参考になれば幸いです。 Laravel5.5 イベント監視、タスクスケジューリング、キュー

1.イベント監視

プロセス:

1.1 イベントを作成する

php artisan make:event UserLogin
Laravel5.5のイベント監視、タスクスケジューリング、キューの分析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 リスナーを作成する1.2.1 方法 1: 手動create

php artisan make:listener EmailAdminUserLogin --event=UserLogin

1.2.2 方法 2:

次の方法をお勧めします:

イベントとリスナーを自動的に生成する

//应用程序的事件监听器映射

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) {
            //
        });
    }
}

イベントとリスナーを生成する:

phpArtisanevent:generate

2. Laravelのタスクスケジューリング(計画タスク)関数Task Scheduling2.1 callメソッド

protected function schedule(Schedule $schedule)
    {
        $schedule->call(function (){
            \Log::info('我是call方法实现的定时任务');
        })->everyMinute();
    }

Execution:

php 職人スケジュール:run

2.2 crontab メソッド

2.2 コマンド メソッド

Laravel5.5のイベント監視、タスクスケジューリング、キューの分析

コマンドの生成:

php 職人 make:command SayHello

<?php namespace App\Console\Commands;

use Illuminate\Console\Command;

class SayHello extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = &#39;message:hi&#39;;

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = &#39;Command description&#39;;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //书写处理逻辑
        \Log::info(&#39;早上好,用户&#39;);
    }
}
Kernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command('message:hi')
             ->everyMinute();
}

実行:

php 職人のスケジュール:run

3. タスクをキューに入れる3.1 必要なドライバーの設定

QUEUE_DRIVER=database

例: データベース ドライバー

php artisan queue:table

php 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');

データベース テーブル

jobsLaravel5.5のイベント監視、タスクスケジューリング、キューの分析5 つのキュー タスクを生成:

#3.4 キュー プロセッサを実行

php artisan queue:work

Laravel5.5のイベント監視、タスクスケジューリング、キューの分析ヒント:

queue:work

コマンドが開始されると、手動で停止するかコンソールを閉じるまで実行されることに注意してください

単一タスクの処理: --once

オプションを使用して、キュー内の単一タスクのみが処理されるように指定できます

php artisan queue:work --once
拡張子:

#BeanstalkdLaravel5.5のイベント監視、タスクスケジューリング、キューの分析 を使用してキューを管理し、

Supervisor

を使用してキュー内のタスクを監視し、キューにタスクがある場合は自動的に実行を支援します。これにより、キューが正しく実行されることを確認するために php 職人 コマンドを手動で入力する必要がなくなりました。《関連する推奨事項: 最新の 5 つの Laravel ビデオ チュートリアル

以上がLaravel5.5のイベント監視、タスクスケジューリング、キューの分析の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はsegmentfault.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。