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



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

例如:

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

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

然而,我被困在第一个障碍上。我不确定该怎么做的是获取内核中定义的计划任务数组.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 AppConsoleCommands;
use IlluminateConsoleCommand;
use IlluminateConsoleSchedulingSchedule;
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');

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

当然,不要忘记注入Facadeuse IlluminateSupportFacadesArtisan;

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

public function index(IlluminateContractsConsoleKernel $kernel, IlluminateConsoleSchedulingSchedule $schedule)
{
    $kernel->schedule($schedule);
    dd($schedule->events());
}

如果有人像我一样并试图在 2021 年这样做。基于旧的答案和一些玩耍。

在 Laravel 框架 8.22.1 中,我开始通过将应用程序/控制台/内核>调度方法公开,然后在控制器中工作:

use IlluminateConsoleSchedulingSchedule;
use IlluminateEventsDispatcher;
    private function getScheduledJobs()
    {
        new AppConsoleKernel(app(), new Dispatcher());
        $schedule = app(Schedule::class);
        $scheduledCommands = collect($schedule->events());
        return $scheduledCommands;
    }

希望这能为像我这样的未来谷歌员工节省时间。

我的任务是从管理仪表板停止/启用和编辑计划任务的频率。所以我在内核中对它们进行了.php并在函数中捕获输出。

$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);
            });
    }

希望这对你有帮助。

最新更新