Home  >  Article  >  PHP Framework  >  This article will take you to understand the operating mechanism of Laravel schedule scheduling.

This article will take you to understand the operating mechanism of Laravel schedule scheduling.

青灯夜游
青灯夜游forward
2022-02-17 19:26:523176browse

This article will talk about the operating mechanism of schedule in Laravel. I hope it will be helpful to everyone!

This article will take you to understand the operating mechanism of Laravel schedule scheduling.

- Laravel's console command line greatly facilitates the setting and running of PHP scheduled tasks. In the past, the process of configuring scheduled tasks through crontab was relatively cumbersome, and it was difficult to prevent overlapping tasks when setting up scheduled tasks through crontab.

# ## The so-called overlapping running of tasks means that due to the long running time of scheduled tasks and the running cycle set by crontab is not reasonable, the started tasks have not yet finished running, and the system starts again. New tasks to perform the same operations. If the problem of data consistency is not handled well within the program, then two tasks operating on the same data at the same time may lead to serious consequences.

runInBackground and withoutOverlapping

To prevent overlapping tasks from running, Laravel provides the

withoutOverlapping() method; In order to allow multiple tasks to be executed in parallel in the background, Laravel provides the runInBackground() method.

runInBackground() Method

 Console Each command in the command line represents an

Event, ## The schedule() method in #\App\Console\Kernel is just to register the Event represented by these command lines to Illuminate\Console\Scheduling\Schedule in the properties $events. There are two ways to run <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> -

Event

: Foreground and Background. The difference between the two lies in whether multiple Event can be executed in parallel. Event runs in Foreground mode by default. In this running mode, multiple Event are executed sequentially, and the subsequent Event needs to wait until Execution can only start after the previous Event is completed. However, in actual applications, we often hope that multiple

Event## # can be executed in parallel. In this case, we need to call

runInBackground()## of Event # Method sets its run mode to Background.    The difference between how the Laravel framework handles these two running modes lies in the way the command line is assembled and the way the callback method is called. <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>  It can be seen from the code that when

Background

is used to run

Event

, a # will be added at the end of the command line during assembly. # symbol, its function is to make the command line program run in the background; in addition, the callback method of Event run in Foreground mode is called synchronously, while using Background#Event run in ## mode, its after callback is executed through the schedule:finish command line. withoutOverlapping() Method

When setting the run cycle of Event, due to the constant changes in application scenarios, It is difficult to avoid that a specific Event will take a long time to complete in a certain period of time, and may not even be completed at the beginning of the next running cycle. If this situation is not handled, it will cause multiple identical

Event

to run at the same time, and if these Event involve operations on data and the program does not handle idempotence well problems may have serious consequences. In order to avoid the above problems, Event provides the withoutOverlapping() method, which changes the

withoutOverlapping of

Event The property is set to TRUE. Each time Event is to be executed, it will be checked whether there is currently the same Event being executed. If it exists, it will not Execute new Event task. <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>mutex Mutex lock When calling the

withoutOverlapping()

method, this method also implements two other functions: one is Set the timeout, the default is 24 hours; the other is to set the callback of 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视频教程

The above is the detailed content of This article will take you to understand the operating mechanism of Laravel schedule scheduling.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete