有没有办法检查Laravel日程安排是否正在运行?

时间:2019-02-02 09:13:30

标签: php laravel scheduled-tasks

我正在不同主机上维护一个应用程序(基于Laravel构建)的许多副本,并且我正在寻找一种方法来检查Laravel Schedule是否确实在为每个应用程序运行。 Laravel为此提供了一些东西吗?

2 个答案:

答案 0 :(得分:0)

否,框架中没有任何内容可以告诉您哪些主机正在运行schedule:run。你将不得不实行的是你自己。

如果您有一个跟踪主机的中央系统,则可以执行一个命令,将心跳ping发送到中央系统,然后将该命令添加到调度程序中。

如果您的逻辑取决于要知道其当前主机是否正在运行调度程序, 那么您可以执行预定的命令,将当前时间写入到 local 磁盘上的文件中(如果未在主机之间共享该缓存,或者该密钥对于主机是唯一的,则写入缓存也可以)。然后,您只需检查($now - $last_write) > $schedule_interval是否为真。

答案 1 :(得分:0)

我遇到了同样的问题,并通过在命令中使用选项来解决了这个问题:

class SomeCommand extends Command
{
    protected $signature = 'some:command {--other-option} {--schedule}';

    public function handle()
    {
        if ($this->option('schedule')) {
            // this command is triggered by the schedule
        }
    }
}

在您的app/Console/Kernel.php中:

class Kernel extends ConsoleKernel
{
    /**
     * @param \Illuminate\Console\Scheduling\Schedule $schedule
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command(SomeCommand::class, ['--schedule'])->hourly();
    }

    protected function commands()
    {
        $this->load(__DIR__ . '/Commands');
    }
}