有没有办法从控制器获取调度的任务作为数组? [英] Is there a way to get the scheduled task as an array from a controller?

查看:92
本文介绍了有没有办法从控制器获取调度的任务作为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从控制器获取计划任务列表. 某些软件包文章,甚至

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:

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'),
    ];
});

您有一个具有三个属性的对象集合(在$events中):

You have an collection of objects (in $events) with three properties:

  • 表达式-示例:12 1 * * *
  • 命令-示例:mypackage:mycommand
  • next_run_at -示例:2018-01-10 16:50:49
  • expression - example: 12 1 * * *
  • command - example: mypackage:mycommand
  • next_run_at - example: 2018-01-10 16:50:49

这篇关于有没有办法从控制器获取调度的任务作为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆