| 
 | 
 | 
 <?php
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 //error_reporting();
 | 
| 
 | 
 | 
 include_once __DIR__."/../vendor/autoload.php";
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 @file_put_contents('sync_run.log',"进程启动 ".getmypid(),FILE_APPEND);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 swoole_set_process_name('php-email-sync-list');
 | 
| 
 | 
 | 
 //TODO:: 不知道为什么,隔断时间mysql会连不上
 | 
| 
 | 
 | 
 \Co\run(function (){
 | 
| 
 | 
 | 
     $start_time = time();
 | 
| 
 | 
 | 
     $goNum = 0;
 | 
| 
 | 
 | 
 // 循环阻塞
 | 
| 
 | 
 | 
     while (true){
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         if (time() - $start_time > 43200){
 | 
| 
 | 
 | 
             @file_put_contents('sync_run.log',"进程停止 ".system("kill ".getmypid()),FILE_APPEND);
 | 
| 
 | 
 | 
             break;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 $pm = new \Swoole\Process\Manager();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         if($goNum > 50){
 | 
| 
 | 
 | 
             co::sleep(0.5);
 | 
| 
 | 
 | 
             continue;
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 $pm->addBatch(60,function ($work_id) {
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     include_once __DIR__."/../vendor/autoload.php";
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     $number = 0;
 | 
| 
 | 
 | 
     while (true){
 | 
| 
 | 
 | 
         if($number > 500){  break;  }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         // 需要同步的id
 | 
| 
 | 
 | 
         $id = redis()->lPop('sync_email_lists');
 | 
| 
...
 | 
...
 | 
@@ -29,9 +20,8 @@ swoole_set_process_name('php-email-sync-list'); | 
| 
 | 
 | 
         if($id && is_numeric($id)){
 | 
| 
 | 
 | 
             // 占用当前的id,占用2小时
 | 
| 
 | 
 | 
             if(redis()->add('just_sync_'.$id,time(),600)){
 | 
| 
 | 
 | 
                 // 启动一个协程
 | 
| 
 | 
 | 
                 go(function () use ($id,&$goNum){
 | 
| 
 | 
 | 
                     $goNum++;
 | 
| 
 | 
 | 
                 $number++;
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                 try{
 | 
| 
 | 
 | 
                     // 开始同步
 | 
| 
 | 
 | 
                     $email = db()->cache(3600)->first(\Model\emailSql::first($id));
 | 
| 
...
 | 
...
 | 
@@ -42,9 +32,7 @@ swoole_set_process_name('php-email-sync-list'); | 
| 
 | 
 | 
                         if(!db()->cache(600)->count(\Model\listsSql::first('`id` > 0'))){
 | 
| 
 | 
 | 
                             $sync->search($search->dateGt($email['created_at']));
 | 
| 
 | 
 | 
                         }else{
 | 
| 
 | 
 | 
                                 // 是否是ai邮箱
 | 
| 
 | 
 | 
                                 if(db()->count("select count(*) from `hot_mail` where `email` = '{$email['email']}'")){
 | 
| 
 | 
 | 
                                     // ai邮件只同步2天内的
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                             if(strtotime("-2 day") > strtotime($email['created_at'])){
 | 
| 
 | 
 | 
                                 $sync->search(
 | 
| 
 | 
 | 
                                     $search->dateGt(
 | 
| 
...
 | 
...
 | 
@@ -60,7 +48,6 @@ swoole_set_process_name('php-email-sync-list'); | 
| 
 | 
 | 
                             }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         }
 | 
| 
 | 
 | 
                             }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                         $sync->sync();
 | 
| 
 | 
 | 
 
 | 
| 
...
 | 
...
 | 
@@ -72,33 +59,21 @@ swoole_set_process_name('php-email-sync-list'); | 
| 
 | 
 | 
                     logs('sync : '.$e->getMessage());
 | 
| 
 | 
 | 
                 }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                     // 协程完成后执行的函数
 | 
| 
 | 
 | 
                     co::defer(function () use ($id,&$goNum){
 | 
| 
 | 
 | 
                         $goNum--;
 | 
| 
 | 
 | 
                 // 30秒后 消除占用
 | 
| 
 | 
 | 
                 redis()->expire('just_sync_'.$id,120);
 | 
| 
 | 
 | 
                         // 写入日志
 | 
| 
 | 
 | 
                         \Lib\Log::getInstance()->write();
 | 
| 
 | 
 | 
                     });
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
                 });
 | 
| 
 | 
 | 
             }
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         //每次都暂停1秒,防止同一时间启动太多的任务
 | 
| 
 | 
 | 
         co::sleep(0.1);
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
         else{
 | 
| 
 | 
 | 
             sleep(1);
 | 
| 
 | 
 | 
         }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
     }
 | 
| 
 | 
 | 
 });
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 //    while ($goNum > 0){
 | 
| 
 | 
 | 
 //        if (time() - $start_time > 24060){ break; }
 | 
| 
 | 
 | 
 //        co::sleep(1);
 | 
| 
 | 
 | 
 //    }
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 $pm->start();
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 });
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
| 
 | 
 | 
 
 | 
...
 | 
...
 | 
 |