作者 邓超

1

... ... @@ -17,85 +17,93 @@ function start(){
break;
}
$lists = db()->all(\Model\sendJobsSql::sendList());
$lists = db()->all(\Model\sendJobsSql::sendList(500-$cNum));
// 循环
foreach ($lists as $list){
go(function ($data) use (&$cNum){
$cNum++; // 协程数+1
// 表单数据
$data['maildata'] = json_decode($data['maildata'],true);
// 查询邮箱
$email = db()->first(\Model\emailSql::first($data['email_id']));
// 是否是单发送
if($data['maildata']['massSuit']??0){
$tos = $data['maildata']['tos'];
foreach ($tos as $to){
// 是否已发送过了
if(db()->count(\Model\sendJobStatusSql::count($data['id'],$to['email']))){
continue;
// 占用 id
if(redis()->add('send_job_run_id_'.$list['id'],$list['id'])){
go(function ($data) use (&$cNum){
$cNum++; // 协程数+1
// 表单数据
$data['maildata'] = json_decode($data['maildata'],true);
// 查询邮箱
$email = db()->first(\Model\emailSql::first($data['email_id']));
// 是否是单发送
if($data['maildata']['massSuit']??0){
$tos = $data['maildata']['tos'];
foreach ($tos as $to){
// 是否已发送过了
if(db()->count(\Model\sendJobStatusSql::count($data['id'],$to['email']))){
continue;
}
// 每个收件人单独发送
$data['maildata']['tos'] = [$to];
$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){
while (true){
// 没5秒循环一次
if(redis()->get('send_job_is_stop')=='stop'){
break;
}
$time-=5;
co::sleep(5);
// 执行下一次了
if (!$time){
break;
}
}
}
}
}
// 每个收件人单独发送
$data['maildata']['tos'] = [$to];
$result = \Lib\Mail\MailFun::sendEmail($data['maildata'],$email);
// 更新状态
\Model\sendJobsSql::upStatus($data['id'],1,db());
}else{
$result = \Lib\Mail\MailFun::sendEmail($data['maildata'],$email);
// 更新状态
\Model\sendJobsSql::upStatus($data['id'],1,db());
// 插入紫薯精
db()->insert(\Model\sendJobStatusSql::$table,[
'job_id' => $data['id'],
'to_email' => $to['email'],
'to_email' => 'all',
'status' => $result[0] ? 1 : 0,
'error' => $result[0] ? $result[1] : ''
]);
}
// 协程结束后
co::defer(function ($id) use(&$cNum,$data){
$cNum--;
// 结束后要关闭数据库链接,不然链接一直暂用
db()->close();
// 删除占用
redis()->delete('send_job_run_id_'.$data['id']);
redis()->close();
});
// 时间距离下次的时间
if($data['maildata']['masssuit_interval_send']??[]){
$time = rand($data['maildata']['masssuit_interval_send']['start'],$data['maildata']['masssuit_interval_send']['end']);
if($time){
while (true){
// 没5秒循环一次
if(redis()->get('send_job_is_stop')=='stop'){
break;
}
$time-=5;
co::sleep(5);
// 执行下一次了
if (!$time){
break;
}
}
}
}
}
},$list);
}
// 更新状态
\Model\sendJobsSql::upStatus($data['id'],1,db());
}else{
$result = \Lib\Mail\MailFun::sendEmail($data['maildata'],$email);
// 更新状态
\Model\sendJobsSql::upStatus($data['id'],1,db());
// 插入紫薯精
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 () use(&$cNum){
$cNum--;
// 结束后要关闭数据库链接,不然链接一直暂用
db()->close();
redis()->close();
});
},$list);
}
}catch (Throwable $e){
logs($e->getMessage().$e->getTraceAsString());
}
... ...
... ... @@ -24,9 +24,9 @@ class sendJobsSql {
* @time 2023/4/11 14:56
* @return string
*/
public static function sendList():string {
public static function sendList($limit):string {
// 控制在500数量,协程数量就控制
return "select * from `".self::$table."` where `status` = 0 and `send_time` <= ".time()." limit 500";
return "select * from `".self::$table."` where `status` = 0 and `send_time` <= ".time()." limit {$limit}";
}
... ...