| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Logics\UserLogCollectorManager;
- use UCore\Command\Command;
- /**
- * 用户日志收集命令
- *
- * 定时执行的计划任务,每2秒收集一次用户日志
- */
- class CollectUserLogsCommand extends Command
- {
- /**
- * 命令名称和参数
- *
- * @var string
- */
- protected $signature = 'game:collect-user-logs
- {--collector= : 指定收集器名称,不指定则执行所有收集器}
- {--reset : 重置收集器进度,从头开始收集}
- {--info : 显示收集器信息}
- {--stats : 显示收集统计信息}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '收集用户日志,将各模块的原始日志转换为用户友好的日志消息';
- /**
- * 执行命令
- */
- public function handleRun()
- {
- $manager = new UserLogCollectorManager();
- // 显示收集器信息
- if ($this->option('info')) {
- $this->showCollectorsInfo($manager);
- return 0;
- }
- // 重置收集器进度
- if ($this->option('reset')) {
- $this->resetCollectors($manager);
- return 0;
- }
- // 显示统计信息
- if ($this->option('stats')) {
- $this->showStats($manager);
- return 0;
- }
- // 执行日志收集
- return $this->executeCollection($manager);
- }
- /**
- * 执行日志收集
- *
- * @param UserLogCollectorManager $manager
- * @return int
- */
- private function executeCollection(UserLogCollectorManager $manager): int
- {
- $collectorName = $this->option('collector');
- try {
- if ($collectorName) {
- // 执行指定收集器
- $this->info("执行收集器: {$collectorName}");
- $result = $manager->collectByName($collectorName);
- $this->displaySingleResult($result);
- } else {
- // 执行所有收集器
- $this->info("执行所有收集器...");
- $results = $manager->collectAll();
- $this->displayAllResults($results);
- }
- return 0;
- } catch (\Exception $e) {
- $this->error("日志收集失败: {$e->getMessage()}");
- return 1;
- }
- }
- /**
- * 显示收集器信息
- *
- * @param UserLogCollectorManager $manager
- * @return void
- */
- private function showCollectorsInfo(UserLogCollectorManager $manager): void
- {
- $this->info("注册的收集器信息:");
- $this->line("");
- $collectorsInfo = $manager->getCollectorsInfo();
-
- foreach ($collectorsInfo as $info) {
- $this->line("收集器: <comment>{$info['name']}</comment>");
- $this->line(" 类名: {$info['class']}");
- $this->line(" 源表: {$info['source_table']}");
- $this->line(" 类型: {$info['source_type']}");
- $this->line("");
- }
- }
- /**
- * 重置收集器进度
- *
- * @param UserLogCollectorManager $manager
- * @return void
- */
- private function resetCollectors(UserLogCollectorManager $manager): void
- {
- $collectorName = $this->option('collector');
- if ($collectorName) {
- if (!$manager->hasCollector($collectorName)) {
- $this->error("收集器 {$collectorName} 不存在");
- return;
- }
- $manager->resetCollector($collectorName);
- $this->info("已重置收集器 {$collectorName} 的进度");
- } else {
- if ($this->confirm('确定要重置所有收集器的进度吗?这将从头开始收集所有日志。')) {
- $manager->resetAllCollectors();
- $this->info("已重置所有收集器的进度");
- }
- }
- }
- /**
- * 显示统计信息
- *
- * @param UserLogCollectorManager $manager
- * @return void
- */
- private function showStats(UserLogCollectorManager $manager): void
- {
- $this->info("收集器统计信息:");
- $this->line("");
- // 这里可以添加更详细的统计信息
- // 比如每个收集器的处理进度、最后处理时间等
- $collectorsInfo = $manager->getCollectorsInfo();
-
- foreach ($collectorsInfo as $info) {
- $this->line("收集器: <comment>{$info['name']}</comment>");
-
- // 获取最后处理的ID
- $cacheKey = "user_log_collector:last_processed_id:{$info['source_table']}";
- $lastProcessedId = \Illuminate\Support\Facades\Cache::get($cacheKey, 0);
-
- $this->line(" 最后处理ID: {$lastProcessedId}");
- $this->line("");
- }
- }
- /**
- * 显示单个收集器结果
- *
- * @param array $result
- * @return void
- */
- private function displaySingleResult(array $result): void
- {
- if ($result['status'] === 'success') {
- $this->info("收集完成:");
- $this->line(" 处理记录数: {$result['processed_count']}");
- $this->line(" 执行时间: {$result['execution_time']}ms");
- } else {
- $this->error("收集失败:");
- $this->line(" 错误信息: {$result['error']}");
- }
- }
- /**
- * 显示所有收集器结果
- *
- * @param array $results
- * @return void
- */
- private function displayAllResults(array $results): void
- {
- $this->info("收集完成:");
- $this->line(" 总处理记录数: {$results['total_processed']}");
- $this->line(" 总执行时间: {$results['total_execution_time']}ms");
- $this->line("");
- $this->info("各收集器详情:");
- foreach ($results['collectors'] as $name => $result) {
- $status = $result['status'] === 'success' ? '<info>成功</info>' : '<error>失败</error>';
- $this->line(" {$name}: {$status} - 处理{$result['processed_count']}条 - {$result['execution_time']}ms");
-
- if ($result['status'] === 'error') {
- $this->line(" 错误: {$result['error']}");
- }
- }
- }
- }
|