作者 邓超

x

正在显示 1 个修改的文件 包含 17 行增加42 行删除
1 <?php 1 <?php
2 2
3 -//error_reporting();  
4 -include_once __DIR__."/../vendor/autoload.php";  
5 -  
6 -@file_put_contents('sync_run.log',"进程启动 ".getmypid(),FILE_APPEND);  
7 3
8 swoole_set_process_name('php-email-sync-list'); 4 swoole_set_process_name('php-email-sync-list');
9 -//TODO:: 不知道为什么,隔断时间mysql会连不上  
10 -\Co\run(function (){  
11 - $start_time = time();  
12 - $goNum = 0;  
13 -// 循环阻塞  
14 - while (true){  
15 5
16 - if (time() - $start_time > 43200){  
17 - @file_put_contents('sync_run.log',"进程停止 ".system("kill ".getmypid()),FILE_APPEND);  
18 - break;  
19 - } 6 +$pm = new \Swoole\Process\Manager();
20 7
21 - if($goNum > 50){  
22 - co::sleep(0.5);  
23 - continue;  
24 - } 8 +
  9 +$pm->addBatch(60,function ($work_id) {
  10 +
  11 + include_once __DIR__."/../vendor/autoload.php";
  12 +
  13 + $number = 0;
  14 + while (true){
  15 + if($number > 500){ break; }
25 16
26 // 需要同步的id 17 // 需要同步的id
27 $id = redis()->lPop('sync_email_lists'); 18 $id = redis()->lPop('sync_email_lists');
@@ -29,9 +20,8 @@ swoole_set_process_name('php-email-sync-list'); @@ -29,9 +20,8 @@ swoole_set_process_name('php-email-sync-list');
29 if($id && is_numeric($id)){ 20 if($id && is_numeric($id)){
30 // 占用当前的id,占用2小时 21 // 占用当前的id,占用2小时
31 if(redis()->add('just_sync_'.$id,time(),600)){ 22 if(redis()->add('just_sync_'.$id,time(),600)){
32 - // 启动一个协程  
33 - go(function () use ($id,&$goNum){  
34 - $goNum++; 23 + $number++;
  24 +
35 try{ 25 try{
36 // 开始同步 26 // 开始同步
37 $email = db()->cache(3600)->first(\Model\emailSql::first($id)); 27 $email = db()->cache(3600)->first(\Model\emailSql::first($id));
@@ -42,9 +32,7 @@ swoole_set_process_name('php-email-sync-list'); @@ -42,9 +32,7 @@ swoole_set_process_name('php-email-sync-list');
42 if(!db()->cache(600)->count(\Model\listsSql::first('`id` > 0'))){ 32 if(!db()->cache(600)->count(\Model\listsSql::first('`id` > 0'))){
43 $sync->search($search->dateGt($email['created_at'])); 33 $sync->search($search->dateGt($email['created_at']));
44 }else{ 34 }else{
45 - // 是否是ai邮箱  
46 - if(db()->count("select count(*) from `hot_mail` where `email` = '{$email['email']}'")){  
47 - // ai邮件只同步2天内的 35 +
48 if(strtotime("-2 day") > strtotime($email['created_at'])){ 36 if(strtotime("-2 day") > strtotime($email['created_at'])){
49 $sync->search( 37 $sync->search(
50 $search->dateGt( 38 $search->dateGt(
@@ -60,7 +48,6 @@ swoole_set_process_name('php-email-sync-list'); @@ -60,7 +48,6 @@ swoole_set_process_name('php-email-sync-list');
60 } 48 }
61 49
62 } 50 }
63 - }  
64 51
65 $sync->sync(); 52 $sync->sync();
66 53
@@ -72,33 +59,21 @@ swoole_set_process_name('php-email-sync-list'); @@ -72,33 +59,21 @@ swoole_set_process_name('php-email-sync-list');
72 logs('sync : '.$e->getMessage()); 59 logs('sync : '.$e->getMessage());
73 } 60 }
74 61
75 -  
76 - // 协程完成后执行的函数  
77 - co::defer(function () use ($id,&$goNum){  
78 - $goNum--;  
79 // 30秒后 消除占用 62 // 30秒后 消除占用
80 redis()->expire('just_sync_'.$id,120); 63 redis()->expire('just_sync_'.$id,120);
81 - // 写入日志  
82 - \Lib\Log::getInstance()->write();  
83 - });  
84 64
85 - });  
86 } 65 }
87 } 66 }
88 -  
89 - //每次都暂停1秒,防止同一时间启动太多的任务  
90 - co::sleep(0.1);  
91 - 67 + else{
  68 + sleep(1);
  69 + }
92 70
93 } 71 }
  72 +});
94 73
95 -// while ($goNum > 0){  
96 -// if (time() - $start_time > 24060){ break; }  
97 -// co::sleep(1);  
98 -// }  
99 74
  75 +$pm->start();
100 76
101 -});  
102 77
103 78
104 79