作者 邓超

优化同步

... ... @@ -14,7 +14,6 @@ class SyncToEsCmd {
public $isStop = false;
static $num = 0;
public function handler(){
... ... @@ -28,6 +27,9 @@ class SyncToEsCmd {
// pcntl_signal(SIGHUP, $handler);
$es = es();
$db = db();
while (1){
// 检查是否接收到信号
... ... @@ -39,45 +41,27 @@ class SyncToEsCmd {
}
$id = redis()->lPop('sync_to_es');
$code = 500;
if($id){
while(self::$num>=50){
co::sleep(0.5);
}
go(function () use ($id){
static::$num++;
$db = db();
$code = 500;
$data = $db->first(\Model\listsSql::first('`id` = '.$id));
if($data){
// 设置 进程 是否在运行
$data['is_auto']=$db->count('select count(*) from `lists_auto` where `list_id` = '.$data['id']) ? 1 : 0;
$data = $this->getEsData($data);
$doc_id = $data['email_id'].'_'.$data['folder_id'].'_'.$data['uid'];
$code = es()->save($doc_id,$data);
}
$data = $db->first(\Model\listsSql::first('`id` = '.$id));
if($data){
// 设置 进程 是否在运行
$data['is_auto']=$db->count('select count(*) from `lists_auto` where `list_id` = '.$data['id']) ? 1 : 0;
if($code!==200){
@file_put_contents(LOG_PATH.'/sync_es_fail.log',$id."\n",FILE_APPEND);
_echo('同步es: '.$doc_id.'===>'.$code);
}
$data = $this->getEsData($data);
$doc_id = $data['email_id'].'_'.$data['folder_id'].'_'.$data['uid'];
$db = null;
co::defer(function (){
static::$num--;
$code = $es->save($doc_id,$data);
}
db()->close();
if($code!==200){
@file_put_contents(LOG_PATH.'/sync_es_fail.log',$id."\n",FILE_APPEND);
_echo('同步es: '.$doc_id.'===>'.$code);
}
\Lib\Log::getInstance()->write();
});
});
}else{
co::sleep(1);
}
sleep(1);
}
}
... ... @@ -108,12 +92,7 @@ class SyncToEsCmd {
}
\Lib\DbPool::$clientNumber = 60;
\Co\run(function (){
(new SyncToEsCmd())->handler();
});
(new SyncToEsCmd())->handler();
return 1;
... ...