作者 邓超

优化同步

... ... @@ -2,41 +2,12 @@
//error_reporting();
use Swoole\Process;
include_once __DIR__."/../vendor/autoload.php";
function start(){
// 删除停止运行的值
// redis()->delete(SYNC_RUNNING_REDIS_KEY,'email_sync_stop_num');
// 进程管理器
$pm = new Process\Manager();
// 启动业务进程
$pm->addBatch(10,function (Process\Pool $pool, int $worker_id){
swoole_set_process_name('php-email-sync-list-'.$worker_id);
include_once __DIR__."/../vendor/autoload.php";
_echo("业务进程({$worker_id})启动成功");
$start_time = time();
$goNum = 0;
// 循环阻塞
while (true){
if($start_time+43200 < time()){
if($start_time+43400 < time()){
break;
}
co::sleep(1);
}
while ($goNum > 50){
co::sleep(0.5);
continue;
}
$runNumber = 1000;
// 循环阻塞
while ($runNumber){
$runNumber--;
// 需要同步的id
$id = redis()->lPop('sync_email_lists');
... ... @@ -44,10 +15,7 @@ function start(){
// 占用当前的id,占用2小时
if(redis()->add('just_sync_'.$id,time(),600)){
// redis()->set('sync_my_pid:'.getmypid(),time(),86400);
// 启动一个协程
go(function () use ($id,&$goNum){
$goNum++;
try{
// 开始同步
$email = db()->cache(3600)->first(\Model\emailSql::first($id));
... ... @@ -68,42 +36,20 @@ function start(){
logs('sync : '.$e->getMessage());
}
// 协程完成后执行的函数
co::defer(function () use ($id,&$goNum){
$goNum--;
// 30秒后 消除占用
redis()->expire('just_sync_'.$id,120);
// 写入日志
\Lib\Log::getInstance()->write();
});
});
}
}else{
break;
}
//每次都暂停1秒,防止同一时间启动太多的任务
co::sleep(1);
}
return 0;
},true);
// 启动管理器
$pm->start();
}
start();
... ...
... ... @@ -48,6 +48,7 @@ class Es {
->setHosts($this->host)
// ->setBasicAuthentication($user, $password)
// ->setCABundle('path/to/http_ca.crt')
->setSSLVerification(false) // 关闭 SSL 证书验证
->build();
}
... ...