作者 邓超

x

... ... @@ -29,11 +29,18 @@ function start(){
$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){
// 是否暂停
if(db()->value(\Model\sendJobsSql::isStatus($data['id'])) === 3){
break;
}
// 是否已发送过了
if(db()->count(\Model\sendJobStatusSql::count($data['id'],$to['email']))){
continue;
... ... @@ -56,30 +63,34 @@ function start(){
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(redis()->get('send_job_is_stop')=='stop'){
$block = true;
break;
}
$time-=5;
co::sleep(5);
// 执行下一次了
if (!$time){
$block = true;
break;
}
}
if($block){
break;
}
}
}
}
// 更新状态
\Model\sendJobsSql::upStatus($data['id'],1,db());
}else{
$result = \Lib\Mail\MailFun::sendEmail($data['maildata'],$email);
// 更新状态
\Model\sendJobsSql::upStatus($data['id'],1,db());
\Model\sendJobsSql::upStatus($data['id'],2,db());
// 插入紫薯精
db()->insert(\Model\sendJobStatusSql::$table,[
'job_id' => $data['id'],
... ... @@ -92,12 +103,23 @@ function start(){
// 协程结束后
co::defer(function ($id) use(&$cNum,$data){
$cNum--;
// 验证是否完成
if($data['maildata']['massSuit']??0){
$total = db()->count(\Model\sendJobStatusSql::count($data['id']));
// 更新状态
\Model\sendJobsSql::upStatus($data['id'],$total == $data['total'] ? 2 : 0,db());
}
// 写入日志
\Lib\Log::getInstance()->write();
// 结束后要关闭数据库链接,不然链接一直暂用
db()->close();
// 删除占用
redis()->delete('send_job_run_id_'.$data['id']);
redis()->close();
});
... ...
<?php
namespace Controller;
use Lib\Mail\Mail;
use Model\emailSql;
use Model\sendJobsSql;
/**
* 邮件发送任务
* @author:dc
* @time 2023/4/17 15:56
* Class Job
* @package Controller
*/
class Job extends Base {
/**
* 任务列表
* @author:dc
* @time 2023/4/17 15:57
*/
public function index(){
$lists = db()->all(sendJobsSql::all(dbWhere([
'email_id' => $this->getEmails('id')
])));
return $lists;
}
}
... ...
... ... @@ -26,8 +26,8 @@ class sendJobStatusSql {
* @author:dc
* @time 2023/4/11 16:13
*/
public static function count(int $job_id, string $to):string {
return "select count(*) from `".self::$table."` where `job_id` = {$job_id} and `to_email` = '{$to}'";
public static function count(int $job_id, string $to=''):string {
return "select count(*) from `".self::$table."` where `job_id` = {$job_id} ".($to ? "and `to_email` = '{$to}'" : '');
}
... ...
... ... @@ -31,6 +31,31 @@ class sendJobsSql {
/**
* 列表
* @param string $where
* @param int $p
* @param int $limit
* @return string
* @author:dc
* @time 2023/4/17 15:59
*/
public static function all(string $where,int $p=1,int $limit=20):string {
$filed = '`id`,`success`,`title`,`created_at`,`error`,`total`,`stop`,`status`,`send_time`,`remark`';
return "select {$filed} from `".static::$table."` where ".$where." limit {$limit} offset ".(($p-1)*$limit);
}
/**
* 某个任务是否暂停了
* @param int $id
* @return bool
* @author:dc
* @time 2023/4/17 16:19
*/
public static function isStatus(int $id):bool {
return "select `status` from `".static::$table."` where `id` = {$id}";
}
/**
* 更新状态
* @param int $id
* @param int $status
... ...
... ... @@ -33,8 +33,10 @@ return [
// 邮件移动文件夹
'move' => [\Controller\Home::class, 'move'],
// 检查邮箱状态
'check' => [\Controller\Home::class, 'check']
'check' => [\Controller\Home::class, 'check'],
// 发送任务
'job' => [\Controller\Job::class, 'index'],
... ...