作者 邓超

to es

@@ -26,14 +26,40 @@ class SyncToEsCmd { @@ -26,14 +26,40 @@ class SyncToEsCmd {
26 */ 26 */
27 public $fob_db; 27 public $fob_db;
28 28
  29 + /**
  30 + * @var \Lib\Db
  31 + */
  32 + public $db;
  33 +
  34 +
  35 + public $startTime = 0;
  36 +
  37 + /**
  38 + * SyncToEsCmd constructor.
  39 + */
  40 + public function __construct()
  41 + {
  42 + $this->db = db();
  43 + $this->fob_db = fob_mysql();
  44 +
  45 + $handler = function ($signal){
  46 + _echo('收到进程信号 '. $signal);
  47 + // 可以处理其他程序
  48 + $this->isStop = true;
  49 + };
  50 + pcntl_signal(SIGTERM, $handler); // 这个是kill
  51 + pcntl_signal(SIGINT, $handler); // 这个是 ctrl+c
  52 +
  53 + $this->startTime = time();
  54 + }
29 55
30 /** 56 /**
31 * 是否是自动回复 57 * 是否是自动回复
32 * @author:dc 58 * @author:dc
33 * @time 2025/6/6 11:07 59 * @time 2025/6/6 11:07
34 */ 60 */
35 - public function isAuto($db,$data){  
36 - $is_auto = $db ->count('select count(*) from `lists_auto` where `list_id` = '.$data['id']) ? 1 : 0; 61 + public function isAuto($data){
  62 + $is_auto = $this->db->count('select count(*) from `lists_auto` where `list_id` = '.$data['id']) ? 1 : 0;
37 63
38 if($is_auto) return 1; 64 if($is_auto) return 1;
39 65
@@ -54,43 +80,36 @@ class SyncToEsCmd { @@ -54,43 +80,36 @@ class SyncToEsCmd {
54 return 0; 80 return 0;
55 } 81 }
56 82
57 - public function handler(){ 83 + /**
  84 + * @return bool
  85 + */
  86 + public function isStop(): bool
  87 + {
  88 + // 检查是否接收到信号
  89 + pcntl_signal_dispatch();
58 90
59 - $handler = function ($signal){  
60 - _echo('收到进程信号 '. $signal);  
61 - // 可以处理其他程序  
62 - $this->isStop = true;  
63 - };  
64 - pcntl_signal(SIGTERM, $handler); // 这个是kill  
65 - pcntl_signal(SIGINT, $handler); // 这个是 ctrl+c  
66 -// pcntl_signal(SIGHUP, $handler); 91 + // 是否超过来最大执行时间
  92 + if(time()-43200 > $this->startTime){
  93 + return true;
  94 + }
67 95
68 - $es = es(); // 第一个库,即将丢弃  
69 - $es2 = es('email_lists'); // 第二个库 新 96 + return $this->isStop;
  97 + }
70 98
71 - $db = db();  
72 - $this->fob_db = fob_mysql();  
73 - $startTime = time();  
74 - while (1){  
75 99
76 - if(time()-43200 > $startTime){  
77 - break;  
78 - } 100 + public function handler(){
79 101
80 - // 检查是否接收到信号  
81 - pcntl_signal_dispatch(); 102 + $es = es(); // 第一个库,即将丢弃
  103 + $es2 = es('email_lists'); // 第二个库 新
82 104
83 - if($this->isStop) {  
84 - _echo('已退出进程');  
85 - break;  
86 - } 105 + while (!$this->isStop()){
87 106
88 $id = redis()->lPop('sync_to_es'); 107 $id = redis()->lPop('sync_to_es');
89 $code = 500; 108 $code = 500;
90 if($id){ 109 if($id){
91 $doc_id = ''; 110 $doc_id = '';
92 try { 111 try {
93 - $data = $db->throw()->first(\Model\listsSql::first('`id` = '.$id)); 112 + $data = $this->db->throw()->first(\Model\listsSql::first('`id` = '.$id));
94 }catch (Throwable $e){ 113 }catch (Throwable $e){
95 redis()->rPush('sync_to_es',$id); 114 redis()->rPush('sync_to_es',$id);
96 _echo('sync to es '.$e->getMessage()); 115 _echo('sync to es '.$e->getMessage());
@@ -101,7 +120,7 @@ class SyncToEsCmd { @@ -101,7 +120,7 @@ class SyncToEsCmd {
101 try { 120 try {
102 // 文件夹 121 // 文件夹
103 if(empty($this->folders[$data['folder_id']])){ 122 if(empty($this->folders[$data['folder_id']])){
104 - $this->folders[$data['folder_id']] = $db->throw()->value(\Model\folderSql::first($data['folder_id'],'folder')); 123 + $this->folders[$data['folder_id']] = $this->db->throw()->value(\Model\folderSql::first($data['folder_id'],'folder'));
105 } 124 }
106 125
107 // 为文件夹打标 方便查询 126 // 为文件夹打标 方便查询
@@ -109,7 +128,7 @@ class SyncToEsCmd { @@ -109,7 +128,7 @@ class SyncToEsCmd {
109 128
110 // 是否是自动回复 129 // 是否是自动回复
111 if($data['folder_as_int'] === 1){ 130 if($data['folder_as_int'] === 1){
112 - $data['is_auto'] = $this->isAuto($db,$data); 131 + $data['is_auto'] = $this->isAuto($data);
113 } 132 }
114 133
115 // postid ai邮箱要用 这个是查询黑格 134 // postid ai邮箱要用 这个是查询黑格
@@ -144,7 +163,6 @@ class SyncToEsCmd { @@ -144,7 +163,6 @@ class SyncToEsCmd {
144 163
145 } 164 }
146 165
147 - protected $postids = [];  
148 166
149 /** 167 /**
150 * 项目id 168 * 项目id