From 27deb4be75a6a9f55e6ba26d4dbd663a8068d2cf Mon Sep 17 00:00:00 2001 From: yaosen <741606767@qq.com> Date: Tue, 2 Jul 2024 09:55:32 +0800 Subject: [PATCH] a --- admin/src/views/qa/city.vue | 2 +- service/app/server/Douyin.php | 72 ++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/admin/src/views/qa/city.vue b/admin/src/views/qa/city.vue index 0bb8e819..3b79f390 100644 --- a/admin/src/views/qa/city.vue +++ b/admin/src/views/qa/city.vue @@ -15,7 +15,7 @@ - + diff --git a/service/app/server/Douyin.php b/service/app/server/Douyin.php index 1623e40d..abf02f15 100644 --- a/service/app/server/Douyin.php +++ b/service/app/server/Douyin.php @@ -266,7 +266,7 @@ class Douyin return true; } - public function _killMiddleProcesses($pattern, $maxProcesses) + /*public function _killMiddleProcesses($pattern, $maxProcesses) { // 检查操作系统是否为 Linux if (PHP_OS !== 'Linux') { @@ -297,6 +297,7 @@ class Douyin $minutes = intval($runtimeParts[1]); $totalMinutes = $hours * 60 + $minutes; + if ($pid !== $currentPid) { exec("kill -9 $pid"); $processesKilled++; @@ -315,9 +316,78 @@ class Douyin } else { echo "Process count is not over the limit.\n"; } + return true; + }*/ + + public function _killMiddleProcesses($pattern, $maxProcesses) + { + // 检查操作系统是否为 Linux + if (PHP_OS !== 'Linux') { + echo "当前系统不是 Linux,跳过执行。\n"; + return false; + } + + // 获取当前进程的 PID + $currentPid = getmypid(); + + // 获取进程列表 + $command = "ps aux | grep '$pattern' | grep -v grep | grep -v '$currentPid' | awk '{print $2,$10}'"; + $output = []; + exec($command, $output); + + $processList = []; + foreach ($output as $line) { + list($pid, $runtime) = explode(' ', $line); + $runtimeParts = explode(':', $runtime); + $hours = intval($runtimeParts[0]); + $minutes = intval($runtimeParts[1]); + $totalMinutes = $hours * 60 + $minutes; + $processList[] = ['pid' => $pid, 'runtime' => $totalMinutes]; + } + + // 按运行时间排序 + usort($processList, function ($a, $b) { + return $a['runtime'] <=> $b['runtime']; + }); + + // 过滤掉当前 PID 和运行时间最短的 PID + if (!empty($processList)) { + array_shift($processList); // 移除运行时间最短的 + } + $processList = array_filter($processList, function ($process) use ($currentPid) { + return $process['pid'] != $currentPid; + }); + + $processCount = count($processList); + + // 如果进程数量超过最大值,则终止中间部分的进程 + if ($processCount > $maxProcesses) { + $processesToKill = $processCount - $maxProcesses; + $processesKilled = 0; + + // 杀死运行时间最短的进程 + foreach ($processList as $process) { + if ($processesKilled >= $processesToKill) { + break; + } + + exec("kill -9 {$process['pid']}"); + $processesKilled++; + + echo "Killed process with PID: {$process['pid']}\n"; + } + + if ($processesKilled === 0) { + echo "No processes to kill.\n"; + } + } else { + echo "Process count is not over the limit.\n"; + } + return true; } + public function _orderDetail($orderId = null, $status = null) { $orderDetail = $this->_curl("/life/trip/v2/travel_agency/book/detail", [