ホームページ  >  記事  >  PHPフレームワーク  >  laravelのスケジュールタスクの使い方と原理を詳しく解説

laravelのスケジュールタスクの使い方と原理を詳しく解説

WBOY
WBOY転載
2022-05-30 11:52:235513ブラウズ

この記事では、laravel に関する関連知識を提供し、主にスケジュールされたタスクの使用法と原則を紹介します。また、アプリケーションのシナリオに従ってスケジュールされたタスクの関連問題についても説明します。皆様のお役に立てれば幸いです。

laravelのスケジュールタスクの使い方と原理を詳しく解説

[関連する推奨事項: laravel ビデオ チュートリアル ]

アプリケーション シナリオ

Web サイト システムには、多くの場合、スケジュールされたタスクを実行する必要があります。たとえば、プッシュ サブスクリプション メッセージ、統計関連データなど Linux では一般的に crontab を使用してスケジュールされたタスクの設定と管理を行いますが、タスクの数が増えると、スケジュールされたタスクの管理はさらに面倒になり、管理が混乱しやすくなります。この問題に対するLaravelの解決策は、スケジュールタスクを1つだけ設定し、ビジネス内のすべてのスケジュールタスクをこのスケジュールタスク上で処理・判定することで、コードレベルでのスケジュールタスクの管理を実現することです。

基本的な使用方法

最初に crontab を設定します:

* * * * * php artisan schedule:run >> /dev/null 2>&1

上記は、スケジュールされたタスクを 1 分ごとに実行するように設定することを意味します。具体的なビジネス構成は App\Console\ に配置されます。カーネル スケジュールメソッド内:

class Kernel extends ConsoleKernel{
    Protected function schedule(Schedule $schedule)
    {
                //综合数据统计
        $schedule->command('complex_data_log')
        ->everyMinute() //每分钟执行一次(除此之外还有,每五、十、十五、三十...,不同方法设置的默认时间不同)
        ->withoutOverlapping() //防止重复执行
        ->onOneServer() //在单台服务器上跑
        ->runInBackground() //任务后台运行
        //->appendOutputTo('log_path')//日志输出,默认追加
        ->sendOutputTo('log_path'); //日志输出,默认覆盖先前日志
    }}

原理分析:

基本原理:
スケジュール:run この仕様は、vendor\illuminate\console\Scheduling\ にあります。 ScheduleRunCommand クラス 内部で定義されており、定義形式は一般的なスケジュールされたタスクと同じです:

/**
 * The console command name.
 *
 * @var string
 */protected $name = 'schedule:run';

laravel がコマンドを解析すると、ScheduleRunCommand クラスはカーネル クラスのコマンド配列とマージされます:

	/**
     * Get the commands to add to the application.
     *
     * @return array
     */
    protected function getCommands()
    {
        return array_merge($this->commands, [
            'Illuminate\Console\Scheduling\ScheduleRunCommand',
        ]);
    }

つまり、phpArtisanschedule:run コマンドはフレームワークの組み込みコマンドです。
コマンドが開始されると、デフォルトでクラス内のハンドル メソッドが検索されて実行されます:

/** vendor\illuminate\console\Command.php
 * Execute the console command.
 * 
 * @param  \Symfony\Component\Console\Input\InputInterface  $input
 * @param  \Symfony\Component\Console\Output\OutputInterface  $output
 * @return mixed
 */protected function execute(InputInterface $input, OutputInterface $output){
    return $this->laravel->call([$this, 'handle']);}

php 職人のスケジュール:ラン コマンドは、Kernel::schedule に登録されているすべての命令を毎分スキャンし、命令が実行サイクルに到達したかどうかを判断し、到達した場合は、実行するキューにその命令をプッシュします:

    /**
     * Schedule the event to run every minute.
     * 代码每分钟执行一次
     * @return $this
     */
    public function everyMinute()
    {
        return $this->spliceIntoPosition(1, '*');
    }
    
    /**
     * Splice the given value into the given position of the expression.
     * 拼接定时任务表达式
     * @param  int  $position
     * @param  string  $value
     * @return $this
     */
    protected function spliceIntoPosition($position, $value)
    {
        $segments = explode(' ', $this->expression);

        $segments[$position - 1] = $value;

        return $this->cron(implode(' ', $segments));
    }

ScheduleRunCommand::handle 関数:

/**
     * Execute the console command.
     * 
     * @return void
     */
    public function handle()
    {
        foreach ($this->schedule->dueEvents($this->laravel) as $event) {
            if (! $event->filtersPass($this->laravel)) {
                continue;
            }
            $this->runEvent($event);
        }
    }

タスクの重複を回避します:
単一のスケジュールされたタスクの実行時間が長すぎる場合があります。次の実行時刻を過ぎても、最後の実行タスクが完了していません。このとき、タスクの重複を避けるために withoutOverlapping() メソッドを使用できます。 。 withoutOverlapping メソッドで、対応するタスクをロックします (onOneServer メソッドにも同じことが当てはまります):

public function create(Event $event){
    return $this->cache->store($this->store)->add(
        $event->mutexName(), true, $event->expiresAt
    );}

対応するタスク ロックが取得された場合にのみ、タスクを実行できます:

/**
     * Run the given event.
     * 运行任务
     * @param  \Illuminate\Contracts\Container\Container  $container
     * @return void
     */
    public function run(Container $container)
    {
        if ($this->withoutOverlapping &&
            ! $this->mutex->create($this)) {
            return;
        }
        
        //判断是否是后台运行
        $this->runInBackground
                    ? $this->runCommandInBackground($container)
                    : $this->runCommandInForeground($container);
    }

タスクはバックグラウンドで実行されます :
スケジュールされたタスクは順番に実行されるため、前のタスクの実行時間が長すぎると、次のタスクの実行時間に影響を及ぼします。 runInBackground メソッドを使用してバックグラウンドでタスクを実行します。これはシェルに似ています。& の役割:

/**
     * Build the command for running the event in the background.
     * 构建定时任务后台运行语句
     * @param  \Illuminate\Console\Scheduling\Event  $event
     * @return string
     */
    protected function buildBackgroundCommand(Event $event)
    {
        $output = ProcessUtils::escapeArgument($event->output);

        $redirect = $event->shouldAppendOutput ? ' >> ' : ' > ';

        $finished = Application::formatCommandString('schedule:finish').' "'.$event->mutexName().'"';

        return $this->ensureCorrectUser($event,
            '('.$event->command.$redirect.$output.' 2>&1 '.(windows_os() ? '&' : ';').' '.$finished.') > '
            .ProcessUtils::escapeArgument($event->getDefaultOutput()).' 2>&1 &'
        );
    }

その他の用途:

上記のメソッドに加えて、次のメソッドも使用できます。シェルコマンドを呼び出すためのlaravelのスケジュールされたタスク:

$schedule->exec('node /home/forge/script.js')->daily();

クロージャーパッケージのスケジュール設定:

$schedule->call(function () {
    DB::table('recent_users')->delete();})->daily();

使用方法の詳細を知りたい場合は、laravelのドキュメントを確認してください:

https://laravelacademy.org/post/19517.html

[関連する推奨事項: laravel ビデオチュートリアル ]

以上がlaravelのスケジュールタスクの使い方と原理を詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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