send_job.php
8.2 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
include_once "../vendor/autoload.php";
// 这里试试不用多进程模式,用多协程模式
class SendJob {
public $cnum = 0;
/**
* 是否停止
* @return bool
* @author:dc
* @time 2024/4/10 9:12
*/
private function isStop(){
return redis()->get('send_job_is_stop') == 'stop';
}
/**
* 休眠
* @param float $sleep
* @return bool
* @author:dc
* @time 2024/4/10 9:12
*/
private function s_sleep(float $sleep):bool {
if($sleep > 0){
$t = microtime(1);
while (!$this->isStop()){
co::sleep(0.1);
if($sleep - (microtime(1)-$t) <= 0){
break;
}
}
}
return true;
}
public function start(){
_echo('启动邮件群发任务 '.getmypid());
// 删除key
redis()->delete('send_job_is_stop');
while (1){
// 是否要停止
if($this->isStop()){
break;
}
$lists = db()->all(\Model\sendJobsSql::sendList(500));
$lists = $lists?$lists:[];
if($lists){
foreach ($lists as $list){
$this->go_($list);
}
}else{
// 休眠30秒
$this->s_sleep(30);
}
}
// 这个是等待所有协程退出
while (true){
_echo('等待协程退出...');
if(!$this->cnum){
break;
}
co::sleep(1);
}
}
/**
* @param $list
* @throws \Lib\Err
* @throws \PHPMailer\PHPMailer\Exception
* @author:dc
* @time 2024/4/10 9:25
*/
public function go_($list){
// 占用 id
if(redis()->add('send_job_run_id_'.$list['id'],$list['id'],600)){
go(function ($data) {
_echo('正在执行任务 '.$data['id']);
$this->cnum++; // 协程数+1
// 表单数据
$data['maildata'] = json_decode($data['maildata'],true);
// 查询邮箱
$email = db()->first(\Model\emailSql::first($data['email_id']));
// 更新状态
\Model\sendJobsSql::upStatus($data['id'],1,db());
// 是否是单发送
if($data['maildata']['massSuit']??0){
$tos = $data['maildata']['tos'];
foreach ($tos as $to){
_echo('正在执行任务 发送邮件 '.$to['email']);
// 续时间
redis()->set('send_job_run_id_'.$data['id'],$data['id'],600);
// 是否暂停
$dst = db()->first(\Model\sendJobsSql::isStatus($data['id']));
if($dst && $dst['status'] === 3){
break;
}
// 是否已发送过了
if(db()->count(\Model\sendJobStatusSql::count($data['id'],$to['email']))){
continue;
}
// 每个收件人单独发送
$data['maildata']['tos'] = [$to];
//替换邮件内容中的指定字段为客户名字
$data['maildata']['body'] = str_replace('{customer_name}', $to['name'], $data['maildata']['body']);
$result = \Lib\Mail\MailFun::sendEmail($data['maildata'],$email);
// 插入紫薯精
db()->insert(\Model\sendJobStatusSql::$table,[
'job_id' => $data['id'],
'to_email' => $to['email'],
'status' => $result[0] ? 1 : 0,
'error' => $result[0] ? $result[1] : ''
]);
// 时间距离下次的时间
if($data['maildata']['masssuit_interval_send']??[]){
$time = rand($data['maildata']['masssuit_interval_send']['start'],$data['maildata']['masssuit_interval_send']['end']);
if($time){
$block = false;
while (true){
// 没5秒循环一次
if($this->isStop()){
$block = true;
break;
}
$time-=5;
$this->s_sleep(5);
// 执行下一次了
if (!$time){
$block = true;
break;
}
}
if($block){
break;
}
}
}
}
}
else{
$result = \Lib\Mail\MailFun::sendEmail($data['maildata'],$email);
// 更新状态
db()->update(\Model\sendJobsSql::$table,[
'status' => 2,
'success' => $result[0] ? $data['total'] : 0,
'error' => $result[0] ? 0 : $data['total'],
],dbWhere(['id'=>$data['id']]));
// 插入紫薯精
db()->insert(\Model\sendJobStatusSql::$table,[
'job_id' => $data['id'],
'to_email' => 'all',
'status' => $result[0] ? 1 : 0,
'error' => $result[0] ? $result[1] : ''
]);
}
// 协程结束后
co::defer(function ($id) use($data){
$this->cnum--;
// 验证是否完成
if($data['maildata']['massSuit']??0){
$total = db()->first(\Model\sendJobStatusSql::countSum($data['id']));
if($total){
// 更新状态
db()->update(\Model\sendJobsSql::$table,[
'status' => $total['t'] == $data['total'] ? 2 : 0,
'success' => $total['s'],
'error' => $total['e'],
],dbWhere(['id'=>$data['id']]));
}
}
// 写入日志
\Lib\Log::getInstance()->write();
// 结束后要关闭数据库链接,不然链接一直暂用
db()->close();
// 删除占用
redis()->delete('send_job_run_id_'.$data['id']);
redis()->close();
});
},$list);
}
}
}
$ps = "ps -ef | grep \"send_job.php start\" | grep -v grep | wc -l";
switch ($argv[1]??0){
case 'start':{
// 开启协程
\Co\run(function (){
$handler = function ($signal){
// 可以处理其他程序
redis()->set('send_job_is_stop','stop');
_echo('收到退出信号 '.$signal);
};
\Swoole\Process::signal(SIGTERM,$handler);
\Swoole\Process::signal(SIGINT,$handler);
(new SendJob)->start();
_echo('进程已退出');
});
break;
}
case 'stop':{
\Co\run(function ($ps){
echo "正在退出程序...\n非必要请不要强制kill掉进程\n";
redis()->set('send_job_is_stop','stop');
while (true){
$num = exec($ps);
if(!$num){
break;
}
co::sleep(0.2);
}
echo "已退出程序\n";
},$ps);
break;
}
default:{
break;
}
}