| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace App\Module\Game\Jobs;
- use App\Module\Game\Logics\UserLogCollectorManager;
- 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 CollectUserLogJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- /**
- * 任务最大尝试次数
- *
- * @var int
- */
- public $tries = 3;
- /**
- * 任务超时时间(秒)
- *
- * @var int
- */
- public $timeout = 60;
- /**
- * 指定收集器名称
- *
- * @var string|null
- */
- protected ?string $collectorName;
- /**
- * 创建新的任务实例
- *
- * @param string|null $collectorName 收集器名称,null表示执行所有收集器
- */
- public function __construct(?string $collectorName = null)
- {
- $this->collectorName = $collectorName;
- }
- /**
- * 执行任务
- *
- * @return void
- */
- public function handle(): void
- {
- try {
- $manager = new UserLogCollectorManager();
- if ($this->collectorName) {
- // 执行指定收集器
- $result = $manager->collectByName($this->collectorName);
- Log::info('用户日志收集任务执行完成', [
- 'collector' => $this->collectorName,
- 'processed_count' => $result['processed_count'],
- 'execution_time' => $result['execution_time'],
- 'status' => $result['status']
- ]);
- if ($result['status'] === 'error') {
- throw new \Exception("收集器 {$this->collectorName} 执行失败: {$result['error']}");
- }
- } else {
- // 执行所有收集器
- $results = $manager->collectAll();
- Log::info('用户日志收集任务执行完成', [
- 'total_processed' => $results['total_processed'],
- 'total_execution_time' => $results['total_execution_time'],
- 'collectors_count' => count($results['collectors'])
- ]);
- // 检查是否有失败的收集器
- $failedCollectors = [];
- foreach ($results['collectors'] as $name => $result) {
- if ($result['status'] === 'error') {
- $failedCollectors[] = $name;
- }
- }
- if (!empty($failedCollectors)) {
- throw new \Exception('部分收集器执行失败: ' . implode(', ', $failedCollectors));
- }
- }
- } catch (\Exception $e) {
- Log::error('用户日志收集任务异常', [
- 'collector' => $this->collectorName,
- 'error' => $e->getMessage(),
- 'attempt' => $this->attempts()
- ]);
- // 重新抛出异常以触发重试机制
- throw $e;
- }
- }
- /**
- * 任务失败时的处理
- *
- * @param \Throwable $exception
- * @return void
- */
- public function failed(\Throwable $exception): void
- {
- Log::error('用户日志收集任务最终失败', [
- 'collector' => $this->collectorName,
- 'error' => $exception->getMessage(),
- 'attempts' => $this->attempts()
- ]);
- // 可以在这里实现失败后的补偿机制
- // 比如发送告警通知或记录到特殊日志文件
- }
- /**
- * 静态方法:调度日志收集任务
- *
- * @param string|null $collectorName 收集器名称,null表示执行所有收集器
- * @return void
- */
- public static function dispatchCollection(?string $collectorName = null): void
- {
- try {
- self::dispatch($collectorName);
- Log::info('调度用户日志收集任务', [
- 'collector' => $collectorName ?? 'all'
- ]);
- } catch (\Exception $e) {
- Log::error('调度用户日志收集任务失败', [
- 'collector' => $collectorName,
- 'error' => $e->getMessage()
- ]);
- }
- }
- }
|