
主要介绍下 Console 内核与下面两者的使用:
- Artisan
- Job
Console 内核
首先理解 app/Console/Kernel 的作用
$commands 属性定义了手动加载的命令
1 | protected $commands = [ |
commands() 方法中引入自动加载命令的文件
1 | protected function commands() |
schedule() 方法中定义定时任务
有如下几种方式:
- Closure 闭包
1 | $schedule->call(function () { |
- Shell 命令
1 | $schedule->exec('node /home/forge/script.js')->daily(); |
- Artisan
1 | $schedule->command('reserve-expired')->dailyAt('02:00'); |
- Job
1 | $schedule->job(new ReceiveMoney)->everyFiveMinutes(); |

Artisan
新建一个预约已过期命令:
1 | php artisan make:command ReserveExpired |
命令结构如下
1 | namespace App\Console\Commands; |
获取输入
1 | $name = $this->ask('What is your name?'); |
请求确认
1 | if ($this->confirm('Do you wish to continue?')) { |
颜色
1 | $this->info('Success'); |
定义输入期望
在 signature 属性中定义期望用户输入的内容
- 花括号
{}为必须 - 加问号
{?}为可选 - 加等号
{=xx}为带默认值的可选
1 | protected $signature = 'wechat-menu {type?} {officialType?}'; |
当输入不符合时,直接返回提示信息:
1 | $usageString = <<<USAGE |

Queue
本次选择阿里云上提供的 RocketMQ,并使用 freyo/laravel-queue-rocketmq 包
1 | 'connections' => [ |
创建任务
1 | php artisan make:job ReceiveMoney |
在构造类中声明需要接收的参数,使用 dispatch 分发它,并指定队列:
1 | ReceiveMoney::dispatch($data, $type)->onQueue(env('ROCKETMQ_TOPIC_JOB')); |
运行队列
1 | # 常驻 |
失败处理
failed 配置队列中的任务执行失败时,保存失败任务的库与表,并用命令创建:
1 | php artisan queue:failed-table |
手动查看与操作失败任务:
1 | # 以表格展示 |
在运行时指定自动重试次数 --tries,例如 3 次:
1 | php artisan queue:listen --queue=common --tries=3 |
在类中定义一个重试时间方法,例如第一次 10 秒,第二次 1 分钟,第三次 5 分钟:
1 | public function backoff() |
完整的运行示例:
1 | php artisan queue:listen --queue=common --delay=1 --sleep=3 --tries=3 --timeout=600 --quiet | bash >> /var/log/message_common.log 2>&1 |
平时在遇到数据延迟时要及时查看队列状态