使用 Laravel 任务调度程序,我在 Kernel.php 中创建了许多任务
例如:
$schedule->command('some:command')
->daily();
$schedule->command('another:command')
->daily();
我想显示计划命令列表和频率(以及上次/下一次运行时间,我可以使用之前/之后的功能记录自己)。
但是我遇到了第一个障碍。我不确定如何获取已在 Kernel.php 中定义的计划任务数组
// Example function needs to be created
$tasks = getAllScheduledTasks();
@foreach($tasks as $task)
<tr>
<td>{{ $task->name }}</td>
<td>{{ $task->description }}</td>
</tr>
@endforeach
简化问题:如何在 Laravel 中获取计划任务数组?
最佳答案
不幸的是,实际上没有开箱即用的支持。您需要做的是扩展 artisan schedule
命令并添加 list
功能。值得庆幸的是,您可以运行一个简单的类:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
class ScheduleList extends Command
{
protected $signature = 'schedule:list';
protected $description = 'List when scheduled commands are executed.';
/**
* @var Schedule
*/
protected $schedule;
/**
* ScheduleList constructor.
*
* @param Schedule $schedule
*/
public function __construct(Schedule $schedule)
{
parent::__construct();
$this->schedule = $schedule;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$events = array_map(function ($event) {
return [
'cron' => $event->expression,
'command' => static::fixupCommand($event->command),
];
}, $this->schedule->events());
$this->table(
['Cron', 'Command'],
$events
);
}
/**
* If it's an artisan command, strip off the PHP
*
* @param $command
* @return string
*/
protected static function fixupCommand($command)
{
$parts = explode(' ', $command);
if (count($parts) > 2 && $parts[1] === "'artisan'") {
array_shift($parts);
}
return implode(' ', $parts);
}
}
这将为您提供一个 php artisan schedule:list
。现在这并不是您所需要的,但是您可以通过执行以下命令轻松地从 Laravel 堆栈中获取此列表:
Artisan::call('schedule:list');
这将为您提供计划命令列表。
当然,不要忘记注入(inject)Facade
:use Illuminate\Support\Facades\Artisan;
关于php - LARAVEL:获取一系列计划任务以在管理仪表板中输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35559769/