作者 邓超

proxy

1 <?php 1 <?php
2 2
3 -//error_reporting();  
4 -include_once __DIR__."/../vendor/autoload.php";  
5 3
6 -swoole_set_process_name('php-email-sync-list-my');  
7 4
8 -\Lib\DbPool::$clientNumber = 60;  
9 5
10 -\Co\run(function (){  
11 - $goNum = 0; 6 +class SyncEmailMy{
  7 +
  8 + static $maxGo = 0;
  9 +
  10 + public static function sync(){
12 while (1){ 11 while (1){
13 12
14 - if($goNum>=50){ 13 + if(self::$maxGo >= 50){
15 co::sleep(1); 14 co::sleep(1);
16 continue; 15 continue;
17 } 16 }
@@ -22,8 +21,20 @@ swoole_set_process_name('php-email-sync-list-my'); @@ -22,8 +21,20 @@ swoole_set_process_name('php-email-sync-list-my');
22 // 占用当前的id,占用2小时 21 // 占用当前的id,占用2小时
23 if(redis()->add('just_sync_'.$id,time(),600)){ 22 if(redis()->add('just_sync_'.$id,time(),600)){
24 23
25 - go(function ($id) use (&$goNum){  
26 - $goNum++; 24 + self::go($id);
  25 +
  26 + }
  27 + co::sleep(0.2);
  28 + }else{
  29 + co::sleep(1);
  30 + }
  31 +
  32 + }
  33 + }
  34 +
  35 + public static function go($id){
  36 + go(function ($id){
  37 + self::$maxGo++;
27 try{ 38 try{
28 // 开始同步 39 // 开始同步
29 (new \Service\SyncMail($id))->isUidAfter(2)->sync(); 40 (new \Service\SyncMail($id))->isUidAfter(2)->sync();
@@ -31,8 +42,8 @@ swoole_set_process_name('php-email-sync-list-my'); @@ -31,8 +42,8 @@ swoole_set_process_name('php-email-sync-list-my');
31 _echo($e->getMessage()); 42 _echo($e->getMessage());
32 } 43 }
33 44
34 - co::defer(function () use ($id,&$goNum){  
35 - $goNum--; 45 + co::defer(function () use ($id){
  46 + self::$maxGo--;
36 // 30秒后 消除占用 47 // 30秒后 消除占用
37 redis()->expire('just_sync_'.$id,30); 48 redis()->expire('just_sync_'.$id,30);
38 49
@@ -41,16 +52,31 @@ swoole_set_process_name('php-email-sync-list-my'); @@ -41,16 +52,31 @@ swoole_set_process_name('php-email-sync-list-my');
41 db()->close(); 52 db()->close();
42 }); 53 });
43 },$id); 54 },$id);
  55 + }
  56 +
  57 +}
  58 +
  59 +
  60 +// 进程管理器
  61 +$pm = new \Swoole\Process\Manager();
  62 +
  63 +// 启动业务进程
  64 +$pm->addBatch(2,function (\Swoole\Process\Pool $pool, int $worker_id){
  65 + //error_reporting();
  66 + include_once __DIR__."/../vendor/autoload.php";
  67 +
  68 + _echo('子进程重新启动了==>'.$worker_id);
  69 +
  70 + \Lib\DbPool::$clientNumber = 60;
  71 +
  72 + SyncEmailMy::sync();
  73 +
  74 +},true);
  75 +
  76 +$pm->start();
44 77
45 78
46 - }  
47 - co::sleep(0.2);  
48 - }else{  
49 - co::sleep(1);  
50 - }  
51 79
52 - }  
53 -});  
54 80
55 81
56 82