Laravel 时间表 - 每小时、每天 - 了解确切的开始时间

Teb*_*ebe 6 php laravel laravel-5 laravel-scheduler laravel-5.5

Laravel文档中的示例:

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        DB::table('recent_users')->delete();
    })->daily();
}
Run Code Online (Sandbox Code Playgroud)

注意每日 功能。

我不明白,它如何知道什么时候开始?它总是在午夜或随机浮动时间开始吗?

我尝试阅读源代码:

/**
 * Schedule the event to run daily.
 *
 * @return $this
 */
public function daily()
{
    return $this->spliceIntoPosition(1, 0)
                ->spliceIntoPosition(2, 0);
}
Run Code Online (Sandbox Code Playgroud)

所以我检查了 spliceIntoPosition 函数:

    /**
 * 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));
}
Run Code Online (Sandbox Code Playgroud)

最终我彻底迷失了。有什么想法吗?

scx*_*scx 2

Laravel 文档准确指定了每天运行的时间

daily();    // Run the task every day at midnight
Run Code Online (Sandbox Code Playgroud)

基本上添加后

* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)

在你的 crontab 中,Laravel 每分钟都会调用调度程序,并且在每次调用时都会评估你的计划任务并运行到期的任务。

我建议您阅读有关cron以及规则如何工作的内容,这将使您了解为什么在那里调用函数 spliceIntoPosition() 以及它的作用。

cron 选项卡记录示例

* * * * * // will run every single minute
0 * * * * // will run every single hour at 30 [ 0:00, 1:00 ...] 
30 1 * * * // will run every single day at 1:30 [ Mon 1:30, Tue 1:30 ...] 
Run Code Online (Sandbox Code Playgroud)

因此,对于 daily() 在 spliceIntoPosition() 调用之后我们得到:

"0 0 * * *" // which will be called at 0:00 every single day
Run Code Online (Sandbox Code Playgroud)