Răsfoiți Sursa

优化:使用Laravel标准Schedule facade语法注册UCore调度

- 将Schedule导入改为使用Illuminate\Support\Facades\Schedule
- 使用Schedule::command()标准语法替代->command()
- 简化registerSchedules()方法实现
- 保持模块化调度的优势:UCore模块自管理调度
- 验证调度功能正常:php artisan schedule:list显示正确
AI Assistant 6 luni în urmă
părinte
comite
6ae4fe8ce9

+ 86 - 0
AiWork/2025年06月/23日0321-UCore任务调度迁移到ServiceProvider.md

@@ -0,0 +1,86 @@
+# UCore任务调度迁移到ServiceProvider
+
+**时间**: 2025年06月23日 03:21  
+**任务**: 将UCore的任务调度从console.php迁移到ServiceProvider注册
+
+## 任务背景
+
+根据项目架构规范,UCore任务调度应该使用ServiceProvider注册,而不是在routes/console.php中直接配置。需要将UCore相关的定时任务从console.php中移除,并在UCore的CommandServiceProvider中注册。
+
+## 实施步骤
+
+### 1. 分析现状
+- 检查routes/console.php中的UCore相关调度
+- 发现`ucore:clean-size-rotating-logs`命令每天凌晨3点执行
+- 查看UCore/Providers/CommandServiceProvider.php的现有结构
+
+### 2. 修改UCore/Providers/CommandServiceProvider.php
+- 添加`Illuminate\Console\Scheduling\Schedule`导入
+- 在boot()方法中调用registerSchedules()方法
+- 新增registerSchedules()方法:
+  - 使用`$this->app->booted()`确保在应用启动完成后注册
+  - 注册`ucore:clean-size-rotating-logs`调度:每天凌晨3点执行
+  - 添加描述信息:'清理UCore轮转日志文件'
+
+### 3. 从routes/console.php中移除UCore调度
+- 删除`ucore:clean-size-rotating-logs`的调度配置行
+- 保留其他模块的调度配置不变
+
+### 4. 验证功能
+- 执行`php artisan schedule:list`确认调度注册成功
+- 确认UCore调度显示正确的执行时间和描述
+
+## 技术实现
+
+### 修改的文件
+
+1. **UCore/Providers/CommandServiceProvider.php**
+   ```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轮转日志文件');
+       });
+   }
+   ```
+
+2. **routes/console.php**
+   - 移除了UCore相关的调度配置
+
+## 验证结果
+
+执行`php artisan schedule:list`显示:
+```
+0 3 * * *  php artisan ucore:clean-size-rotating-logs ............................ Next Due: 23小时后
+```
+
+调度成功注册,功能正常。
+
+## 优势
+
+1. **架构清晰**: UCore的调度逻辑集中在自己的ServiceProvider中
+2. **模块化**: 每个模块管理自己的调度,便于维护
+3. **可扩展**: 后续UCore新增调度可以直接在registerSchedules()方法中添加
+4. **符合规范**: 遵循项目架构设计原则
+
+## 提交信息
+
+```
+重构:将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中,实现了更好的模块化架构。调度功能保持不变,但代码组织更加清晰和规范。

+ 2 - 4
UCore/Providers/CommandServiceProvider.php

@@ -3,7 +3,7 @@
 namespace UCore\Providers;
 
 use Illuminate\Support\ServiceProvider;
-use Illuminate\Console\Scheduling\Schedule;
+use Illuminate\Support\Facades\Schedule;
 
 class CommandServiceProvider extends ServiceProvider
 {
@@ -36,10 +36,8 @@ class CommandServiceProvider extends ServiceProvider
     {
         // 在应用完全启动后注册定时任务
         $this->app->booted(function () {
-            $schedule = $this->app->make(Schedule::class);
-
             // 每天凌晨3点清理 size_rotating_daily 日志文件(保留配置的天数)
-            $schedule->command('ucore:clean-size-rotating-logs')
+            Schedule::command('ucore:clean-size-rotating-logs')
                 ->dailyAt('03:00')
                 ->description('清理UCore轮转日志文件');
         });

+ 1 - 1
app/Console/Kernel.php

@@ -35,7 +35,7 @@ class Kernel extends ConsoleKernel
     {
 
         //  命令调度不是写在这里!
-        // 调度写在 routes/console.php  中
+        // 调度写在 routes/console.php  中,或模块的 ServiceProvider 中
 
     }