|
@@ -4,79 +4,57 @@ |
|
@@ -4,79 +4,57 @@ |
4
|
|
4
|
|
5
|
use Swoole\Process;
|
5
|
use Swoole\Process;
|
6
|
|
6
|
|
|
|
7
|
+include_once __DIR__."/../vendor/autoload.php";
|
7
|
|
8
|
|
|
|
9
|
+\Co\run(function (){
|
|
|
10
|
+ $goNum = 0;
|
|
|
11
|
+ while (true){
|
8
|
|
12
|
|
9
|
-function start(){
|
|
|
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
|
- $goNum = 0;
|
|
|
26
|
- // 循环阻塞
|
|
|
27
|
- while (true){
|
13
|
+ if($goNum>=200){
|
|
|
14
|
+ break;
|
|
|
15
|
+ }
|
28
|
|
16
|
|
29
|
- // 需要同步的id
|
|
|
30
|
- $id = redis()->lPop('sync_email_lists');
|
17
|
+ // 需要同步的id
|
|
|
18
|
+ $id = redis()->lPop('sync_email_lists');
|
|
|
19
|
+
|
|
|
20
|
+ if($id && is_numeric($id)){
|
|
|
21
|
+ // 占用当前的id,占用2小时
|
|
|
22
|
+ if(redis()->add('just_sync_'.$id,time(),600)){
|
|
|
23
|
+ // 启动一个协程
|
|
|
24
|
+ go(function () use ($id,&$goNum){
|
|
|
25
|
+ $goNum++;
|
|
|
26
|
+ try{
|
|
|
27
|
+ // 开始同步
|
|
|
28
|
+ (new \Service\SyncMail($id))->sync();
|
|
|
29
|
+ }catch (Throwable $e){
|
|
|
30
|
+ logs('sync : '.$e->getMessage());
|
|
|
31
|
+ }
|
31
|
|
32
|
|
32
|
- if($id && is_numeric($id)){
|
|
|
33
|
- // 占用当前的id,占用2小时
|
|
|
34
|
- if(redis()->add('just_sync_'.$id,time(),600)){
|
|
|
35
|
- // 启动一个协程
|
|
|
36
|
- go(function () use ($id,&$goNum){
|
|
|
37
|
- $goNum++;
|
|
|
38
|
- try{
|
|
|
39
|
- // 开始同步
|
|
|
40
|
- (new \Service\SyncMail($id))->sync();
|
|
|
41
|
- }catch (Throwable $e){
|
|
|
42
|
- logs('sync : '.$e->getMessage());
|
|
|
43
|
- }
|
|
|
44
|
|
33
|
|
|
|
34
|
+ // 协程完成后执行的函数
|
|
|
35
|
+ co::defer(function () use ($id,&$goNum){
|
|
|
36
|
+ $goNum--;
|
|
|
37
|
+ // 30秒后 消除占用
|
|
|
38
|
+ redis()->expire('just_sync_'.$id,60);
|
|
|
39
|
+ // 写入日志
|
|
|
40
|
+ \Lib\Log::getInstance()->write();
|
|
|
41
|
+ });
|
45
|
|
42
|
|
46
|
- // 协程完成后执行的函数
|
|
|
47
|
- co::defer(function () use ($id,&$goNum){
|
|
|
48
|
- $goNum--;
|
|
|
49
|
- // 30秒后 消除占用
|
|
|
50
|
- redis()->expire('just_sync_'.$id,60);
|
|
|
51
|
- // 写入日志
|
|
|
52
|
- \Lib\Log::getInstance()->write();
|
|
|
53
|
- });
|
43
|
+ });
|
54
|
|
44
|
|
55
|
- });
|
|
|
56
|
- while ($goNum == 0){
|
|
|
57
|
- co::sleep(0.5);
|
|
|
58
|
- break;
|
|
|
59
|
- }
|
|
|
60
|
- }
|
|
|
61
|
- }else{
|
|
|
62
|
- co::sleep(1);
|
|
|
63
|
}
|
45
|
}
|
64
|
- //每次都暂停1秒,防止同一时间启动太多的任务
|
|
|
65
|
- co::sleep(0.5);
|
46
|
+ }else{
|
|
|
47
|
+ break;
|
66
|
}
|
48
|
}
|
|
|
49
|
+ }
|
67
|
|
50
|
|
68
|
- },true);
|
|
|
69
|
-
|
|
|
70
|
-
|
|
|
71
|
- // 启动管理器
|
|
|
72
|
- $pm->start();
|
|
|
73
|
-
|
|
|
74
|
-}
|
|
|
75
|
-
|
|
|
76
|
-
|
51
|
+ while ($goNum == 0){
|
|
|
52
|
+ co::sleep(0.5);
|
|
|
53
|
+ break;
|
|
|
54
|
+ }
|
|
|
55
|
+});
|
77
|
|
56
|
|
78
|
|
57
|
|
79
|
-start();
|
|
|
80
|
|
58
|
|
81
|
|
59
|
|
82
|
|
60
|
|