作者 邓超

预热邮件

1 <?php 1 <?php
2 2
  3 +//error_reporting();
  4 +include_once __DIR__."/../vendor/autoload.php";
3 5
4 swoole_set_process_name('php-email-sync-list-my'); 6 swoole_set_process_name('php-email-sync-list-my');
5 7
6 -$pm = new \Swoole\Process\Manager(); 8 +function stop(){
  9 + if(redis()->add('sync_my_pid_lock',getmypid(),10)){
  10 + $keys = redis()->keys('sync_my_pid:*');
7 11
  12 + foreach ($keys as $key){
  13 + list($k,$pid) = explode(':',$key);
  14 + if(posix_kill($pid,0)){
  15 + $t = redis()->get($key);
8 16
9 -$pm->addBatch(30,function ($work_id){  
10 - //error_reporting();  
11 - include_once __DIR__."/../vendor/autoload.php";  
12 -  
13 - $number = 0;  
14 -  
15 - while (1){  
16 - if($number>500){  
17 - break;  
18 - }  
19 - $id = redis()->lPop('sync_email_lists_my');  
20 -  
21 - if($id && is_numeric($id)){  
22 - // 占用当前的id,占用2小时  
23 - if(redis()->add('just_sync_'.$id,time(),600)){  
24 - $number++;  
25 - try{  
26 - // 开始同步  
27 - $email = db()->cache(3600)->first(\Model\emailSql::first($id));  
28 - if($email){  
29 - (new \Service\SyncMail($email))->sync(); 17 + if(time()-$t > 60){
  18 + _echo('向'.$pid.'发送终止信号');
  19 + if(posix_kill($pid,SIGTERM)){
  20 + redis()->delete($key);
30 } 21 }
31 -  
32 - }catch (Throwable $e){  
33 - logs('sync : '.$e->getMessage());  
34 } 22 }
35 -  
36 - // 30秒后 消除占用  
37 - redis()->expire('just_sync_'.$id,120); 23 + }else{
  24 + redis()->delete($key);
38 } 25 }
39 - }else{  
40 - sleep(1);  
41 } 26 }
  27 +
  28 + redis()->delete('sync_my_pid_lock');
42 } 29 }
  30 +}
  31 +
  32 +while (1){
43 33
44 - _echo('子进程即将推出'); 34 + $id = redis()->lPop('sync_email_lists_my');
  35 + redis()->set('sync_my_pid:'.getmypid(),time(),86400);
45 36
46 -}); 37 + stop();
47 38
48 -$pm->start(); 39 + // _echo('读取到'.$id);
  40 + if($id && is_numeric($id)){
  41 + // 占用当前的id,占用2小时
  42 + if(redis()->add('just_sync_'.$id,time(),600)){
  43 +
  44 + try{
  45 + // 开始同步
  46 + (new \Service\SyncMail($id))->sync();
  47 + }catch (Throwable $e){
  48 + _echo($e->getMessage());
  49 + }
  50 + // 30秒后 消除占用
  51 + redis()->expire('just_sync_'.$id,30);
  52 +
  53 + \Lib\Log::getInstance()->write();
  54 +
  55 + }
  56 + }else{
  57 + sleep(1);
  58 + }
49 59
  60 +}
50 61
51 62
52 63