|
@@ -4,37 +4,58 @@ |
|
@@ -4,37 +4,58 @@ |
4
|
|
4
|
|
5
|
use Swoole\Process;
|
5
|
use Swoole\Process;
|
6
|
|
6
|
|
7
|
-include_once __DIR__."/../vendor/autoload.php";
|
|
|
8
|
-swoole_set_process_name('php-email-sync-list');
|
7
|
+
|
9
|
|
8
|
|
10
|
function start(){
|
9
|
function start(){
|
11
|
- // 需要同步的id
|
|
|
12
|
- $id = redis()->lPop('sync_email_lists');
|
|
|
13
|
-
|
|
|
14
|
- if($id && is_numeric($id)){
|
|
|
15
|
- // 占用当前的id,占用2小时
|
|
|
16
|
- if(redis()->add('just_sync_'.$id,time(),600)){
|
|
|
17
|
- go(function () use ($id){
|
|
|
18
|
- echo \Swlib\SaberGM::get('http://mail-serve.hagro.cn/v3/sync?id='.$id,['timeout'=>60])->getBody()->getContents();
|
|
|
19
|
- });
|
10
|
+
|
|
|
11
|
+// 删除停止运行的值
|
|
|
12
|
+// redis()->delete(SYNC_RUNNING_REDIS_KEY,'email_sync_stop_num');
|
|
|
13
|
+
|
|
|
14
|
+ // 进程管理器
|
|
|
15
|
+ $pm = new Process\Manager();
|
|
|
16
|
+
|
|
|
17
|
+ // 启动业务进程
|
|
|
18
|
+ $pm->addBatch(10,function (Process\Pool $pool, int $worker_id){
|
|
|
19
|
+
|
|
|
20
|
+ swoole_set_process_name('php-email-sync-list-'.$worker_id);
|
|
|
21
|
+
|
|
|
22
|
+ include_once __DIR__."/../vendor/autoload.php";
|
|
|
23
|
+ _echo("业务进程({$worker_id})启动成功");
|
|
|
24
|
+
|
|
|
25
|
+ // 循环阻塞
|
|
|
26
|
+ while (true){
|
|
|
27
|
+
|
|
|
28
|
+ // 需要同步的id
|
|
|
29
|
+ $id = redis()->lPop('sync_email_lists');
|
|
|
30
|
+
|
|
|
31
|
+ if($id && is_numeric($id)){
|
|
|
32
|
+ // 占用当前的id,占用2小时
|
|
|
33
|
+ if(redis()->add('just_sync_'.$id,time(),600)){
|
|
|
34
|
+ // 启动一个协程
|
|
|
35
|
+ go(function () use ($id){
|
|
|
36
|
+ echo file_get_contents('http://mail-serve.hagro.cn/v3/sync?id='.$id);
|
|
|
37
|
+ });
|
|
|
38
|
+
|
|
|
39
|
+ }
|
|
|
40
|
+ }else{
|
|
|
41
|
+ co::sleep(1);
|
|
|
42
|
+ }
|
|
|
43
|
+ //每次都暂停1秒,防止同一时间启动太多的任务
|
|
|
44
|
+ co::sleep(0.5);
|
20
|
}
|
45
|
}
|
21
|
- }else{
|
|
|
22
|
- co::sleep(1);
|
|
|
23
|
- }
|
|
|
24
|
- //每次都暂停1秒,防止同一时间启动太多的任务
|
|
|
25
|
- co::sleep(0.5);
|
46
|
+
|
|
|
47
|
+ },true);
|
|
|
48
|
+
|
|
|
49
|
+
|
|
|
50
|
+ // 启动管理器
|
|
|
51
|
+ $pm->start();
|
26
|
|
52
|
|
27
|
}
|
53
|
}
|
28
|
|
54
|
|
29
|
|
55
|
|
30
|
|
56
|
|
31
|
-\Co\run(function (){
|
|
|
32
|
- // 循环阻塞
|
|
|
33
|
- while (true) {
|
|
|
34
|
- start();
|
|
|
35
|
- }
|
|
|
36
|
-});
|
|
|
37
|
|
57
|
|
|
|
58
|
+start();
|
38
|
|
59
|
|
39
|
|
60
|
|
40
|
|
61
|
|