|
|
<?php
|
|
|
|
|
|
//error_reporting();
|
|
|
include_once __DIR__."/../vendor/autoload.php";
|
|
|
|
|
|
swoole_set_process_name('php-email-sync-list-my');
|
|
|
|
|
|
$pm = new \Swoole\Process\Manager();
|
|
|
function stop(){
|
|
|
if(redis()->add('sync_my_pid_lock',getmypid(),10)){
|
|
|
$keys = redis()->keys('sync_my_pid:*');
|
|
|
|
|
|
foreach ($keys as $key){
|
|
|
list($k,$pid) = explode(':',$key);
|
|
|
if(posix_kill($pid,0)){
|
|
|
$t = redis()->get($key);
|
|
|
|
|
|
$pm->addBatch(30,function ($work_id){
|
|
|
//error_reporting();
|
|
|
include_once __DIR__."/../vendor/autoload.php";
|
|
|
|
|
|
$number = 0;
|
|
|
|
|
|
while (1){
|
|
|
if($number>500){
|
|
|
break;
|
|
|
}
|
|
|
$id = redis()->lPop('sync_email_lists_my');
|
|
|
|
|
|
if($id && is_numeric($id)){
|
|
|
// 占用当前的id,占用2小时
|
|
|
if(redis()->add('just_sync_'.$id,time(),600)){
|
|
|
$number++;
|
|
|
try{
|
|
|
// 开始同步
|
|
|
$email = db()->cache(3600)->first(\Model\emailSql::first($id));
|
|
|
if($email){
|
|
|
(new \Service\SyncMail($email))->sync();
|
|
|
if(time()-$t > 60){
|
|
|
_echo('向'.$pid.'发送终止信号');
|
|
|
if(posix_kill($pid,SIGTERM)){
|
|
|
redis()->delete($key);
|
|
|
}
|
|
|
|
|
|
}catch (Throwable $e){
|
|
|
logs('sync : '.$e->getMessage());
|
|
|
}
|
|
|
|
|
|
// 30秒后 消除占用
|
|
|
redis()->expire('just_sync_'.$id,120);
|
|
|
}else{
|
|
|
redis()->delete($key);
|
|
|
}
|
|
|
}else{
|
|
|
sleep(1);
|
|
|
}
|
|
|
|
|
|
redis()->delete('sync_my_pid_lock');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
while (1){
|
|
|
|
|
|
_echo('子进程即将推出');
|
|
|
$id = redis()->lPop('sync_email_lists_my');
|
|
|
redis()->set('sync_my_pid:'.getmypid(),time(),86400);
|
|
|
|
|
|
});
|
|
|
stop();
|
|
|
|
|
|
$pm->start();
|
|
|
// _echo('读取到'.$id);
|
|
|
if($id && is_numeric($id)){
|
|
|
// 占用当前的id,占用2小时
|
|
|
if(redis()->add('just_sync_'.$id,time(),600)){
|
|
|
|
|
|
try{
|
|
|
// 开始同步
|
|
|
(new \Service\SyncMail($id))->sync();
|
|
|
}catch (Throwable $e){
|
|
|
_echo($e->getMessage());
|
|
|
}
|
|
|
// 30秒后 消除占用
|
|
|
redis()->expire('just_sync_'.$id,30);
|
|
|
|
|
|
\Lib\Log::getInstance()->write();
|
|
|
|
|
|
}
|
|
|
}else{
|
|
|
sleep(1);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
...
|
...
|
|