Is there a way to get the scheduled task as an array from a controller?

夙愿已清 提交于 2019-12-02 16:45:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!