作者 邓超

x

@@ -29,11 +29,18 @@ function start(){ @@ -29,11 +29,18 @@ function start(){
29 $data['maildata'] = json_decode($data['maildata'],true); 29 $data['maildata'] = json_decode($data['maildata'],true);
30 // 查询邮箱 30 // 查询邮箱
31 $email = db()->first(\Model\emailSql::first($data['email_id'])); 31 $email = db()->first(\Model\emailSql::first($data['email_id']));
  32 + // 更新状态
  33 + \Model\sendJobsSql::upStatus($data['id'],1,db());
32 // 是否是单发送 34 // 是否是单发送
33 if($data['maildata']['massSuit']??0){ 35 if($data['maildata']['massSuit']??0){
34 $tos = $data['maildata']['tos']; 36 $tos = $data['maildata']['tos'];
35 foreach ($tos as $to){ 37 foreach ($tos as $to){
36 38
  39 + // 是否暂停
  40 + if(db()->value(\Model\sendJobsSql::isStatus($data['id'])) === 3){
  41 + break;
  42 + }
  43 +
37 // 是否已发送过了 44 // 是否已发送过了
38 if(db()->count(\Model\sendJobStatusSql::count($data['id'],$to['email']))){ 45 if(db()->count(\Model\sendJobStatusSql::count($data['id'],$to['email']))){
39 continue; 46 continue;
@@ -56,30 +63,34 @@ function start(){ @@ -56,30 +63,34 @@ function start(){
56 if($data['maildata']['masssuit_interval_send']??[]){ 63 if($data['maildata']['masssuit_interval_send']??[]){
57 $time = rand($data['maildata']['masssuit_interval_send']['start'],$data['maildata']['masssuit_interval_send']['end']); 64 $time = rand($data['maildata']['masssuit_interval_send']['start'],$data['maildata']['masssuit_interval_send']['end']);
58 if($time){ 65 if($time){
  66 + $block = false;
59 while (true){ 67 while (true){
60 // 没5秒循环一次 68 // 没5秒循环一次
61 if(redis()->get('send_job_is_stop')=='stop'){ 69 if(redis()->get('send_job_is_stop')=='stop'){
  70 + $block = true;
62 break; 71 break;
63 } 72 }
64 $time-=5; 73 $time-=5;
65 co::sleep(5); 74 co::sleep(5);
66 // 执行下一次了 75 // 执行下一次了
67 if (!$time){ 76 if (!$time){
  77 + $block = true;
68 break; 78 break;
69 } 79 }
70 } 80 }
  81 +
  82 + if($block){
  83 + break;
  84 + }
71 } 85 }
72 } 86 }
73 87
74 } 88 }
75 89
76 - // 更新状态  
77 - \Model\sendJobsSql::upStatus($data['id'],1,db());  
78 -  
79 }else{ 90 }else{
80 $result = \Lib\Mail\MailFun::sendEmail($data['maildata'],$email); 91 $result = \Lib\Mail\MailFun::sendEmail($data['maildata'],$email);
81 // 更新状态 92 // 更新状态
82 - \Model\sendJobsSql::upStatus($data['id'],1,db()); 93 + \Model\sendJobsSql::upStatus($data['id'],2,db());
83 // 插入紫薯精 94 // 插入紫薯精
84 db()->insert(\Model\sendJobStatusSql::$table,[ 95 db()->insert(\Model\sendJobStatusSql::$table,[
85 'job_id' => $data['id'], 96 'job_id' => $data['id'],
@@ -92,12 +103,23 @@ function start(){ @@ -92,12 +103,23 @@ function start(){
92 // 协程结束后 103 // 协程结束后
93 co::defer(function ($id) use(&$cNum,$data){ 104 co::defer(function ($id) use(&$cNum,$data){
94 $cNum--; 105 $cNum--;
  106 + // 验证是否完成
  107 + if($data['maildata']['massSuit']??0){
  108 + $total = db()->count(\Model\sendJobStatusSql::count($data['id']));
  109 +
  110 + // 更新状态
  111 + \Model\sendJobsSql::upStatus($data['id'],$total == $data['total'] ? 2 : 0,db());
  112 +
  113 + }
  114 +
95 // 写入日志 115 // 写入日志
96 \Lib\Log::getInstance()->write(); 116 \Lib\Log::getInstance()->write();
  117 +
97 // 结束后要关闭数据库链接,不然链接一直暂用 118 // 结束后要关闭数据库链接,不然链接一直暂用
98 db()->close(); 119 db()->close();
99 // 删除占用 120 // 删除占用
100 redis()->delete('send_job_run_id_'.$data['id']); 121 redis()->delete('send_job_run_id_'.$data['id']);
  122 +
101 redis()->close(); 123 redis()->close();
102 }); 124 });
103 125
  1 +<?php
  2 +
  3 +namespace Controller;
  4 +
  5 +use Lib\Mail\Mail;
  6 +use Model\emailSql;
  7 +use Model\sendJobsSql;
  8 +
  9 +
  10 +/**
  11 + * 邮件发送任务
  12 + * @author:dc
  13 + * @time 2023/4/17 15:56
  14 + * Class Job
  15 + * @package Controller
  16 + */
  17 +class Job extends Base {
  18 +
  19 +
  20 + /**
  21 + * 任务列表
  22 + * @author:dc
  23 + * @time 2023/4/17 15:57
  24 + */
  25 + public function index(){
  26 +
  27 + $lists = db()->all(sendJobsSql::all(dbWhere([
  28 + 'email_id' => $this->getEmails('id')
  29 + ])));
  30 +
  31 + return $lists;
  32 + }
  33 +
  34 +
  35 +
  36 +
  37 +
  38 +
  39 +
  40 +}
  41 +
  42 +
  43 +
  44 +
  45 +
  46 +
  47 +
  48 +
  49 +
  50 +
  51 +
  52 +
  53 +
  54 +
@@ -26,8 +26,8 @@ class sendJobStatusSql { @@ -26,8 +26,8 @@ class sendJobStatusSql {
26 * @author:dc 26 * @author:dc
27 * @time 2023/4/11 16:13 27 * @time 2023/4/11 16:13
28 */ 28 */
29 - public static function count(int $job_id, string $to):string {  
30 - return "select count(*) from `".self::$table."` where `job_id` = {$job_id} and `to_email` = '{$to}'"; 29 + public static function count(int $job_id, string $to=''):string {
  30 + return "select count(*) from `".self::$table."` where `job_id` = {$job_id} ".($to ? "and `to_email` = '{$to}'" : '');
31 } 31 }
32 32
33 33
@@ -31,6 +31,31 @@ class sendJobsSql { @@ -31,6 +31,31 @@ class sendJobsSql {
31 31
32 32
33 /** 33 /**
  34 + * 列表
  35 + * @param string $where
  36 + * @param int $p
  37 + * @param int $limit
  38 + * @return string
  39 + * @author:dc
  40 + * @time 2023/4/17 15:59
  41 + */
  42 + public static function all(string $where,int $p=1,int $limit=20):string {
  43 + $filed = '`id`,`success`,`title`,`created_at`,`error`,`total`,`stop`,`status`,`send_time`,`remark`';
  44 + return "select {$filed} from `".static::$table."` where ".$where." limit {$limit} offset ".(($p-1)*$limit);
  45 + }
  46 +
  47 + /**
  48 + * 某个任务是否暂停了
  49 + * @param int $id
  50 + * @return bool
  51 + * @author:dc
  52 + * @time 2023/4/17 16:19
  53 + */
  54 + public static function isStatus(int $id):bool {
  55 + return "select `status` from `".static::$table."` where `id` = {$id}";
  56 + }
  57 +
  58 + /**
34 * 更新状态 59 * 更新状态
35 * @param int $id 60 * @param int $id
36 * @param int $status 61 * @param int $status
@@ -33,8 +33,10 @@ return [ @@ -33,8 +33,10 @@ return [
33 // 邮件移动文件夹 33 // 邮件移动文件夹
34 'move' => [\Controller\Home::class, 'move'], 34 'move' => [\Controller\Home::class, 'move'],
35 // 检查邮箱状态 35 // 检查邮箱状态
36 - 'check' => [\Controller\Home::class, 'check'] 36 + 'check' => [\Controller\Home::class, 'check'],
37 37
  38 + // 发送任务
  39 + 'job' => [\Controller\Job::class, 'index'],
38 40
39 41
40 42