Ver código fonte

修复脚本

tgz 2 anos atrás
pai
commit
d438ea3b9d
2 arquivos alterados com 66 adições e 4 exclusões
  1. 56 0
      app/command/FixTrackLog.php
  2. 10 4
      app/services/TrackService.php

+ 56 - 0
app/command/FixTrackLog.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace app\command;
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Output\OutputInterface;
+use support\Redis;
+use App\services\TrackService;
+
+
+class FixTrackLog extends Command
+{
+    protected static $defaultName = 'fix_track_log';
+    protected static $defaultDescription = '根据 /app/runtime/logs/tarck/ 下的文件修复日志';
+
+    /**
+     * @return void
+     */
+    protected function configure()
+    {
+        //$this->addArgument('file', InputArgument::OPTIONAL, '文件路径及其名称');
+        $this->addOption('file', 'file', InputOption::VALUE_REQUIRED, '要解析的文件名称');
+    }
+
+    /**
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     * @return int
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $file = $input->getOption('file');
+        $fileData = function($file) {
+            $content = fopen($file, 'r');
+            //if (!$file) die('file does not exist or cannot be opened');
+            while (($line = fgets($content)) !== false) {
+                yield $line;
+            }
+            fclose($content);
+        };
+        foreach ($fileData($file) as $line) {
+            $line = explode(": ", $line);
+            $paramsc  = $line[count($line)-1];
+            $params = json_decode($paramsc,true);
+            if (!is_array($params)) {
+                $output->writeln('不是json:'.$params);
+            }
+            TrackService::push($params,1);
+        }
+        return 1;
+    }
+}
+

+ 10 - 4
app/services/TrackService.php

@@ -12,14 +12,18 @@ class TrackService
     
     /**
      *  把请求信息,放到redis队列中
+     *  @params  这是get请求的 数据的数组
+     *  @fix     1 代表是从日志文件取出来的修复数据,默认是0;是监测链接过来的数据
      */
-    public static function push ($params){
+    public static function push ($params,$fix=0){
         $dycallback = getProp($params,'dycallback');
         $track_data = json_encode($params);
         switch ($dycallback) {
             case 1:
                 //投递队列
-                $params['log_time'] = date('Y-m-d H:i:s',time());
+                if(empty($params['log_time'])){
+                    $params['log_time'] = date('Y-m-d H:i:s',time());
+                }
                 Redis::lpush('tiktok_track',$track_data);
                 // if (!Redis::get('tiktok_track_deal_lock')) { // 锁住
                 //     Redis::setEx('tiktok_track_deal_lock',1,1);
@@ -30,8 +34,10 @@ class TrackService
                 // code...
                 break;
         }
-        // 记录 json 日志信息;
-        Logger::track(json_encode($params));
+        // 修复数据不需要-记录 json 日志信息;
+        if (!$fix) {
+            Logger::track(json_encode($params));
+        }
     }
     
     /**