この記事では、Laravelのスケジュールスケジューリングの動作メカニズムを理解します。

青灯夜游
リリース: 2022-03-10 11:33:36
転載
3207 人が閲覧しました

今回はLaravelにおけるスケジュールの動作仕組みについてお話しますので、皆様のお役に立てれば幸いです!

この記事では、Laravelのスケジュールスケジューリングの動作メカニズムを理解します。

- Laravel のコンソール コマンドラインを使用すると、PHP スケジュールされたタスクの設定と実行が大幅に容易になります。以前は、crontab を使用してスケジュールされたタスクを構成するプロセスは比較的煩雑で、crontab を使用してスケジュールされたタスクを設定するときにタスクの重複を防ぐのは困難でした。

いわゆるタスクの重複実行とは、スケジュールされたタスクの実行時間が長く、crontab によって設定された実行サイクルが適切ではないため、開始されたタスクの実行がまだ終了せず、システムが再起動されることを意味します。 . 同じ操作を実行する新しいタスク。データの一貫性の問題がプログラム内で適切に処理されていない場合、同じデータを同時に操作する 2 つのタスクが重大な結果を招く可能性があります。

runInBackgroundwithoutOverlapping

重複するタスクの実行を防ぐために、Laravel は withoutOverlapping() メソッドを提供します。複数のタスクをバックグラウンドで並行して実行できるようにするために、Laravel は runInBackground() メソッドを提供します。

runInBackground() Method

Console コマンドラインの各コマンドは、Event、## を表します。 #\App\Console\Kernelschedule() メソッドは、これらのコマンド ラインで表される EventIlluminate\Console\Scheduling\Schedule # に登録するだけです。 ## プロパティ $events にあります。 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">// namespace \Illuminate\Console\Scheduling\Schedule public function command($command, array $parameters = []) {     if (class_exists($command)) {         $command = Container::getInstance()-&gt;make($command)-&gt;getName();     }     return $this-&gt;exec(         Application::formatCommandString($command), $parameters     ); } public function exec($command, array $parameters = []) {     if (count($parameters)) {         $command .= ' '.$this-&gt;compileParameters($parameters);     }     $this-&gt;events[] = $event = new Event($this-&gt;eventMutex, $command, $this-&gt;timezone);     return $event; }</pre><div class="contentsignin">ログイン後にコピー</div></div> -

Event

を実行するには、ForegroundBackground の 2 つの方法があります。 2 つの違いは、複数の Event を並列実行できるかどうかです。 Event は、デフォルトでは Foreground モードで実行されます。この実行モードでは、複数の Event が順番に実行され、後続の Event は待機する必要がありますuntil 実行は、前の Event が完了した後にのみ開始できます。 ただし、実際のアプリケーションでは、複数の

Event

を並行して実行できることが望まれることがよくあります。この場合、## の runInBackground()## を呼び出す必要があります。 #Event # メソッドは実行モードを Background に設定します。 Laravel フレームワークがこれら 2 つの実行モードを処理する方法の違いは、コマンドラインの組み立て方法とコールバック メソッドの呼び出し方法にあります。 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">// namespace \Illuminate\Console\Scheduling\Event protected function runCommandInForeground(Container $container) { $this-&gt;callBeforeCallbacks($container); $this-&gt;exitCode = Process::fromShellCommandline($this-&gt;buildCommand(), base_path(), null, null, null)-&gt;run(); $this-&gt;callAfterCallbacks($container); } protected function runCommandInBackground(Container $container) { $this-&gt;callBeforeCallbacks($container); Process::fromShellCommandline($this-&gt;buildCommand(), base_path(), null, null, null)-&gt;run(); } public function buildCommand() { return (new CommandBuilder)-&gt;buildCommand($this); } // namespace Illuminate\Console\Scheduling\CommandBuilder public function buildCommand(Event $event) { if ($event-&gt;runInBackground) { return $this-&gt;buildBackgroundCommand($event); } return $this-&gt;buildForegroundCommand($event); } protected function buildForegroundCommand(Event $event) { $output = ProcessUtils::escapeArgument($event-&gt;output); return $this-&gt;ensureCorrectUser( $event, $event-&gt;command.($event-&gt;shouldAppendOutput ? &amp;#39; &gt;&gt; &amp;#39; : &amp;#39; &gt; &amp;#39;).$output.&amp;#39; 2&gt;&amp;1&amp;#39; ); } protected function buildBackgroundCommand(Event $event) { $output = ProcessUtils::escapeArgument($event-&gt;output); $redirect = $event-&gt;shouldAppendOutput ? &amp;#39; &gt;&gt; &amp;#39; : &amp;#39; &gt; &amp;#39;; $finished = Application::formatCommandString(&amp;#39;schedule:finish&amp;#39;).&amp;#39; &quot;&amp;#39;.$event-&gt;mutexName().&amp;#39;&quot;&amp;#39;; if (windows_os()) { return &amp;#39;start /b cmd /c &quot;(&amp;#39;.$event-&gt;command.&amp;#39; &amp; &amp;#39;.$finished.&amp;#39; &quot;%errorlevel%&quot;)&amp;#39;.$redirect.$output.&amp;#39; 2&gt;&amp;1&quot;&amp;#39;; } return $this-&gt;ensureCorrectUser($event, &amp;#39;(&amp;#39;.$event-&gt;command.$redirect.$output.&amp;#39; 2&gt;&amp;1 ; &amp;#39;.$finished.&amp;#39; &quot;$?&quot;) &gt; &amp;#39; .ProcessUtils::escapeArgument($event-&gt;getDefaultOutput()).&amp;#39; 2&gt;&amp;1 &amp;&amp;#39; ); }</pre><div class="contentsignin">ログイン後にコピー</div></div> コードからわかるように、

Background

を使用して

Event

を実行すると、最後に &## が追加されます。アセンブリ中のコマンド ライン。# シンボル。その機能は、コマンド ライン プログラムをバックグラウンドで実行することです。さらに、Foreground モードで実行される Event のコールバック メソッドが同期的に呼び出されます。 Background#Event を ## モードで実行している間、その after コールバックは schedule:finish コマンド ラインを通じて実行されます。 withoutOverlapping() メソッド

アプリケーションの継続的な変更のため、Event の実行サイクルを設定する場合シナリオでは、特定の Event が一定期間内に完了するまでに長い時間がかかり、次の実行サイクルの開始時にも完了しない可能性があることを避けるのは困難です。この状況が処理されない場合、複数の同一の

Event

が同時に実行されることになります。これらの Event にデータの操作が含まれており、プログラムが冪等性を適切に処理できない場合、問題が発生する可能性があります。重大な結果をもたらします。 上記の問題を回避するために、EventwithoutOverlapping() メソッドを提供します。これは、

Event

withoutOverlapping を変更します プロパティは TRUE に設定されています。Event が実行されるたびに、現在同じ Event が実行されているかどうかがチェックされます。存在しても、新しい Event タスクは実行されません。 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">// namespace Illuminate\Console\Scheduling\Event public function withoutOverlapping($expiresAt = 1440) { $this-&gt;withoutOverlapping = true; $this-&gt;expiresAt = $expiresAt; return $this-&gt;then(function () { $this-&gt;mutex-&gt;forget($this); })-&gt;skip(function () { return $this-&gt;mutex-&gt;exists($this); }); } public function run(Container $container) { if ($this-&gt;withoutOverlapping &amp;&amp; ! $this-&gt;mutex-&gt;create($this)) { return; } $this-&gt;runInBackground ? $this-&gt;runCommandInBackground($container) : $this-&gt;runCommandInForeground($container); }</pre><div class="contentsignin">ログイン後にコピー</div></div>mutex ミューテックス ロック

withoutOverlapping()

メソッドを呼び出すと、このメソッドは他の 2 つの関数も実装します。1 つはタイムアウトの設定です。 、デフォルトは 24 時間です。もう 1 つは、Event のコールバックを設定することです。

⑴ 超时时间

  首先说超时时间,这个超时时间并不是 Event 的超时时间,而是 Event 的属性 mutex 的超时时间。在向 Illuminate\Console\Scheduling\Schedule 的属性 $events 中注册 Event 时,会调用 Schedule 中的 exec() 方法,在该方法中会新建 Event 对象,此时会向 Event 的构造方法中传入一个 eventMutex ,这就是 Event 对象中的属性 mutex ,超时时间就是为这个 mutex 设置的。而 Schedule 中的 eventMutex 则是通过实例化 CacheEventMutex 来创建的。

// namespace \Illuminate\Console\Scheduling\Schedule
$this->eventMutex = $container->bound(EventMutex::class)
                                ? $container->make(EventMutex::class)
                                : $container->make(CacheEventMutex::class);
ログイン後にコピー

  设置了 withoutOverlappingEvent 在执行之前,首先会尝试获取 mutex 互斥锁,如果无法成功获取到锁,那么 Event 就不会执行。获取互斥锁的操作通过调用 mutexcreate() 方法完成。

  CacheEventMutex 在实例化时需要传入一个 \Illuminate\Contracts\Cache\Factory 类型的实例,其最终传入的是一个 \Illuminate\Cache\CacheManager 实例。在调用 create() 方法获取互斥锁时,还需要通过调用 store() 方法设置存储引擎。

// namespace \Illuminate\Foundation\Console\Kernel
protected function defineConsoleSchedule()
{
    $this->app->singleton(Schedule::class, function ($app) {
        return tap(new Schedule($this->scheduleTimezone()), function ($schedule) {
            $this->schedule($schedule->useCache($this->scheduleCache()));
        });
    });
}

protected function scheduleCache()
{
    return Env::get(&#39;SCHEDULE_CACHE_DRIVER&#39;);
}

// namespace \Illuminate\Console\Scheduling\Schedule
public function useCache($store)
{
    if ($this->eventMutex instanceof CacheEventMutex) {
        $this->eventMutex->useStore($store);
    }

    /* ... ... */
    return $this;
}

// namespace \Illuminate\Console\Scheduling\CacheEventMutex
public function create(Event $event)
{
    return $this->cache->store($this->store)->add(
        $event->mutexName(), true, $event->expiresAt * 60
    );
}

// namespace \Illuminate\Cache\CacheManager
public function store($name = null)
{
    $name = $name ?: $this->getDefaultDriver();

    return $this->stores[$name] = $this->get($name);
}

public function getDefaultDriver()
{
    return $this->app[&#39;config&#39;][&#39;cache.default&#39;];
}

protected function get($name)
{
    return $this->stores[$name] ?? $this->resolve($name);
}

protected function resolve($name)
{
    $config = $this->getConfig($name);

    if (is_null($config)) {
        throw new InvalidArgumentException("Cache store [{$name}] is not defined.");
    }

    if (isset($this->customCreators[$config[&#39;driver&#39;]])) {
        return $this->callCustomCreator($config);
    } else {
        $driverMethod = &#39;create&#39;.ucfirst($config[&#39;driver&#39;]).&#39;Driver&#39;;

        if (method_exists($this, $driverMethod)) {
            return $this->{$driverMethod}($config);
        } else {
            throw new InvalidArgumentException("Driver [{$config[&#39;driver&#39;]}] is not supported.");
        }
    }
}

protected function getConfig($name)
{
    return $this->app[&#39;config&#39;]["cache.stores.{$name}"];
}

protected function createFileDriver(array $config)
{
    return $this->repository(new FileStore($this->app[&#39;files&#39;], $config[&#39;path&#39;], $config[&#39;permission&#39;] ?? null));
}
ログイン後にコピー

  在初始化 Schedule 时会指定 eventMutex 的存储引擎,默认为环境变量中的配置项 SCHEDULE_CACHE_DRIVER 的值。但通常这一项配置在环境变量中并不存在,所以 useCache() 的参数值为空,进而 eventMutexstore 属性值也为空。这样,在 eventMutexcreate() 方法中调用 store() 方法为其设置存储引擎时,store() 方法的参数值也为空。

  当 store() 方法的传参为空时,会使用应用的默认存储引擎(如果不做任何修改,默认 cache 的存储引擎为 file)。之后会取得默认存储引擎的配置信息(引擎、存储路径、连接信息等),然后实例化存储引擎。最终,file 存储引擎实例化的是 \Illuminate\Cache\FileStore

  在设置完存储引擎之后,紧接着会调用 add() 方法获取互斥锁。由于 store() 方法返回的是 \Illuminate\Contracts\Cache\Repository 类型的实例,所以最终调用的是 Illuminate\Cache\Repository 中的 add() 方法。

// namespace \Illuminate\Cache\Repository
public function add($key, $value, $ttl = null)
{
    if ($ttl !== null) {
        if ($this->getSeconds($ttl) <= 0) {
            return false;
        }

        if (method_exists($this->store, &#39;add&#39;)) {
            $seconds = $this->getSeconds($ttl);

            return $this->store->add(
                $this->itemKey($key), $value, $seconds
            );
        }
    }

    if (is_null($this->get($key))) {
        return $this->put($key, $value, $ttl);
    }

    return false;
}

public function get($key, $default = null)
{
    if (is_array($key)) {
        return $this->many($key);
    }

    $value = $this->store->get($this->itemKey($key));

    if (is_null($value)) {
        $this->event(new CacheMissed($key));

        $value = value($default);
    } else {
        $this->event(new CacheHit($key, $value));
    }

    return $value;
}

// namespace \Illuminate\Cache\FileStore
public function get($key)
{
    return $this->getPayload($key)[&#39;data&#39;] ?? null;
}

protected function getPayload($key)
{
    $path = $this->path($key);

    try {
        $expire = substr(
            $contents = $this->files->get($path, true), 0, 10
        );
    } catch (Exception $e) {
        return $this->emptyPayload();
    }

    if ($this->currentTime() >= $expire) {
        $this->forget($key);

        return $this->emptyPayload();
    }

    try {
        $data = unserialize(substr($contents, 10));
    } catch (Exception $e) {
        $this->forget($key);

        return $this->emptyPayload();
    }

    $time = $expire - $this->currentTime();

    return compact(&#39;data&#39;, &#39;time&#39;);
}
ログイン後にコピー

  这里需要说明,所谓互斥锁,其本质是写文件。如果文件不存在或文件内容为空或文件中存储的过期时间小于当前时间,则互斥锁可以顺利获得;否则无法获取到互斥锁。文件内容为固定格式:timestampb:1

  所谓超时时间,与此处的 timestamp 的值有密切的联系。获取互斥锁时的时间戳,再加上超时时间的秒数,即是此处的 timestamp 的值。

  由于 FileStore 中不存在 add() 方法,所以程序会直接尝试调用 get() 方法获取文件中的内容。如果 get() 返回的结果为 NULL,说明获取互斥锁成功,之后会调用 FileStoreput() 方法写文件;否则,说明当前有相同的 Event 在运行,不会再运行新的 Event

  在调用 put() 方法写文件时,首先需要根据传参计算 eventMutex 的超时时间的秒数,之后再调用 FileStore 中的 put() 方法,将数据写入文件中。

// namespace \Illuminate\Cache\Repository
public function put($key, $value, $ttl = null)
{
    /* ... ... */

    $seconds = $this->getSeconds($ttl);

    if ($seconds <= 0) {
        return $this->forget($key);
    }

    $result = $this->store->put($this->itemKey($key), $value, $seconds);

    if ($result) {
        $this->event(new KeyWritten($key, $value, $seconds));
    }

    return $result;
}

// namespace \Illuminate\Cache\FileStore
public function put($key, $value, $seconds)
{
    $this->ensureCacheDirectoryExists($path = $this->path($key));

    $result = $this->files->put(
        $path, $this->expiration($seconds).serialize($value), true
    );

    if ($result !== false && $result > 0) {
        $this->ensureFileHasCorrectPermissions($path);

        return true;
    }

    return false;
}

protected function path($key)
{
    $parts = array_slice(str_split($hash = sha1($key), 2), 0, 2);

    return $this->directory.&#39;/&#39;.implode(&#39;/&#39;, $parts).&#39;/&#39;.$hash;
}

// namespace \Illuminate\Console\Scheduling\Schedule
public function mutexName()
{
    return &#39;framework&#39;.DIRECTORY_SEPARATOR.&#39;schedule-&#39;.sha1($this->expression.$this->command);
}
ログイン後にコピー

  这里需要重点说明的是 $key 的生成方法以及文件路径的生成方法。$key 通过调用 EventmutexName() 方法生成,其中需要用到 Event$expression$command 属性。其中 $command 为我们定义的命令行,在调用 $schedule->comand() 方法时传入,然后进行格式化,$expression 则为 Event 的运行周期。

  以命令行 schedule:test 为例,格式化之后的命令行为 `/usr/local/php/bin/php` `artisan` schedule:test,如果该命令行设置的运行周期为每分钟一次,即 * * * * * ,则最终计算得到的 $key 的值为 framework/schedule-768a42da74f005b3ac29ca0a88eb72d0ca2b84be 。文件路径则是将 $key 的值再次进行 sha1 计算之后,以两个字符为一组切分成数组,然后取数组的前两项组成一个二级目录,而配置文件中 file 引擎的默认存储路径为 storage/framework/cache/data ,所以最终的文件路径为 storage/framework/cache/data/eb/60/eb608bf555895f742e5bd57e186cbd97f9a6f432 。而文件中存储的内容则为 1642122685b:1

⑵ 回调方法

  再来说设置的 Event 回调,调用 withoutOverlapping() 方法会为 Event 设置两个回调:一个是 Event 运行完成之后的回调,用于释放互斥锁,即清理缓存文件;另一个是在运行 Event 之前判断互斥锁是否被占用,即缓存文件是否已经存在。

  无论 Event 是以 Foreground 的方式运行,还是以 Background 的方式运行,在运行完成之后都会调用 callAfterCallbacks() 方法执行 afterCallbacks 中的回调,其中就有一项回调用于释放互斥锁,删除缓存文件 $this->mutex->forget($this) 。区别就在于,以 Foreground 方式运行的 Event 是在运行完成之后显式的调用这些回调方法,而以 Background 方式运行的 Event 则需要借助 schedule:finish 来调用这些回调方法。

  所有在 \App\Console\Kernel 中注册 Event,都是通过命令行 schedule:run 来调度的。在调度之前,首先会判断当前时间点是否满足各个 Event 所配置的运行周期的要求。如果满足的话,接下来就是一些过滤条件的判断,这其中就包括判断互斥锁是否被占用。只有在互斥锁没有被占用的情况下,Event 才可以运行。

// namespace \Illuminate\Console\Scheduling\ScheduleRunCommand
public function handle(Schedule $schedule, Dispatcher $dispatcher)
{
    $this->schedule = $schedule;
    $this->dispatcher = $dispatcher;

    foreach ($this->schedule->dueEvents($this->laravel) as $event) {
        if (! $event->filtersPass($this->laravel)) {
            $this->dispatcher->dispatch(new ScheduledTaskSkipped($event));

            continue;
        }

        if ($event->onOneServer) {
            $this->runSingleServerEvent($event);
        } else {
            $this->runEvent($event);
        }

        $this->eventsRan = true;
    }

    if (! $this->eventsRan) {
        $this->info(&#39;No scheduled commands are ready to run.&#39;);
    }
}

// namespace \Illuminate\Console\Scheduling\Schedule
public function dueEvents($app)
{
    return collect($this->events)->filter->isDue($app);
}

// namespace \Illuminate\Console\Scheduling\Event
public function isDue($app)
{
    /* ... ... */
    return $this->expressionPasses() &&
           $this->runsInEnvironment($app->environment());
}

protected function expressionPasses()
{
    $date = Carbon::now();
    /* ... ... */
    return CronExpression::factory($this->expression)->isDue($date->toDateTimeString());
}

// namespace \Cron\CronExpression
public function isDue($currentTime = &#39;now&#39;, $timeZone = null)
{
   /* ... ... */
   
    try {
        return $this->getNextRunDate($currentTime, 0, true)->getTimestamp() === $currentTime->getTimestamp();
    } catch (Exception $e) {
        return false;
    }
}

public function getNextRunDate($currentTime = &#39;now&#39;, $nth = 0, $allowCurrentDate = false, $timeZone = null)
{
    return $this->getRunDate($currentTime, $nth, false, $allowCurrentDate, $timeZone);
}
ログイン後にコピー
  有时候,我们可能需要 kill 掉一些在后台运行的命令行,但紧接着我们会发现这些被 kill 掉的命令行在一段时间内无法按照设置的运行周期自动调度,其原因就在于手动 kill 掉的命令行没有调用 schedule:finish 清理缓存文件,释放互斥锁。这就导致在设置的过期时间到达之前,互斥锁会一直被占用,新的 Event 不会再次运行。

【相关推荐:laravel视频教程

以上がこの記事では、Laravelのスケジュールスケジューリングの動作メカニズムを理解します。の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:juejin.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!