今天,总监说要一个任务调度的列表。

今天,总监说,要一个任务调度的列表,这样他就能看到哪些任务在自动执行。
于是乎,就去看了看laravel 相关的源码,下面直接上解决方案。

//1. 加载Console内核
app()->make(\Illuminate\Contracts\Console\Kernel::class);

//2.  获取计划任务列表
$scheduleList = app()->make(\Illuminate\Console\Scheduling\Schedule::class)->events();

至于原理,大家可以去看看laravel的源码。

本作品采用《CC 协议》,转载必须注明作者和本文链接
打酱油
本帖由系统于 5年前 自动加精
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 9
//1. 加载Console内核
app()->make(\Illuminate\Contracts\Console\Kernel::class);

//2. 获取计划任务列表
$this->schedule = app()->make(\Illuminate\Console\Scheduling\Schedule::class);

//3. 遍历获取属性
if (count($this->schedule->events()) > 0) {
    $events = collect($this->schedule->events())->map(function ($event) {
        if (!strpos($event->command, "'artisan")) {
            $command = $event->command;
            $commandType = empty($command) ? 'job' : 'shell';
        } else {
            $command = ltrim(strtok(str_after($event->command, "'artisan'"), ''));
            $commandType = 'command';
        }
        return [
            'type'          => $commandType,
            'description'   => $event->description ?: '',
            'command'       => $command,
            'schedule'      => $event->expression,
            'upcoming'      => $this->upcoming($event),
            'timezone'      => $event->timezone ?: config('app.timezone'),
            'overlaps'      => $event->withoutOverlapping ? 'No' : 'Yes',
            'maintenance'   => $event->evenInMaintenanceMode ? 'Yes' : 'No',
            'background'    => $event->runInBackground ? 'Yes' : 'No',
            'one_server'   => $event->onOneServer ? 'Yes' : 'No',
        ];
    });

    dd($events);
}
    /**
     * Get Upcoming schedule.
     *
     * @return bool
     */
    protected function upcoming($event)
    {
        $date = Carbon::now();

        if ($event->timezone) {
            $date->setTimezone($event->timezone);
        }

        return (CronExpression::factory($event->expression)->getNextRunDate($date->toDateTimeString()))->format('Y-m-d H:i:s');
    }

在楼主的代码基础上修改显示任务的详情,参考来源 https://github.com/codestudiohq/laravel-to...

效果图

4年前 评论
Complicated

真棒,今天正好要做这个任务,,还想着通过数据库记录呢,,,,

5年前 评论
喜欢悠闲独自在

app()->make(Illuminate\Contracts\Console\Kernel::class); 这里开头少了一个\

5年前 评论

@狗达 多谢提醒~

5年前 评论

@Complicated 哈哈哈,我开始也想每加一个写到数据库里面去,但是太麻烦了,想来想去,还是去扒扒源码好了

5年前 评论

正好需要一个这样的功能

5年前 评论
王举

很优雅,不敢指正:+1:

5年前 评论
hareluya

这个。。。https://horizon.laravel.com/。。。?

5年前 评论
qbhy

@hareluya 调度跟队列不一样的小哥

5年前 评论
//1. 加载Console内核
app()->make(\Illuminate\Contracts\Console\Kernel::class);

//2. 获取计划任务列表
$this->schedule = app()->make(\Illuminate\Console\Scheduling\Schedule::class);

//3. 遍历获取属性
if (count($this->schedule->events()) > 0) {
    $events = collect($this->schedule->events())->map(function ($event) {
        if (!strpos($event->command, "'artisan")) {
            $command = $event->command;
            $commandType = empty($command) ? 'job' : 'shell';
        } else {
            $command = ltrim(strtok(str_after($event->command, "'artisan'"), ''));
            $commandType = 'command';
        }
        return [
            'type'          => $commandType,
            'description'   => $event->description ?: '',
            'command'       => $command,
            'schedule'      => $event->expression,
            'upcoming'      => $this->upcoming($event),
            'timezone'      => $event->timezone ?: config('app.timezone'),
            'overlaps'      => $event->withoutOverlapping ? 'No' : 'Yes',
            'maintenance'   => $event->evenInMaintenanceMode ? 'Yes' : 'No',
            'background'    => $event->runInBackground ? 'Yes' : 'No',
            'one_server'   => $event->onOneServer ? 'Yes' : 'No',
        ];
    });

    dd($events);
}
    /**
     * Get Upcoming schedule.
     *
     * @return bool
     */
    protected function upcoming($event)
    {
        $date = Carbon::now();

        if ($event->timezone) {
            $date->setTimezone($event->timezone);
        }

        return (CronExpression::factory($event->expression)->getNextRunDate($date->toDateTimeString()))->format('Y-m-d H:i:s');
    }

在楼主的代码基础上修改显示任务的详情,参考来源 https://github.com/codestudiohq/laravel-to...

效果图

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!