I would like to get the scheduled tasks list from a controller. Some packages, articles and even StackOverflow explain how to display it from a command, but I did not found how to do it without a command. My goal is to get an array of scheduled task with their date and description.
Is there a way to get the scheduled task as an array (or an object list, or anything that can be easily handled) from a controller?
Here is a way to get all scheduled tasks:
use Carbon\Carbon;
use Cron\CronExpression;
use Illuminate\Support\Str;
app()->make(\Illuminate\Contracts\Console\Kernel::class);
$schedule = app()->make(\Illuminate\Console\Scheduling\Schedule::class);
$events = collect($schedule->events())->map(function($event) {
$cron = CronExpression::factory($event->expression);
$date = Carbon::now();
if ($event->timezone) {
$date->setTimezone($event->timezone);
}
return (object) [
'expression' => $event->expression,
'command' => Str::after($event->command, '\'artisan\' '),
'next_run_at' => $cron->getNextRunDate()->format('Y-m-d H:i:s'),
];
});
You have a collection of objects (in $events
) with three properties:
12 1 * * *
mypackage:mycommand
2018-01-10 16:50:49