gpt4 book ai didi

php - Laravel5 : How to disable default scheduler message if no command is ready to run

转载 作者:可可西里 更新时间:2023-11-01 12:36:45 25 4
gpt4 key购买 nike

使用 Laravel5 scheduler 时:

* * * * * cd/path-to-your-project && php artisan schedule:run >>/dev/null 2>&1

如果没有准备好运行的命令,我们会收到以下默认输出:

# No scheduled commands are ready to run.

如何禁用这个默认的 Laravel5 消息?如果没有准备好运行的命令,我们不希望有输出。最好的情况是,当我们能够配置该消息并自行返回代码时。

最佳答案

您可以在 app/Console/Commands 中创建一个类似于下面的新命令,它扩展了默认的 schedule:run 命令。

它覆盖了 handle 方法,同时保留所有其他内容,以避免让 Laravel 输出“没有计划的命令准备好运行”。当它没有做任何事情时行。

通过使用不同的名称,无需担心冲突,如果您愿意,您仍然可以随时运行原始的 php artisan schedule:run 命令。

<?php

namespace App\Console\Commands

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Console\Scheduling\ScheduleRunCommand;

class RunTasks extends ScheduleRunCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'run:tasks';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Custom task runner with no default output';

/**
* Create a new command instance.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
public function __construct(Schedule $schedule)
{
parent::__construct($schedule);
}

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

if (! $this->eventsRan) {
// Laravel would output the default text here. You can remove
// this if statement entirely if you don't want output.
//
// Alternatively, define some custom output with:
// $this->info("My custom 'nothing ran' message");
}
}
}

验证 Laravel 是否看到你的新命令:

php artisan | grep run:tasks

最后更新您的 cron 以运行新命令:

* * * * * cd /path-to-your-project && php artisan run:tasks >> /dev/null 2>&1

关于php - Laravel5 : How to disable default scheduler message if no command is ready to run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54687337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com