| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- namespace App\Module\Pet\Jobs;
- use App\Module\Pet\Models\PetActiveSkill;
- use App\Module\Pet\Logic\PetAutoSkillLogic;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\Log;
- /**
- * 处理宠物激活技能的定时任务
- *
- * 每分钟执行一次,检查所有激活中的宠物技能并执行相应操作
- */
- class ProcessActiveSkillsJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- /**
- * 任务超时时间(秒)
- *
- * @var int
- */
- public $timeout = 300; // 5分钟
- /**
- * 最大重试次数
- *
- * @var int
- */
- public $tries = 3;
- /**
- * 创建新的任务实例
- *
- * @return void
- */
- public function __construct()
- {
- //
- }
- /**
- * 执行任务
- *
- * @return void
- */
- public function handle()
- {
- Log::info('开始处理宠物激活技能定时任务');
- try {
- // 获取所有生效中的技能
- $activeSkills = PetActiveSkill::active()->get();
- Log::info('找到激活技能数量', ['count' => $activeSkills->count()]);
- $processedCount = 0;
- $expiredCount = 0;
- foreach ($activeSkills as $activeSkill) {
- try {
- // 检查技能是否已过期
- if ($activeSkill->isExpired()) {
- $activeSkill->markAsExpired();
- $expiredCount++;
- Log::info('技能已过期', [
- 'active_skill_id' => $activeSkill->id,
- 'pet_id' => $activeSkill->pet_id,
- 'skill_name' => $activeSkill->skill_name
- ]);
- continue;
- }
- // 检查是否需要执行检查
- if (!$activeSkill->shouldCheck()) {
- Log::debug('shouldCheck', [
- 'active_skill_id' => $activeSkill->id,
- 'pet_id' => $activeSkill->pet_id,
- 'skill_name' => $activeSkill->skill_name
- ]);
- continue;
- }
- // 处理技能效果
- $this->processSkillEffect($activeSkill);
- $processedCount++;
- } catch (\Exception $e) {
- Log::error('处理单个激活技能失败', [
- 'active_skill_id' => $activeSkill->id,
- 'pet_id' => $activeSkill->pet_id,
- 'skill_name' => $activeSkill->skill_name,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- }
- Log::info('宠物激活技能定时任务完成', [
- 'total_skills' => $activeSkills->count(),
- 'processed_count' => $processedCount,
- 'expired_count' => $expiredCount
- ]);
- } catch (\Exception $e) {
- Log::error('处理宠物激活技能定时任务失败', [
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- throw $e;
- }
- }
- /**
- * 处理技能效果
- *
- * @param PetActiveSkill $activeSkill 激活的技能
- * @return void
- */
- protected function processSkillEffect(PetActiveSkill $activeSkill): void
- {
- $autoSkillLogic = new PetAutoSkillLogic();
- switch ($activeSkill->skill_name) {
- case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_HARVEST_JOB->value:
- $autoSkillLogic->processAutoHarvest($activeSkill);
- break;
- case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_PLANT_JOB->value:
- $autoSkillLogic->processAutoPlant($activeSkill);
- break;
- case \App\Module\Pet\Enums\PET_SKILL_NAME::DISASTER_PROTECTION->value:
- $autoSkillLogic->processDisasterProtection($activeSkill);
- break;
- case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WEEDING->value:
- $autoSkillLogic->processAutoWeeding($activeSkill);
- break;
- case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WATERING->value:
- $autoSkillLogic->processAutoWatering($activeSkill);
- break;
- case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_PEST_CONTROL->value:
- $autoSkillLogic->processAutoPestControl($activeSkill);
- break;
- default:
- Log::warning('未知的技能类型', [
- 'active_skill_id' => $activeSkill->id,
- 'skill_name' => $activeSkill->skill_name
- ]);
- break;
- }
- // 更新最后检查时间
- $activeSkill->updateLastCheckTime();
- }
- /**
- * 任务失败时的处理
- *
- * @param \Throwable $exception
- * @return void
- */
- public function failed(\Throwable $exception)
- {
- Log::error('宠物激活技能定时任务失败', [
- 'error' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString()
- ]);
- }
- }
|