sync.php
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
//error_reporting();
use Swlib\SaberGM;
use Swoole\Process;
// 表示同步ai邮件脚本
// 这个的作用主要用于 凡ai邮箱收件箱邮件,同步回来都是未读,不读取远程的
define('CLI_AI_SYNC_START', 1);
function start(){
// 删除停止运行的值
// redis()->delete(SYNC_RUNNING_REDIS_KEY,'email_sync_stop_num');
// 进程管理器
$pm = new Process\Manager();
// 启动业务进程
$pm->addBatch(11,function (Process\Pool $pool, int $worker_id){
if($worker_id===0){
$i=43200;
while($i>0){
$i--;
co::sleep(1);
}
$pool->shutdown();
return 0;
}
include_once __DIR__."/../vendor/autoload.php";
_echo("业务进程({$worker_id})启动成功");
\Lib\DbPool::$clientNumber = 55;
\Lib\RedisPool::$clientNumber = 55;
// 循环阻塞
while (true){
while (\Lib\SwGo::$runNumber > 50){
co::sleep(1);
continue;
}
if(redis()->add('mailbox_status_setlock',1,30)){
$data = [];
foreach (range(1,100) as $v){
$data[$v] = redis()->lPop('fob_mailbox_status_set');
if(!$data[$v]){ unset($data[$v]); break;}
}
SaberGM::post(SUPER_DATA2_HOST . 'api/mailbox_status_sets', [
'data' => $data
]);
}
// 需要同步的id
$id = redis()->lPop('sync_email_lists');
if($id && is_numeric($id)){
// 占用当前的id,占用2小时
if(redis()->add('just_sync_'.$id,time(),600)){
\Lib\SwGo::start(function ($id){
try{
// 开始同步
$email = db()->cache(300)->first(\Model\emailSql::first($id));
if($email){
$sync = new \Service\SyncMail($email);
// 这些文件夹不同步 谷歌的所有文件夹
$sync->setNoSyncFolder([
'[Gmail]/&UWiQ6JD1TvY-','[Gmail]/All Mail','[Gmail]/&YkBnCZCuTvY-'
]);
// ai邮件只同步2天内的
$sync->search(
(new \Lib\Imap\ImapSearch())
->dateGt(date('Y-m-d',strtotime("-1 day")))
);
// 不同步文件夹
if(!redis()->add('ai_sync_email_no_sync_folder:'.$email['id'],1,rand(3600,86400))){
$sync->noSyncFolder();
}
$sync->isUidAfter(2)->sync();
$sync = null;
unset($sync);
}
}catch (Throwable $e){
if(!strpos($e->getMessage(),'ead time out') && !strpos($e->getMessage(),'NS Lookup resolve failed') && !strpos($e->getMessage(),'onnection timed out')){
logs('sync : '.($email['email']??'').' '.$e->getMessage());
}
}
// 30秒后 消除占用
redis()->expire('just_sync_'.$id,120);
},$id);
}
}
//每次都暂停1秒,防止同一时间启动太多的任务
co::sleep(0.2);
}
return 0;
},true);
// 启动管理器
$pm->start();
}
start();