Browse Source

为pet:process-active-skills命令添加同步处理参数:支持--sync选项绕过队列直接处理,并修复PetActiveSkill模型config字段类型转换问题

notfff 7 months ago
parent
commit
3cf0d732a1

+ 4 - 0
AiWork/WORK.md

@@ -20,7 +20,11 @@ shop_items 的 $max_buy 确认被替代后移除,使用mcp执行sql
 
 ## 待处理任务
 
+guomu@guomu-d5:/data/wwwroot/nusuus/kknongchang/kku_laravel$ php artisan pet:process-active-skills
+开始处理宠物激活技能...
+宠物激活技能处理任务已分发到队列
 
+增加一个参数,可以同步处理(不入队列)
 
 ## 已完成任务(保留最新的10条,多余的删除)
 

+ 21 - 6
app/Module/Pet/Console/ProcessActiveSkillsCommand.php

@@ -18,7 +18,7 @@ class ProcessActiveSkillsCommand extends Command
      *
      * @var string
      */
-    protected $signature = 'pet:process-active-skills';
+    protected $signature = 'pet:process-active-skills {--sync : 同步处理,不使用队列}';
 
     /**
      * 命令描述
@@ -37,12 +37,26 @@ class ProcessActiveSkillsCommand extends Command
         $this->info('开始处理宠物激活技能...');
 
         try {
-            // 分发任务到队列
-            ProcessActiveSkillsJob::dispatch();
+            $sync = $this->option('sync');
 
-            $this->info('宠物激活技能处理任务已分发到队列');
+            if ($sync) {
+                // 同步处理,直接执行任务逻辑
+                $this->info('使用同步模式处理...');
 
-            Log::info('宠物激活技能定时命令执行成功');
+                $job = new ProcessActiveSkillsJob();
+                $job->handle();
+
+                $this->info('宠物激活技能处理完成(同步模式)');
+
+                Log::info('宠物激活技能定时命令执行成功(同步模式)');
+            } else {
+                // 分发任务到队列
+                ProcessActiveSkillsJob::dispatch();
+
+                $this->info('宠物激活技能处理任务已分发到队列');
+
+                Log::info('宠物激活技能定时命令执行成功(队列模式)');
+            }
 
             return Command::SUCCESS;
 
@@ -51,7 +65,8 @@ class ProcessActiveSkillsCommand extends Command
 
             Log::error('宠物激活技能定时命令执行失败', [
                 'error' => $e->getMessage(),
-                'trace' => $e->getTraceAsString()
+                'trace' => $e->getTraceAsString(),
+                'sync_mode' => $this->option('sync')
             ]);
 
             return Command::FAILURE;

+ 72 - 3
app/Module/Pet/Models/PetActiveSkill.php

@@ -149,6 +149,20 @@ class PetActiveSkill extends ModelCore
     public function updateLastCheckTime(): bool
     {
         $config = $this->config;
+
+        // 确保config是数组类型
+        if (!is_array($config)) {
+            // 如果是字符串,尝试解析JSON
+            if (is_string($config)) {
+                $config = json_decode($config, true);
+                if (json_last_error() !== JSON_ERROR_NONE) {
+                    $config = [];
+                }
+            } else {
+                $config = [];
+            }
+        }
+
         $config['last_check_time'] = now()->toDateTimeString();
         $this->config = $config;
         return $this->save();
@@ -161,7 +175,21 @@ class PetActiveSkill extends ModelCore
      */
     public function getLastCheckTime(): ?\Carbon\Carbon
     {
-        $lastCheckTime = $this->config['last_check_time'] ?? null;
+        $config = $this->config;
+
+        // 确保config是数组类型
+        if (!is_array($config)) {
+            if (is_string($config)) {
+                $config = json_decode($config, true);
+                if (json_last_error() !== JSON_ERROR_NONE) {
+                    return null;
+                }
+            } else {
+                return null;
+            }
+        }
+
+        $lastCheckTime = $config['last_check_time'] ?? null;
         return $lastCheckTime ? \Carbon\Carbon::parse($lastCheckTime) : null;
     }
 
@@ -181,7 +209,21 @@ class PetActiveSkill extends ModelCore
             return true;
         }
 
-        $checkInterval = $this->config['check_interval'] ?? 60; // 默认60秒
+        $config = $this->config;
+
+        // 确保config是数组类型
+        if (!is_array($config)) {
+            if (is_string($config)) {
+                $config = json_decode($config, true);
+                if (json_last_error() !== JSON_ERROR_NONE) {
+                    $config = [];
+                }
+            } else {
+                $config = [];
+            }
+        }
+
+        $checkInterval = $config['check_interval'] ?? 60; // 默认60秒
         return now()->diffInSeconds($lastCheckTime) >= $checkInterval;
     }
 
@@ -194,7 +236,21 @@ class PetActiveSkill extends ModelCore
      */
     public function getConfigValue(string $key, $default = null)
     {
-        return $this->config[$key] ?? $default;
+        $config = $this->config;
+
+        // 确保config是数组类型
+        if (!is_array($config)) {
+            if (is_string($config)) {
+                $config = json_decode($config, true);
+                if (json_last_error() !== JSON_ERROR_NONE) {
+                    return $default;
+                }
+            } else {
+                return $default;
+            }
+        }
+
+        return $config[$key] ?? $default;
     }
 
     /**
@@ -207,6 +263,19 @@ class PetActiveSkill extends ModelCore
     public function setConfigValue(string $key, $value): bool
     {
         $config = $this->config;
+
+        // 确保config是数组类型
+        if (!is_array($config)) {
+            if (is_string($config)) {
+                $config = json_decode($config, true);
+                if (json_last_error() !== JSON_ERROR_NONE) {
+                    $config = [];
+                }
+            } else {
+                $config = [];
+            }
+        }
+
         $config[$key] = $value;
         $this->config = $config;
         return $this->save();