Browse Source

refactor: UpgradeProgress 改为可重入,使用 Cache 跟踪已完成步骤

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
visuddhinanda 3 days ago
parent
commit
b52112dd97
1 changed files with 43 additions and 9 deletions
  1. 43 9
      api-v13/app/Console/Commands/UpgradeProgress.php

+ 43 - 9
api-v13/app/Console/Commands/UpgradeProgress.php

@@ -5,18 +5,52 @@ namespace App\Console\Commands;
 use Illuminate\Console\Attributes\Description;
 use Illuminate\Console\Attributes\Description;
 use Illuminate\Console\Attributes\Signature;
 use Illuminate\Console\Attributes\Signature;
 use Illuminate\Console\Command;
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Cache;
 
 
-#[Signature('app:upgrade-progress')]
-#[Description('Command description')]
+#[Signature('app:upgrade-progress {--fresh : Clear cached progress and start from scratch}')]
+#[Description('Run upgrade progress commands (reentrant via Cache)')]
 class UpgradeProgress extends Command
 class UpgradeProgress extends Command
 {
 {
-    /**
-     * Execute the console command.
-     */
-    public function handle()
+    // 缓存已完成的步骤名,使命令可重入:中断后重跑会跳过已完成的子命令
+    private const CACHE_KEY = 'upgrade-progress:completed-steps';
+
+    public function handle(): int
     {
     {
-        //
-        $this->call('upgrade:progress.para');
-        $this->call('upgrade:progress.chapter');
+        if ($this->option('fresh')) {
+            Cache::forget(self::CACHE_KEY);
+            $this->info('Cleared cached progress.');
+        }
+
+        $completedSteps = Cache::get(self::CACHE_KEY, []);
+
+        $steps = [
+            'para' => 'upgrade:progress.para',
+            'chapter' => 'upgrade:progress.chapter',
+        ];
+
+        foreach ($steps as $key => $command) {
+            if (in_array($key, $completedSteps)) {
+                $this->info("Skipping [{$command}] (already completed).");
+
+                continue;
+            }
+
+            $this->info("Running [{$command}]...");
+            $exitCode = $this->call($command);
+
+            if ($exitCode !== 0) {
+                $this->error("[{$command}] failed with exit code {$exitCode}. Re-run to resume.");
+
+                return $exitCode;
+            }
+
+            $completedSteps[] = $key;
+            Cache::put(self::CACHE_KEY, $completedSteps, now()->addHours(48));
+        }
+
+        Cache::forget(self::CACHE_KEY);
+        $this->info('All steps completed.');
+
+        return 0;
     }
     }
 }
 }