Laravel——自定义命令command
简单使用自定义命令command :
项目场景:Laravel5.4 最近有个项目 业务场景要处理三方接口,用户提交信息发给三方,等待三方回调,需要做一个队列来保证每次都能调到三方 于是自己就用命令行,做了一个任务
新建了一个命令行处理文件
php artisan make:command CronAppraisalPush--command=appraisal-push
下面是生成的文件
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Log;
class CronAppraisalPush extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'appraisal-push';
/**
* The console command description.
*
* @var string
*/
protected $description = '三方鉴别队列处理';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
var_dump(123);
// $good = unserialize(Redis::get('artisan_appraisal_push'));//取数组
// Redis::lpush('Queue',$good);//将一个或多个值插入到列表头部
// $length = Redis::llen('Queue');//队列数据的条数
// while($length > 0)
// {
// $sicker = Redis::rpop("Queue");//移出并获取列表的最后一个元素
// //处理结果
// }
}
}
这个时候执行是不存在此命令
解决方案:
找到 Kernel.php 文件
在 这个里边引入 $commands = []类
在 schedule ()方法里
$schedule->command(‘命令’)->everyMinute();
->cron(‘* * * * * *’); 自定义调度任务
->everyMinute(); 每分钟执行一次任务
->everyFiveMinutes(); 每五分钟执行一次任务
->everyTenMinutes(); 每十分钟执行一次任务
->everyThirtyMinutes(); 每半小时执行一次任务
->hourly(); 每小时执行一次任务
->hourlyAt(17); 每一个小时的第 17 分钟运行一次
->daily(); 每到午夜执行一次任务
->dailyAt(‘13:00’); 每天的 13:00 执行一次任务
->twiceDaily(1,13); 每天的 1:00 和 13:00 分别执行一次任务
->weekly(); 每周执行一次任务
->monthly(); 每月执行一次任务
->monthlyOn(4,‘15:00’); 在每个月的第四天的 15:00 执行一次任务
->quarterly(); 每季度执行一次任务
->yearly(); 每年执行一次任务
->timezone(‘America/New_York’); 设置时区
->weekdays(); 限制任务在工作日
->sundays(); 限制任务在星期日
->mondays(); 限制任务在星期一
->tuesdays(); 限制任务在星期二
->wednesdays(); 限制任务在星期三
->thursdays(); 限制任务在星期四
->fridays(); 限制任务在星期五
->saturdays(); 限制任务在星期六
->between(
s
t
a
r
t
,
start,
start,end); 限制任务运行在开始到结束时间范围内
->when(Closure); 限制任务基于一个为真的验证
<?php
namespace App\Console;
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 = [
\App\Console\Commands\CronMember::class,
\App\Console\Commands\CronNotice::class,
\App\Console\Commands\CronJpushTask::class,
\App\Console\Commands\CronAppraisalPush::class
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('cronmember')->everyMinute();
$schedule->command('cronnotice')->everyMinute();
$schedule->command('cronjpushtask')->everyMinute();
}
/**
* Register the Closure based commands for the application.
*
* @return void
*/
protected function commands()
{
// $this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律