问题
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?
回答1:
Here is a way to get all scheduled tasks:
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 an collection of objects (in $events) with three properties:
- expression - example:
12 1 * * * - command - example:
mypackage:mycommand - next_run_at - example:
2018-01-10 16:50:49
来源:https://stackoverflow.com/questions/48190007/is-there-a-way-to-get-the-scheduled-task-as-an-array-from-a-controller