Parcourir la source

重构:将UCore任务调度从console.php迁移到ServiceProvider注册

- 在UCore/Providers/CommandServiceProvider.php中添加registerSchedules()方法
- 使用->app->booted()确保在应用启动完成后注册调度
- 将ucore:clean-size-rotating-logs调度从routes/console.php迁移到ServiceProvider
- 保持调度功能不变:每天凌晨3点清理轮转日志文件
- 验证调度注册成功,php artisan schedule:list显示正常
AI Assistant il y a 6 mois
Parent
commit
269fea5082
3 fichiers modifiés avec 25 ajouts et 3 suppressions
  1. 2 1
      AiWork/now.md
  2. 23 0
      UCore/Providers/CommandServiceProvider.php
  3. 0 2
      routes/console.php

+ 2 - 1
AiWork/now.md

@@ -1,11 +1,12 @@
 # 当前工作状态
 
-**最后更新**: 2025年06月23日 02:49
+**最后更新**: 2025年06月23日 03:21
 
 ## 正在进行的任务
 - 无
 
 ## 最近完成的任务
+- ✅ UCore任务调度从console.log迁移到ServiceProvider注册(2025-06-23 03:21)
 - ✅ 修复推荐注册奖励功能并完成三级推荐奖励测试(2025-06-23 02:49)
 - ✅ 增加日志收集器启用/禁用配置功能(2025-06-23 01:45)
 - ✅ 重构用户日志收集器架构,消除重复MaxId获取逻辑(2025-06-23 01:32)

+ 23 - 0
UCore/Providers/CommandServiceProvider.php

@@ -3,11 +3,13 @@
 namespace UCore\Providers;
 
 use Illuminate\Support\ServiceProvider;
+use Illuminate\Console\Scheduling\Schedule;
 
 class CommandServiceProvider extends ServiceProvider
 {
     public function boot()
     {
+        // 注册UCore命令
         $this->commands([
             \UCore\Commands\GenerateModelAnnotation::class,
             \UCore\Commands\GenerateAppTreeCommand::class,
@@ -15,10 +17,31 @@ class CommandServiceProvider extends ServiceProvider
 
             \UCore\Commands\ExampleCommand::class
         ]);
+
+        // 注册UCore定时任务
+        $this->registerSchedules();
     }
 
     public function register()
     {
         //
     }
+
+    /**
+     * 注册UCore相关的定时任务
+     *
+     * 将原本在 routes/console.php 中的UCore调度配置迁移到此处
+     */
+    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轮转日志文件');
+        });
+    }
 }

+ 0 - 2
routes/console.php

@@ -16,8 +16,6 @@ Artisan::command('inspire', function () {
 \Illuminate\Support\Facades\Schedule::command(\App\Module\Farm\Commands\GenerateDisastersCommand::class)->everyMinute();
 // 每分钟处理宠物激活技能
 \Illuminate\Support\Facades\Schedule::command('pet:process-active-skills')->everyMinute();
-// 每天凌晨3点清理 size_rotating_daily 日志文件(保留6天)
-\Illuminate\Support\Facades\Schedule::command('ucore:clean-size-rotating-logs')->dailyAt('03:00');
 
 // 每分钟收集用户日志(实现高频收集)
 //\Illuminate\Support\Facades\Schedule::command('game:collect-user-logs --limit=100')->everyMinute();