UpgradeProgress.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Attributes\Description;
  4. use Illuminate\Console\Attributes\Signature;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Cache;
  7. #[Signature('app:upgrade-progress {--fresh : Clear cached progress and start from scratch}')]
  8. #[Description('Run upgrade progress commands (reentrant via Cache)')]
  9. class UpgradeProgress extends Command
  10. {
  11. // 缓存已完成的步骤名,使命令可重入:中断后重跑会跳过已完成的子命令
  12. private const CACHE_KEY = 'upgrade-progress:completed-steps';
  13. public function handle(): int
  14. {
  15. if ($this->option('fresh')) {
  16. Cache::forget(self::CACHE_KEY);
  17. $this->info('Cleared cached progress.');
  18. }
  19. $completedSteps = Cache::get(self::CACHE_KEY, []);
  20. $steps = [
  21. 'para' => 'upgrade:progress.para',
  22. 'chapter' => 'upgrade:progress.chapter',
  23. ];
  24. foreach ($steps as $key => $command) {
  25. if (in_array($key, $completedSteps)) {
  26. $this->info("Skipping [{$command}] (already completed).");
  27. continue;
  28. }
  29. $this->info("Running [{$command}]...");
  30. $exitCode = $this->call($command);
  31. if ($exitCode !== 0) {
  32. $this->error("[{$command}] failed with exit code {$exitCode}. Re-run to resume.");
  33. return $exitCode;
  34. }
  35. $completedSteps[] = $key;
  36. Cache::put(self::CACHE_KEY, $completedSteps, now()->addHours(48));
  37. }
  38. Cache::forget(self::CACHE_KEY);
  39. $this->info('All steps completed.');
  40. return 0;
  41. }
  42. }