原csdn地址https://blog.csdn.|

蜗牛使劲冲

园龄:7年6个月粉丝:3关注:10

laravel任务调度小记

参考:https://learnku.com/docs/laravel/5.8/scheduling/3924#scheduling-artisan-commands (文档)
https://www.jianshu.com/p/99baaffcec18

原理:就是crontab增加个以每分钟或者每隔多长时间触发执行laravel项目设置定时任务

设置crontab每分钟触发

* * * * * cd /www/test && php artisan schedule:run >> /dev/null 2>&1

然后在\App\Console\Teacher.php

<?php

namespace App\Console\Operate;

use App\Common\Base\BaseCommand;
use App\Modules\Report\DTO\ReportDateDTO;
use App\Modules\Report\Services\ReportMainService;

class Teacher extends BaseCommand
{

    protected $signature = 'reportMain:generate {module-code-time} {--dto=}';

    protected $description = 'reportMain generate
                                module-code-time 选择 day或week或month
                                --dto 可传可不传,例:--dto=20160904,20200415,20200621 (dataId,startAt,endAt)
    ';

    public $reportMainService;

    public function __construct(ReportMainService $reportMainService)
    {
        parent::__construct();
        $this->reportMainService = $reportMainService;
    }

    public function handle()
    {
        $moduleCodeTime = $this->argument('module-code-time');
        if(!in_array($moduleCodeTime,['day','week','month'])){
            $this->error('失败:module-code-time参数错误');
            return;
        }

        $moduleCode = 'operate_teacher_' . $moduleCodeTime;

        $ops = $this->options();
        $dtoString = $ops['dto']??null;

        $dto = null;
        if(!is_null($dtoString)){
            $dto = new ReportDateDTO();
            $dtoArr = explode(',',$dtoString);

            $dto->dateId = $dtoArr[0];
            $dto->startAt = $dtoArr[1];
            $dto->endAt = $dtoArr[2];
        }

        $this->reportMainService->generate($moduleCode, $dto);
        $this->info('生成结束');
        return;
    }
}

上面的是command代码,触发方式:php artisan reportMain:generate week --dto=20160904,20200415,20200621
这边-dto格式要注意
然后就是正题每隔一分钟触发我好几个command命令了,在\App\Console\Kernel修修改改

<?php

namespace App\Console;


use App\Console\Operate\Teacher;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Teacher::class,// 这边加下
    ];

    /**
     * Define the application's command schedule.
     * 下面就是调用的command的了
     * @param  Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('reportMain:generate day')
            ->description('每日凌晨1点执行')
            ->dailyAt('01:00')
            ->withoutOverlapping()
            ->onOneServer()
            ->runInBackground();

        $schedule->command('reportMain:generate week')
            ->description('每周一凌晨1点执行')
            ->weeklyOn(1, '01:00')
            ->withoutOverlapping()
            ->onOneServer()
            ->runInBackground();

        $schedule->command('reportMain:generate month')
            ->description('每月一号凌晨1点执行')
            ->monthlyOn(1, '01:00')
            ->withoutOverlapping()
            ->onOneServer()
            ->runInBackground();

    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');


        require base_path('routes/console.php');
    }
}

可测试

本文作者:蜗牛使劲冲

本文链接:https://www.cnblogs.com/warrenwt/p/18074578

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   蜗牛使劲冲  阅读(4)  评论(0编辑  收藏  举报  
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起