From 5888cf701529727143241f251d035ed067e58458 Mon Sep 17 00:00:00 2001 From: luhaoling <2198702716@qq.com> Date: Tue, 20 Feb 2024 17:25:36 +0800 Subject: [PATCH] fix: del the unuse func --- scripts/check-all-by-signal.sh | 2 +- scripts/check-all-by-signal1.sh | 106 ------------------------- scripts/install/openim-msgtransfer.sh | 29 +------ scripts/lib/util.sh | 107 +++----------------------- scripts/stop-all1.sh | 39 ---------- 5 files changed, 15 insertions(+), 268 deletions(-) delete mode 100644 scripts/check-all-by-signal1.sh delete mode 100644 scripts/stop-all1.sh diff --git a/scripts/check-all-by-signal.sh b/scripts/check-all-by-signal.sh index 06572ef20..5bc7d25dd 100644 --- a/scripts/check-all-by-signal.sh +++ b/scripts/check-all-by-signal.sh @@ -38,7 +38,7 @@ handle_error() { trap handle_error ERR . $(dirname ${BASH_SOURCE})/install/openim-msgtransfer.sh openim::msgtransfer::check_by_signal - +echo "Check ports:" openim::util::check_ports_by_signal ${OPENIM_SERVER_PORT_LISTARIES[@]} if [[ $? -ne 0 ]]; then openim::log::error "The service does not stop properly, there are still processes running, please check!" diff --git a/scripts/check-all-by-signal1.sh b/scripts/check-all-by-signal1.sh deleted file mode 100644 index 7db4e22ff..000000000 --- a/scripts/check-all-by-signal1.sh +++ /dev/null @@ -1,106 +0,0 @@ -#!/usr/bin/env bash -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# This script is check openim service is running normally -# -# Usage: `scripts/check-all.sh`. -# Encapsulated as: `make check`. -# READ: https://github.com/openimsdk/open-im-server/tree/main/scripts/install/environment.sh - -set -o errexit -set -o nounset -set -o pipefail - -OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. -source "${OPENIM_ROOT}/scripts/install/common.sh" - -OPENIM_VERBOSE=4 - -openim::log::info "\n# Begin to check all openim service" - -openim::log::status "Check all dependent service ports" -# Elegant printing function -# Elegant printing function -print_services_and_ports() { - local service_names=("$@") - local half_length=$((${#service_names[@]} / 2)) - local service_ports=("${service_names[@]:half_length}") - - echo "+-------------------------+----------+" - echo "| Service Name | Port |" - echo "+-------------------------+----------+" - - for ((index=0; index < half_length; index++)); do - printf "| %-23s | %-8s |\n" "${service_names[$index]}" "${service_ports[$index]}" - done - - echo "+-------------------------+----------+" -} - -handle_error() { - echo "An error occurred. Printing ${STDERR_LOG_FILE} contents:" - cat "${STDERR_LOG_FILE}" - exit 1 -} - -trap handle_error ERR - -# Assuming OPENIM_SERVER_NAME_TARGETS and OPENIM_SERVER_PORT_TARGETS are defined -# Similarly for OPENIM_DEPENDENCY_TARGETS and OPENIM_DEPENDENCY_PORT_TARGETS - -# Print out services and their ports -print_services_and_ports "${OPENIM_SERVER_NAME_TARGETS[@]}" "${OPENIM_SERVER_PORT_TARGETS[@]}" - -# Print out dependencies and their ports -print_services_and_ports "${OPENIM_DEPENDENCY_TARGETS[@]}" "${OPENIM_DEPENDENCY_PORT_TARGETS[@]}" - -# OpenIM check -echo "++ The port being checked: ${OPENIM_SERVER_PORT_LISTARIES[@]}" -openim::log::info "\n## Check all dependent service ports" -echo "++ The port being checked: ${OPENIM_DEPENDENCY_PORT_LISTARIES[@]}" - -set +e - -# Later, after discarding Docker, the Docker keyword is unreliable, and Kubepods is used -if grep -qE 'docker|kubepods' /proc/1/cgroup || [ -f /.dockerenv ]; then - openim::color::echo ${COLOR_CYAN} "Environment in the interior of the container" -else - openim::color::echo ${COLOR_CYAN} "The environment is outside the container" - openim::util::check_ports ${OPENIM_DEPENDENCY_PORT_LISTARIES[@]} || return 0 -fi - -if [[ $? -ne 0 ]]; then - openim::log::error_exit "The service does not start properly, please check the port, query variable definition!" - echo "+++ https://github.com/openimsdk/open-im-server/tree/main/scripts/install/environment.sh +++" -else - echo "++++ Check all dependent service ports successfully !" -fi - -openim::log::info "\n## Check OpenIM service name" -. $(dirname ${BASH_SOURCE})/install/openim-msgtransfer.sh openim::msgtransfer::check_by_signal1 - -openim::log::info "\n## Check all OpenIM service ports" -echo "+++ The port being checked: ${OPENIM_SERVER_PORT_LISTARIES[@]}" -openim::util::check_ports_by_signal1 ${OPENIM_SERVER_PORT_LISTARIES[@]} -if [[ $? -ne 0 ]]; then - echo "++++ All openim service ports stop successfully !" -else - echo "+++ cat openim log file >>> ${LOG_FILE}" - openim::log::error_exit "The service does not stop properly, please check!" -fi - -set -e - -trap - ERR \ No newline at end of file diff --git a/scripts/install/openim-msgtransfer.sh b/scripts/install/openim-msgtransfer.sh index d9e112659..90789b621 100755 --- a/scripts/install/openim-msgtransfer.sh +++ b/scripts/install/openim-msgtransfer.sh @@ -93,34 +93,10 @@ function openim::msgtransfer::check_by_signal() { if [ "$NUM_PROCESSES" -gt 0 ]; then openim::log::error "Found $NUM_PROCESSES processes for $OPENIM_OUTPUT_HOSTBIN/openim-msgtransfer" - for PID in $PIDS; do - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - openim::log::error "$(ps -p $PID -o pid=,cmd= | awk '{print "PID: " $1 ", CMD: " $2}')" - elif [[ "$OSTYPE" == "darwin"* ]]; then - # 优化后的命令 - openim::log::error "$(ps -p $PID -o pid=,cmd= | awk '{print "PID: " $1 ", CMD: " $2}')" - else - openim::log::error "Unsupported OS type: $OSTYPE" - fi - done - openim::log::error "Processes have not been stopped properly." - else - openim::log::success "All openim-msgtransfer processes have been stopped properly." - fi -} - - -function openim::msgtransfer::check_by_signal1() { - PIDS=$(pgrep -f "${OPENIM_OUTPUT_HOSTBIN}/openim-msgtransfer") - - NUM_PROCESSES=$(echo "$PIDS" | wc -l) - - if [ "$NUM_PROCESSES" -eq "$OPENIM_MSGGATEWAY_NUM" ]; then - openim::log::info "Found $OPENIM_MSGGATEWAY_NUM processes named $OPENIM_OUTPUT_HOSTBIN" for PID in $PIDS; do if [[ "$OSTYPE" == "linux-gnu"* ]]; then ps -p $PID -o pid,cmd - elif [[ "$OSTYPE" == "darwin"* ]]; then + elif [[ "$OSTYPE" == "darwin"* ]]; then ps -p $PID -o pid,comm else openim::log::error "Unsupported OS type: $OSTYPE" @@ -128,8 +104,7 @@ function openim::msgtransfer::check_by_signal1() { done openim::log::error "Processes have not been stopped properly." else -# openim::log::error_exit "Expected $OPENIM_MSGGATEWAY_NUM openim msgtransfer processes, but found $NUM_PROCESSES msgtransfer processes." - openim::log::success "All openim-msgtransfer processes have been stopped properly." + openim::log::success "All openim-msgtransfer processes have been stopped properly." fi } diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 15ed9c5bb..d2ca8e865 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -447,87 +447,6 @@ openim::util::check_ports_by_signal() { fi } -openim::util::check_ports_by_signal1() { - # An array to collect ports of processes that are not running. - local not_started=() - - # An array to collect information about processes that are running. - local started=() - - openim::log::info "Checking ports: $*" - # Iterate over each given port. - for port in "$@"; do - # Initialize variables - # Check the OS and use the appropriate command - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - if command -v ss > /dev/null 2>&1; then - info=$(ss -ltnp | grep ":$port" || true) - else - info=$(netstat -ltnp | grep ":$port" || true) - fi - elif [[ "$OSTYPE" == "darwin"* ]]; then - # For macOS, use lsof - info=$(lsof -P -i:"$port" | grep "LISTEN" || true) - fi - - # Check if any process is using the port - if [[ -z $info ]]; then - not_started+=($port) - else - if [[ "$OSTYPE" == "linux-gnu"* ]]; then - # Extract relevant details for Linux: Process Name, PID, and FD. - details=$(echo $info | sed -n 's/.*users:(("\([^"]*\)",pid=\([^,]*\),fd=\([^)]*\))).*/\1 \2 \3/p') - command=$(echo $details | awk '{print $1}') - pid=$(echo $details | awk '{print $2}') - fd=$(echo $details | awk '{print $3}') - elif [[ "$OSTYPE" == "darwin"* ]]; then - # Handle extraction for macOS - pid=$(echo $info | awk '{print $2}' | cut -d'/' -f1) - command=$(ps -p $pid -o comm= | xargs basename) - fd=$(echo $info | awk '{print $4}' | cut -d'/' -f1) - fi - - # Get the start time of the process using the PID - if [[ -z $pid ]]; then - start_time="N/A" - else - start_time=$(ps -p $pid -o lstart=) - fi - - started+=("Port $port - Command: $command, PID: $pid, FD: $fd, Started: $start_time") - fi - done - - # Print information about ports whose processes are not running. - if [[ ${#not_started[@]} -ne 0 ]]; then - openim::log::info "\n### Not started ports:" - for port in "${not_started[@]}"; do - openim::log::error "Port $port is not started." - done - fi - - # Print information about ports whose processes are running. - if [[ ${#started[@]} -ne 0 ]]; then - openim::log::info "\n### Started ports:" - for info in "${started[@]}"; do - openim::log::info "$info" - done - fi - - # If any of the processes is not running, return a status of 1. - if [[ ${#not_started[@]} -ne 0 ]]; then - openim::log::success "All specified processes are stop." - return 1 - else - openim::color::echo $COLOR_RED " OpenIM Stdout Log >> cat ${LOG_FILE}" - openim::color::echo $COLOR_RED " OpenIM Stderr Log >> cat ${STDERR_LOG_FILE}" - cat "$TMP_LOG_FILE" | awk '{print "\033[31m" $0 "\033[0m"}' - openim::log::error "Have processes no stop." - return 0 - fi -} - - # set +o errexit # Sample call for testing: # openim::util::check_ports 10002 1004 12345 13306 @@ -634,24 +553,22 @@ openim::util::stop_services_on_ports() { openim::log::info "Stopping services on ports: $*" # Iterate over each given port. for port in "$@"; do - # Use the `lsof` command to find process information related to the given port. - info=$(lsof -i :$port -n -P | grep LISTEN || true) - - # If there's process information, it means the process associated with the port is running. + local info=$(lsof -i :$port -n -P | grep LISTEN || true) if [[ -n $info ]]; then - # Extract the Process ID. + local stopped_this_port=false while read -r line; do local pid=$(echo $line | awk '{print $2}') - - # Try to stop the service by killing its process. - if kill -15 $pid; then - stopped+=($port) - else - not_stopped+=($port) + if kill -15 "$pid" &> /dev/null; then + stopped+=("$port") + stopped_this_port=true + break # Jump out of loop after successfully sending SIGTERM fi - done <<< "$info" - fi - done + done <<< "$info" + if ! $stopped_this_port; then + not_stopped+=("$port") + fi + fi + done # Print information about ports whose processes couldn't be stopped. if [[ ${#not_stopped[@]} -ne 0 ]]; then diff --git a/scripts/stop-all1.sh b/scripts/stop-all1.sh deleted file mode 100644 index 9709b822f..000000000 --- a/scripts/stop-all1.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env bash -# Copyright © 2023 OpenIM. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# This script is stop all openim service -# -# Usage: `scripts/stop.sh`. -# Encapsulated as: `make stop`. - -set -o errexit -set -o nounset -set -o pipefail - -OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. - -source "${OPENIM_ROOT}/scripts/install/common.sh" - -openim::log::info "\n# Begin to stop all openim service" - -echo "++ Ready to stop port: ${OPNIM_SERVER_PORT_LISTARIES[@]}" - -openim::util::stop_services_on_ports ${OPENIM_SERVER_PORT_LISTARIES[@]} - -openim::util::stop_services_with_name "${OPENIM_OUTPUT_HOSTBIN}" - -echo "++ Check if the services have been stopped" - -openim::log::success "✨ All processes to be killed" \ No newline at end of file