LARAVEL:在管理仪表板中获取输出的一系列计划任务

Joh*_*lor 9 php laravel laravel-5

使用Laravel任务调度程序我在Kernel.php中创建了许多任务

例如:

$schedule->command('some:command')
    ->daily();

$schedule->command('another:command')
    ->daily();
Run Code Online (Sandbox Code Playgroud)

我想显示预定命令的列表和频率(以及上一个/下一个运行时间,我可以使用之前/之后的函数记录自己).

但是我陷入了第一道障碍.我不知道该怎么做是获取已在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
Run Code Online (Sandbox Code Playgroud)

简化问题:如何在Laravel中获取一系列计划任务?

Ohg*_*why 14

不幸的是,实际上没有开箱即用的支持.您需要做的是扩展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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这将为您提供一个php artisan schedule:list.现在这不是你需要的,但是你可以通过执行以下命令轻松地从你的Laravel堆栈中获取这个列表:

Artisan::call('schedule:list');
Run Code Online (Sandbox Code Playgroud)

这将为您提供计划命令的列表.

当然,不要忘记注入Facade:use Illuminate\Support\Facades\Artisan;


小智 6

在 Laravel Framework 8.22.1 中,我通过将app/Console/Kernel->schedule方法公开,然后在控制器中进行工作:

use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Events\Dispatcher;

    private function getScheduledJobs()
    {
        new \App\Console\Kernel(app(), new Dispatcher());
        $schedule = app(Schedule::class);
        $scheduledCommands = collect($schedule->events());

        return $scheduledCommands;
    }
Run Code Online (Sandbox Code Playgroud)


fir*_*ire 5

由于未通过控制台运行,因此需要在控制器的内核上调用schedule方法...(不要忘记将该schedule方法公开而不是受保护)。

public function index(\Illuminate\Contracts\Console\Kernel $kernel, \Illuminate\Console\Scheduling\Schedule $schedule)
{
    $kernel->schedule($schedule);
    dd($schedule->events());
}
Run Code Online (Sandbox Code Playgroud)