| 
...
 | 
...
 | 
@@ -17,112 +17,46 @@ function start(){ | 
| 
 | 
 | 
     // 进程管理器
 | 
| 
 | 
 | 
     $pm = new Process\Manager();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     // 启动一个进程来管理定时
 | 
| 
 | 
 | 
     $pm->add(function (Process\Pool $pool, int $workerId){
 | 
| 
 | 
 | 
         _echo("定时进程({$workerId})启动成功");
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         // 每秒执行
 | 
| 
 | 
 | 
         \Swoole\Timer::tick(1000,function() use(&$pool){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             if(redis()->getOriginData('email_sync_stop_num') >= WORKER_NUM+1 ){
 | 
| 
 | 
 | 
                 $pool->shutdown();
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             //
 | 
| 
 | 
 | 
             \Lib\Log::getInstance()->write();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         });
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         // 进行阻塞,否则定时器无法运行
 | 
| 
 | 
 | 
         while (true){
 | 
| 
 | 
 | 
             co::sleep(10);
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     },true);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 // 协程配置
 | 
| 
 | 
 | 
     \co::set([
 | 
| 
 | 
 | 
         'max_coroutine'=>COROUTINE_MAX_NUM, // 最大携程数量
 | 
| 
 | 
 | 
         'hook_flags'=>SWOOLE_HOOK_TCP,  //  redis需要的配置
 | 
| 
 | 
 | 
     ]);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     // 启动业务进程
 | 
| 
 | 
 | 
     $pm->addBatch(WORKER_NUM,function (Process\Pool $pool, int $worker_id){
 | 
| 
 | 
 | 
         _echo("业务进程({$worker_id})启动成功");
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         $start_num = 0;// 启动的协程数量
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         // 循环阻塞
 | 
| 
 | 
 | 
         while (true){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             if(redis()->get(SYNC_RUNNING_REDIS_KEY)=='stop'){
 | 
| 
 | 
 | 
                 break;
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             // 是否到了协程配置的数量上限
 | 
| 
 | 
 | 
             if($start_num < COROUTINE_MAX_NUM){
 | 
| 
 | 
 | 
             swoole_set_process_name('php-email-sync-list-'.$worker_id);
 | 
| 
 | 
 | 
             // 需要同步的id
 | 
| 
 | 
 | 
             $id = redis()->lPop('sync_email_lists');
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             if(!$id || !is_numeric($id)){
 | 
| 
 | 
 | 
                 co::sleep(1);
 | 
| 
 | 
 | 
                 }else{
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
             else{
 | 
| 
 | 
 | 
                 // 占用当前的id,占用2小时
 | 
| 
 | 
 | 
                 if(redis()->add('just_sync_'.$id,time(),600)){
 | 
| 
 | 
 | 
                     // 启动一个协程
 | 
| 
 | 
 | 
                         go(function () use (&$start_num,$worker_id,$id){
 | 
| 
 | 
 | 
                             $start_num++;
 | 
| 
 | 
 | 
                     go(function () use ($id){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         // 开始同步
 | 
| 
 | 
 | 
                         try {
 | 
| 
 | 
 | 
                                 sync($id,$worker_id);
 | 
| 
 | 
 | 
                             sync($id);
 | 
| 
 | 
 | 
                         }catch (\Throwable $e){
 | 
| 
 | 
 | 
                                 // 重新发布同步任务,如果失败了是否重新发布
 | 
| 
 | 
 | 
 //                            redis()->rPush('sync_email_lists',$id);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 //                            _echo($e->getMessage());
 | 
| 
 | 
 | 
                             logs(
 | 
| 
 | 
 | 
                                 $e->getMessage().PHP_EOL.$e->getTraceAsString(),
 | 
| 
 | 
 | 
                                     LOG_PATH.'/'.$worker_id.'.log'
 | 
| 
 | 
 | 
                                 LOG_PATH.'/sync/'.$id.'.log'
 | 
| 
 | 
 | 
                             );
 | 
| 
 | 
 | 
                         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         // 协程完成后执行的函数
 | 
| 
 | 
 | 
                             co::defer(function () use (&$start_num,$worker_id,$id){
 | 
| 
 | 
 | 
 //                        _echo('正常关闭进程('.$worker_id.')下的协程('.co::getCid().')');
 | 
| 
 | 
 | 
                                 $start_num--;
 | 
| 
 | 
 | 
                         co::defer(function () use ($id){
 | 
| 
 | 
 | 
                             // 消除占用
 | 
| 
 | 
 | 
                             redis()->delete('just_sync_'.$id);
 | 
| 
 | 
 | 
                             // 写入日志
 | 
| 
 | 
 | 
                             \Lib\Log::getInstance()->write();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         });
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                     });
 | 
| 
 | 
 | 
                 }
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
             }else{
 | 
| 
 | 
 | 
                 // 协程到了最大的数量,阻塞1秒
 | 
| 
 | 
 | 
                 co::sleep(1);
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         // 验证是否全部进程退出了
 | 
| 
 | 
 | 
         while (true){
 | 
| 
 | 
 | 
             if(!$start_num){
 | 
| 
 | 
 | 
                 redis()->incr('email_sync_stop_num');
 | 
| 
 | 
 | 
                 break;
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
             co::sleep(0.5);
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
         while (true){
 | 
| 
 | 
 | 
             co::sleep(99);
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     },true);
 | 
| 
...
 | 
...
 | 
@@ -130,16 +64,9 @@ function start(){ | 
| 
 | 
 | 
     // 启动一个同步内容的进程
 | 
| 
 | 
 | 
     $pm->add(function (Process\Pool $pool, int $worker_id){
 | 
| 
 | 
 | 
         _echo("业务进程({$worker_id})启动成功,body");
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         $start_num = 0;// 启动的协程数量
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         swoole_set_process_name('php-email-sync-body-'.$worker_id);
 | 
| 
 | 
 | 
         // 循环阻塞
 | 
| 
 | 
 | 
         while (true){
 | 
| 
 | 
 | 
             if(redis()->get(SYNC_RUNNING_REDIS_KEY)=='stop'){
 | 
| 
 | 
 | 
                 break;
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
             // 是否到了协程配置的数量上限
 | 
| 
 | 
 | 
             if($start_num < 500){
 | 
| 
 | 
 | 
             // 需要同步的id
 | 
| 
 | 
 | 
             $id = redis()->lPop('sync_email_body');
 | 
| 
 | 
 | 
 
 | 
| 
...
 | 
...
 | 
@@ -149,56 +76,35 @@ function start(){ | 
| 
 | 
 | 
                 // 占用当前的id,占用2小时
 | 
| 
 | 
 | 
                 if(redis()->add('just_sync_body_'.$id['lists_id'],time(),600)){
 | 
| 
 | 
 | 
                     // 启动一个协程
 | 
| 
 | 
 | 
                         go(function () use (&$start_num,$worker_id,$id){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                             $start_num++;
 | 
| 
 | 
 | 
                     go(function () use ($id){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         // 开始同步
 | 
| 
 | 
 | 
                         try {
 | 
| 
 | 
 | 
                                 sync_body($id,$worker_id);
 | 
| 
 | 
 | 
                             sync_body($id);
 | 
| 
 | 
 | 
                         }catch (\Throwable $e){
 | 
| 
 | 
 | 
 //                            _echo($e->getMessage());
 | 
| 
 | 
 | 
                             logs(
 | 
| 
 | 
 | 
                                 $e->getMessage().PHP_EOL.$e->getTraceAsString(),
 | 
| 
 | 
 | 
                                     LOG_PATH.'/'.$worker_id.'.log'
 | 
| 
 | 
 | 
                                 LOG_PATH.'/'.$id['lists_id'].'.log'
 | 
| 
 | 
 | 
                             );
 | 
| 
 | 
 | 
                         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         // 协程完成后执行的函数
 | 
| 
 | 
 | 
                             co::defer(function () use (&$start_num,$worker_id,$id){
 | 
| 
 | 
 | 
                         co::defer(function () use ($id){
 | 
| 
 | 
 | 
 //                        _echo('正常关闭进程('.$worker_id.')下的协程('.co::getCid().')');
 | 
| 
 | 
 | 
                                 $start_num--;
 | 
| 
 | 
 | 
                             // 消除占用
 | 
| 
 | 
 | 
                             redis()->delete('just_sync_body_'.$id['lists_id']);
 | 
| 
 | 
 | 
                             // 写入日志
 | 
| 
 | 
 | 
                             \Lib\Log::getInstance()->write();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         });
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                     });
 | 
| 
 | 
 | 
                 }
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
             }else{
 | 
| 
 | 
 | 
                 // 协程到了最大的数量,阻塞1秒
 | 
| 
 | 
 | 
                 co::sleep(1);
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         // 验证是否全部进程退出了
 | 
| 
 | 
 | 
         while (true){
 | 
| 
 | 
 | 
             if(!$start_num){
 | 
| 
 | 
 | 
                 redis()->incr('email_sync_stop_num');
 | 
| 
 | 
 | 
                 break;
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
             co::sleep(0.5);
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
         while (true){
 | 
| 
 | 
 | 
             co::sleep(99);
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     },true);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     // 启动管理器
 | 
| 
...
 | 
...
 | 
@@ -214,7 +120,7 @@ function start(){ | 
| 
 | 
 | 
  * @author:dc
 | 
| 
 | 
 | 
  * @time 2023/3/23 10:18
 | 
| 
 | 
 | 
  */
 | 
| 
 | 
 | 
 function sync_body($id,$worker_id){
 | 
| 
 | 
 | 
 function sync_body($id){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     // 是否有数据
 | 
| 
 | 
 | 
     if(db()->count(\Model\bodySql::has((int) $id['lists_id']))){
 | 
| 
...
 | 
...
 | 
@@ -256,7 +162,7 @@ function sync_body($id,$worker_id){ | 
| 
 | 
 | 
  * @author:dc
 | 
| 
 | 
 | 
  * @time 2023/3/10 10:19
 | 
| 
 | 
 | 
  */
 | 
| 
 | 
 | 
 function sync($email_id,$worker_id){
 | 
| 
 | 
 | 
 function sync($email_id){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     $email = db()->first(\Model\emailSql::first($email_id));
 | 
| 
 | 
 | 
     if(!$email){
 | 
| 
...
 | 
...
 | 
@@ -277,8 +183,8 @@ function sync($email_id,$worker_id){ | 
| 
 | 
 | 
 //    $mailServer->client->debug(true,LOG_PATH.'/'.$email_id.'/');
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     // 同步文件夹
 | 
| 
 | 
 | 
     $mailServer->syncFolder($email_id,db());
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     $mailServer->syncFolder($email_id);
 | 
| 
 | 
 | 
     _echo('文件夹同步成功-'.$email_id);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     // 读取到邮箱中的文件夹
 | 
| 
 | 
 | 
     $folders = db()->all(\Model\folderSql::all($email['id']));
 | 
| 
...
 | 
...
 | 
@@ -289,10 +195,12 @@ function sync($email_id,$worker_id){ | 
| 
 | 
 | 
     foreach ($folders as $folder){
 | 
| 
 | 
 | 
         try {
 | 
| 
 | 
 | 
             if(empty($folder['_child'])){
 | 
| 
 | 
 | 
                 _echo('同步文件夹('.$folder['origin_folder'].')邮件列表');
 | 
| 
 | 
 | 
                 // 同步父文件夹
 | 
| 
 | 
 | 
                 $mailServer->syncMail($email_id,$folder['id'],$folder['origin_folder']);
 | 
| 
 | 
 | 
             }else{
 | 
| 
 | 
 | 
                 foreach ($folder['_child'] as $item){
 | 
| 
 | 
 | 
                     _echo('同步文件夹('.$item['origin_folder'].')邮件列表');
 | 
| 
 | 
 | 
                     // 同步子文件夹
 | 
| 
 | 
 | 
                     $mailServer->syncMail($email_id,$item['id'],$item['origin_folder']);
 | 
| 
 | 
 | 
                 }
 | 
| 
...
 | 
...
 | 
@@ -319,41 +227,7 @@ if(!function_exists("imap_8bit")){ | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 $ps = "ps -ef | grep \"sync.php start\" | grep -v grep | wc -l";
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 switch ($argv[1]??0){
 | 
| 
 | 
 | 
     case 'start':{
 | 
| 
 | 
 | 
 //        $num = exec($ps);
 | 
| 
 | 
 | 
 //        if($num){
 | 
| 
 | 
 | 
 //            echo '正则运行,请勿重复运行';
 | 
| 
 | 
 | 
 //        }else{
 | 
| 
 | 
 | 
             start();
 | 
| 
 | 
 | 
 //        }
 | 
| 
 | 
 | 
         break;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
     case 'stop':{
 | 
| 
 | 
 | 
         \Co\run(function ($ps){
 | 
| 
 | 
 | 
             echo "正在退出程序...\n非必要请不要强制kill掉进程\n";
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             redis()->set(SYNC_RUNNING_REDIS_KEY,'stop');
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
             while (true){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                 $num = exec($ps);
 | 
| 
 | 
 | 
                 if(!$num){
 | 
| 
 | 
 | 
                     break;
 | 
| 
 | 
 | 
                 }
 | 
| 
 | 
 | 
                 co::sleep(0.2);
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
             echo "已退出程序\n";
 | 
| 
 | 
 | 
         },$ps);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         break;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
     default:{
 | 
| 
 | 
 | 
         break;
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 }
 | 
| 
 | 
 | 
 start();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
...
 | 
...
 | 
 |