| 
 | 
 | 
 <?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;
 | 
| 
 | 
 | 
                 if(time()-$t > 60){
 | 
| 
 | 
 | 
                     _echo('向'.$pid.'发送终止信号');
 | 
| 
 | 
 | 
                     if(posix_kill($pid,SIGTERM)){
 | 
| 
 | 
 | 
                         redis()->delete($key);
 | 
| 
 | 
 | 
                     }
 | 
| 
 | 
 | 
                 }
 | 
| 
 | 
 | 
             }else{
 | 
| 
 | 
 | 
                 redis()->delete($key);
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     while (1){
 | 
| 
 | 
 | 
         if($number>500){
 | 
| 
 | 
 | 
             break;
 | 
| 
 | 
 | 
         redis()->delete('sync_my_pid_lock');
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 while (1){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     $id = redis()->lPop('sync_email_lists_my');
 | 
| 
 | 
 | 
     redis()->set('sync_my_pid:'.getmypid(),time(),86400);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     stop();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     // _echo('读取到'.$id);
 | 
| 
 | 
 | 
     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();
 | 
| 
 | 
 | 
                     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                 (new \Service\SyncMail($id))->sync();
 | 
| 
 | 
 | 
             }catch (Throwable $e){
 | 
| 
 | 
 | 
                     logs('sync : '.$e->getMessage());
 | 
| 
 | 
 | 
                 _echo($e->getMessage());
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             // 30秒后 消除占用
 | 
| 
 | 
 | 
                 redis()->expire('just_sync_'.$id,120);
 | 
| 
 | 
 | 
             redis()->expire('just_sync_'.$id,30);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             \Lib\Log::getInstance()->write();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
     }else{
 | 
| 
 | 
 | 
         sleep(1);
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     _echo('子进程即将推出');
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 });
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 $pm->start();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
...
 | 
...
 | 
 |