使用Laravel任务计划程序,我在Kernel.php中创建了许多任务
例如:
$schedule->command('some:command')
->daily();
$schedule->command('another:command')
->daily();
我想显示调度命令的列表以及出现的频率(以及上次/下一次运行时间,可以使用before / after函数记录自己的时间)。
但是我被困在第一个障碍。我不确定该怎么做才能获取已在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');
这将为您提供schedule命令的列表。
当然,不要忘记注入Facade
:use Illuminate\Support\Facades\Artisan;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句