时间: 2025年06月23日 03:21
任务: 将UCore的任务调度从console.php迁移到ServiceProvider注册
根据项目架构规范,UCore任务调度应该使用ServiceProvider注册,而不是在routes/console.php中直接配置。需要将UCore相关的定时任务从console.php中移除,并在UCore的CommandServiceProvider中注册。
ucore:clean-size-rotating-logs命令每天凌晨3点执行Illuminate\Console\Scheduling\Schedule导入$this->app->booted()确保在应用启动完成后注册ucore:clean-size-rotating-logs调度:每天凌晨3点执行ucore:clean-size-rotating-logs的调度配置行php artisan schedule:list确认调度注册成功UCore/Providers/CommandServiceProvider.php
protected function registerSchedules(): void
{
// 在应用完全启动后注册定时任务
$this->app->booted(function () {
$schedule = $this->app->make(Schedule::class);
// 每天凌晨3点清理 size_rotating_daily 日志文件(保留配置的天数)
$schedule->command('ucore:clean-size-rotating-logs')
->dailyAt('03:00')
->description('清理UCore轮转日志文件');
});
}
routes/console.php
执行php artisan schedule:list显示:
0 3 * * * php artisan ucore:clean-size-rotating-logs ............................ Next Due: 23小时后
调度成功注册,功能正常。
重构:将UCore任务调度从console.php迁移到ServiceProvider注册
- 在UCore/Providers/CommandServiceProvider.php中添加registerSchedules()方法
- 使用$this->app->booted()确保在应用启动完成后注册调度
- 将ucore:clean-size-rotating-logs调度从routes/console.php迁移到ServiceProvider
- 保持调度功能不变:每天凌晨3点清理轮转日志文件
- 验证调度注册成功,php artisan schedule:list显示正常
成功将UCore的任务调度从routes/console.php迁移到UCore的CommandServiceProvider中,实现了更好的模块化架构。调度功能保持不变,但代码组织更加清晰和规范。