ProcessActiveSkillsCommand.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Module\Pet\Console;
  3. use App\Module\Pet\Jobs\ProcessActiveSkillsJob;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 处理宠物激活技能的定时命令
  8. *
  9. * 每分钟执行一次,处理所有激活中的宠物技能
  10. */
  11. class ProcessActiveSkillsCommand extends Command
  12. {
  13. /**
  14. * 命令签名
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'pet:process-active-skills {--sync : 同步处理,不使用队列}';
  19. /**
  20. * 命令描述
  21. *
  22. * @var string
  23. */
  24. protected $description = '处理宠物激活技能的定时任务';
  25. /**
  26. * 执行命令
  27. *
  28. * @return int
  29. */
  30. public function handle()
  31. {
  32. $this->info('开始处理宠物激活技能...');
  33. try {
  34. $sync = $this->option('sync');
  35. if ($sync) {
  36. // 同步处理,直接执行任务逻辑
  37. $this->info('使用同步模式处理...');
  38. $job = new ProcessActiveSkillsJob();
  39. $job->run();
  40. $this->info('宠物激活技能处理完成(同步模式)');
  41. Log::info('宠物激活技能定时命令执行成功(同步模式)');
  42. } else {
  43. // 分发任务到队列
  44. ProcessActiveSkillsJob::dispatch();
  45. $this->info('宠物激活技能处理任务已分发到队列');
  46. Log::info('宠物激活技能定时命令执行成功(队列模式)');
  47. }
  48. return Command::SUCCESS;
  49. } catch (\Exception $e) {
  50. $this->error('处理宠物激活技能失败: ' . $e->getMessage());
  51. Log::error('宠物激活技能定时命令执行失败', [
  52. 'error' => $e->getMessage(),
  53. 'trace' => $e->getTraceAsString(),
  54. 'sync_mode' => $this->option('sync')
  55. ]);
  56. return Command::FAILURE;
  57. }
  58. }
  59. }