Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

php - LARAVEL: Get an array of scheduled tasks for output in admin dashboard

Using the Laravel task scheduler I have created a number of tasks in 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).

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

// 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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

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:

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 IlluminateSupportFacadesArtisan;


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...