LARAVEL:获取一系列计划任务以在管理仪表板中输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35559769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LARAVEL: Get an array of scheduled tasks for output in admin dashboard
提问by John Mellor
Using the Laravel task scheduler I have created a number of tasks in Kernel.php
使用 Laravel 任务调度程序,我在 Kernel.php 中创建了许多任务
e.g:
例如:
$schedule->command('some:command')
->daily();
$schedule->command('another:command')
->daily();
I would like to display the list of scheduled commands and there frequency (as well as last/next run time, which I can log myself using the before/after functions).
我想显示预定命令的列表和频率(以及上次/下一次运行时间,我可以使用 before/after 函数记录自己)。
However i'm stuck at the first hurdle. What i'm not sure how to do is get an array of the scheduled tasks that have been defined in Kernel.php
然而,我被困在第一个障碍。我不知道该怎么做是获取已在 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
Simplified Question: How can I get an array of the scheduled tasks in Laravel?
简化问题:如何在 Laravel 中获取一系列计划任务?
回答by Ohgodwhy
There's actually no support out of the box for this, unfortunately. What you'll have to do is extend the artisan schedule
command and add a list
feature. Thankfully there's a simple class you can run:
不幸的是,实际上没有现成的支持。您需要做的是扩展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);
}
}
This will provide you with a php artisan schedule:list
. Now that's not exactly what you need, but then you can easily get this list from within your Laravel stack by executing:
这将为您提供一个php artisan schedule:list
. 现在这不是您所需要的,但是您可以通过执行以下命令轻松地从 Laravel 堆栈中获取此列表:
Artisan::call('schedule:list');
And that will provide you with a list of the schedule commands.
这将为您提供调度命令的列表。
Of course, don't forget to inject the Facade
: use Illuminate\Support\Facades\Artisan;
当然,不要忘记注入Facade
:use Illuminate\Support\Facades\Artisan;
回答by fire
As your not running through the console you need to invoke the schedule method on the Kernel in your controller... (don't forget to make the schedule
method public instead of protected).
由于您没有通过控制台运行,您需要在控制器的内核上调用 schedule 方法......(不要忘记将schedule
方法设为公开而不是受保护)。
public function index(\Illuminate\Contracts\Console\Kernel $kernel, \Illuminate\Console\Scheduling\Schedule $schedule)
{
$kernel->schedule($schedule);
dd($schedule->events());
}
回答by Martin Tonev
I was having the task to stop/enable and edit the frequency of the scheduled tasks from the admin dashboard. So I foreach them in Kernel.php and capture the output in function.
我的任务是从管理仪表板停止/启用和编辑计划任务的频率。所以我在 Kernel.php 中 foreach 它们并在函数中捕获输出。
$enabled_commands = ConsoleCommand::where('is_enabled', 1)
->get();
foreach ($enabled_commands as $command)
{
$schedule->command($command->command_name)
->{$command->frequency}($command->time)
->after(function () use ($command) {
$this->log($command->command_name);
});
}
Hope this help you.
希望这对你有帮助。