From f0e2a6610ed7d174e57dfab5bdedce29b63da7f5 Mon Sep 17 00:00:00 2001 From: chengxl Date: Wed, 23 Jul 2025 22:12:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E7=94=9F=E6=88=90=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common/QueueRedis.php | 52 ++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/application/common/QueueRedis.php b/application/common/QueueRedis.php index c6b05e3..c28faf7 100644 --- a/application/common/QueueRedis.php +++ b/application/common/QueueRedis.php @@ -158,6 +158,56 @@ LUA; return false; } } + // 多问题按条件拆分成两个队列新增日志记录 + public function recordQuestionProcessingStart($key, $totalQuestions) + { + try { + $redis = $this->connect(); + $redis->hMSet($key, [ + 'status' => 'processing', + 'total' => $totalQuestions, + 'completed' => 0, + 'start_time' => time(), + 'queue_1_completed' => 0, + 'queue_2_completed' => 0 + ]); + $redis->expire($key, 10800); // 6小时过期 + return true; + } catch (\Exception $e) { + return false; + } + } + // 多问题按条件拆分成两个队列更新日志记录 + public function updateQuestionProcessingProgress($key, $sKeyName) + { + $redis = $this->connect(); + // 获取总数 + $total = $redis->hGet($key, 'total'); + if (!$total) { + return 0; + } + if(!empty($sKeyName)){ + $redis->hIncrBy($key, $sKeyName, 1); + } + //获取每个队列完成数量 + $queue_1_completed = $redis->hGet($key, 'queue_1_completed'); + $queue_2_completed = $redis->hGet($key, 'queue_2_completed'); + $completed = $queue_1_completed + $queue_2_completed; + if($completed > $total){ + return 100; + } + // 计算进度 + $iProgress = round(($completed / $total) * 100, 2); + // 事务更新多个字段 + $redis->hSet($key, 'completed', $completed); + $redis->hSet($key, 'progress', $iProgress); + if ($iProgress >= 100) { + $redis->hSet($key, 'status', 'completed'); + $redis->hSet($key, 'end_time', time()); + } + return $iProgress; + + } // 更新处理进度 public function updateProcessingProgress($key, $completed) @@ -166,7 +216,7 @@ LUA; // 获取总数 $total = $redis->hGet($key, 'total'); if (!$total) { - return false; + return 0; } // 计算进度