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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步