| 
 | 
 | 
 <?php
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 //error_reporting();
 | 
| 
 | 
 | 
 include_once __DIR__."/../vendor/autoload.php";
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 swoole_set_process_name('php-email-sync-list-my');
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 \Lib\DbPool::$clientNumber = 60;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 \Co\run(function (){
 | 
| 
 | 
 | 
     $goNum = 0;
 | 
| 
 | 
 | 
     while (1){
 | 
| 
 | 
 | 
 class SyncEmailMy{
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         if($goNum>=50){
 | 
| 
 | 
 | 
             co::sleep(1);
 | 
| 
 | 
 | 
             continue;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
     static $maxGo = 0;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         $id = redis()->lPop('sync_email_lists_my');
 | 
| 
 | 
 | 
         // _echo('读取到'.$id);
 | 
| 
 | 
 | 
         if($id && is_numeric($id)){
 | 
| 
 | 
 | 
             // 占用当前的id,占用2小时
 | 
| 
 | 
 | 
             if(redis()->add('just_sync_'.$id,time(),600)){
 | 
| 
 | 
 | 
     public static function sync(){
 | 
| 
 | 
 | 
         while (1){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                 go(function ($id) use (&$goNum){
 | 
| 
 | 
 | 
                     $goNum++;
 | 
| 
 | 
 | 
                     try{
 | 
| 
 | 
 | 
                         // 开始同步
 | 
| 
 | 
 | 
                         (new \Service\SyncMail($id))->isUidAfter(2)->sync();
 | 
| 
 | 
 | 
                     }catch (Throwable $e){
 | 
| 
 | 
 | 
                         _echo($e->getMessage());
 | 
| 
 | 
 | 
                     }
 | 
| 
 | 
 | 
             if(self::$maxGo >= 50){
 | 
| 
 | 
 | 
                 co::sleep(1);
 | 
| 
 | 
 | 
                 continue;
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                     co::defer(function () use ($id,&$goNum){
 | 
| 
 | 
 | 
                         $goNum--;
 | 
| 
 | 
 | 
                         // 30秒后 消除占用
 | 
| 
 | 
 | 
                         redis()->expire('just_sync_'.$id,30);
 | 
| 
 | 
 | 
             $id = redis()->lPop('sync_email_lists_my');
 | 
| 
 | 
 | 
             // _echo('读取到'.$id);
 | 
| 
 | 
 | 
             if($id && is_numeric($id)){
 | 
| 
 | 
 | 
                 // 占用当前的id,占用2小时
 | 
| 
 | 
 | 
                 if(redis()->add('just_sync_'.$id,time(),600)){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         \Lib\Log::getInstance()->write();
 | 
| 
 | 
 | 
                     self::go($id);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         db()->close();
 | 
| 
 | 
 | 
                     });
 | 
| 
 | 
 | 
                 },$id);
 | 
| 
 | 
 | 
                 }
 | 
| 
 | 
 | 
                 co::sleep(0.2);
 | 
| 
 | 
 | 
             }else{
 | 
| 
 | 
 | 
                 co::sleep(1);
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     public static function go($id){
 | 
| 
 | 
 | 
         go(function ($id){
 | 
| 
 | 
 | 
             self::$maxGo++;
 | 
| 
 | 
 | 
             try{
 | 
| 
 | 
 | 
                 // 开始同步
 | 
| 
 | 
 | 
                 (new \Service\SyncMail($id))->isUidAfter(2)->sync();
 | 
| 
 | 
 | 
             }catch (Throwable $e){
 | 
| 
 | 
 | 
                 _echo($e->getMessage());
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
             co::sleep(0.2);
 | 
| 
 | 
 | 
         }else{
 | 
| 
 | 
 | 
             co::sleep(1);
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             co::defer(function () use ($id){
 | 
| 
 | 
 | 
                 self::$maxGo--;
 | 
| 
 | 
 | 
                 // 30秒后 消除占用
 | 
| 
 | 
 | 
                 redis()->expire('just_sync_'.$id,30);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                 \Lib\Log::getInstance()->write();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                 db()->close();
 | 
| 
 | 
 | 
             });
 | 
| 
 | 
 | 
         },$id);
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 });
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 // 进程管理器
 | 
| 
 | 
 | 
 $pm = new \Swoole\Process\Manager();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 // 启动业务进程
 | 
| 
 | 
 | 
 $pm->addBatch(2,function (\Swoole\Process\Pool $pool, int $worker_id){
 | 
| 
 | 
 | 
     //error_reporting();
 | 
| 
 | 
 | 
     include_once __DIR__."/../vendor/autoload.php";
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     _echo('子进程重新启动了==>'.$worker_id);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     \Lib\DbPool::$clientNumber = 60;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     SyncEmailMy::sync();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 },true);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 $pm->start();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
...
 | 
...
 | 
 |