<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class AppTotal extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'AppTotal:run';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//历史数据入库
// $redis = Redis::connection('web_active');
// $app_name = DB::connection('phpLog')->table('app_set_name')->where("appName", '<>', ' ')->lists('id', 'appName');
// $redis->hmset('app_name', $app_name);
// exit;
while(1) {
$redis = Redis::connection('web_active');
//队列名称
$res = $redis->lpop('log_app_list');
//开关按钮
$lock = $redis->get('log_app_lock');
if (!empty($res)) {
list($date,$uid) = explode(':',$res);
$result = $redis->hget('log_app:'.$date, $uid);
if (!empty($result)) {
$table_name = 'app_total'.date('Ym');
$list = json_decode($result, true);
$data['uid'] = isset($list['uid']) ? $list['uid'] : '20001' ;
$data['sex'] = isset($list['sex']) ? $list['sex'] : '' ;
$data['device'] = isset($list['device']) ? $list['device'] : '' ;
$data['appList'] = isset($list['list']) ? $list['list'] : '' ;
//数据去重 flip比unique更节约性能
$data['appList'] = array_flip($data['appList']);
$data['appList'] = array_flip($data['appList']);
$data['time'] = date('Y-m-d');
//app应用过滤
$app_res = $redis->hmget('app_name', $data['appList']);
//新增加app数组
$new_app = [];
//mysql 入库数组
$mysql_new_app = [];
//获取当前redis 自增指针
$total = $redis->get('app_name_total');
foreach ($app_res as $key =>& $val) {
if (is_null($val)) {
$total += 1;
$new_app[$data['appList'][$key]] = $total;
$val = $total;
array_push($mysql_new_app,['id' => $total, 'appName'=> $data['appList'][$key]]);
}
}
if (count($new_app)){
$str = "INSERT IGNORE INTO app_set_name (id,appName) values";
foreach ($new_app as $key => $val) {
$str.= "(".$val.",'".$key."'),";
}
$str = trim($str, ',');
//$mysql_res = DB::connection('phpLog')->table('app_set_name')->insert($mysql_new_app);
$mysql_res = DB::connection('phpLog')->statement($str);
if ($mysql_res) {
// 设置redis 指针
$redis->set('app_name_total', $total);
// redis 数据入库
$redis->hmset('app_name', $new_app);
}
}
// 详情数据入库
$data['appList'] = implode(',', $app_res);
//app统计入库
DB::connection('phpLog')->statement("INSERT IGNORE INTO ".$table_name." (uid,sex,device,`time`,appList)
values('".$data['uid']."',".$data['sex'].",'".$data['device']."','".$data['time']."','".$data['appList']."')");
//log 记录 当文件达到123MB的时候产生内存保错 所有这个地方可是利用日志切割 或者 不写入 日志
Storage::disk('local')->append(DIRECTORY_SEPARATOR.'total'.DIRECTORY_SEPARATOR.'loaAppTotal.txt', date('Y-m-d H:i:s').' success '.$result."\n");
} else {
Storage::disk('local')->append(DIRECTORY_SEPARATOR.'total'.DIRECTORY_SEPARATOR.'loaAppTotal.txt', date('Y-m-d H:i:s').' error '.$result."\n");
}
}
//执行间隔
sleep(1);
//结束按钮
if ($lock == 2) {
exit;
}
//内存检测
if(memory_get_usage()>1000*1024*1024){
exit('内存溢出');//大于100M内存退出程序,防止内存泄漏被系统杀死导致任务终端
}
}
}
}
|