From 297e001f14a8bb0ee4e24de8f20232426e32970e Mon Sep 17 00:00:00 2001 From: luhaoling <2198702716@qq.com> Date: Mon, 19 Feb 2024 15:53:02 +0800 Subject: [PATCH] fix: fix the make stop script --- cmd/openim-api/main.go | 1 + internal/msggateway/init.go | 2 + pkg/common/startrpc/start.go | 1 + scripts/check-all-by-signal.sh | 89 ++++++++++++++++++++++++++++++++++ scripts/lib/util.sh | 71 +++++++++++++++++++++++++++ scripts/stop-all.sh | 10 ++-- 6 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 scripts/check-all-by-signal.sh diff --git a/cmd/openim-api/main.go b/cmd/openim-api/main.go index 04a0f13df..7a7e06293 100644 --- a/cmd/openim-api/main.go +++ b/cmd/openim-api/main.go @@ -119,6 +119,7 @@ func run(port int, proPort int) error { defer cancel() select { case <-sigs: + util.SIGUSR1Exit() err := server.Shutdown(ctx) if err != nil { return errs.Wrap(err, "shutdown err") diff --git a/internal/msggateway/init.go b/internal/msggateway/init.go index b18efcd50..8ea648af8 100644 --- a/internal/msggateway/init.go +++ b/internal/msggateway/init.go @@ -46,7 +46,9 @@ func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error { netDone := make(chan error) go func() { err = hubServer.Start() + //if err != nil { netDone <- err + //} }() return hubServer.LongConnServer.Run(netDone) } diff --git a/pkg/common/startrpc/start.go b/pkg/common/startrpc/start.go index 8f77ce841..3d13240e0 100644 --- a/pkg/common/startrpc/start.go +++ b/pkg/common/startrpc/start.go @@ -149,6 +149,7 @@ func Start( if err != nil { return errs.Wrap(err, "shutdown err") } + //return errs.Wrap(errors.New("SIGTERM EXIT")) case <-netDone: close(netDone) return netErr diff --git a/scripts/check-all-by-signal.sh b/scripts/check-all-by-signal.sh new file mode 100644 index 000000000..e23906fc2 --- /dev/null +++ b/scripts/check-all-by-signal.sh @@ -0,0 +1,89 @@ +#!/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" + +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 + +openim::log::info "\n## Check all OpenIM service ports" +echo "+++ The port being checked: ${OPENIM_SERVER_PORT_LISTARIES[@]}" +openim::util::check_ports ${OPENIM_SERVER_PORT_LISTARIES[@]} +if [[ $? -ne 0 ]]; then + echo "+++ cat openim log file >>> ${LOG_FILE}" + echo "++++ All openim service ports stop successfully !" +else + openim::log::error_exit "The service does not stop properly, please check the port, query variable definition!" +fi + +set -e + +trap - ERR \ No newline at end of file diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 1bdb7f640..8d97d6d9b 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -519,6 +519,35 @@ openim::util::stop_services_on_ports() { return 0 fi } + +openim::util::stop_services_by_signal() { + # An array to collect ports of processes that couldn't be stopped. + local not_stopped=() + + # An array to collect information about processes that were stopped. + local stopped=() + + # 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. + if [[ -n $info ]]; then + # Extract the Process ID. + 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) + fi + done <<< "$info" + fi + done +} # nc -l -p 12345 # nc -l -p 123456 # ps -ef | grep "nc -l" @@ -594,6 +623,48 @@ openim::util::stop_services_with_name() { openim::log::success "All specified services were stopped." echo "" } + +openim::util::stop_services_by_name_signal() { + # An array to collect names of processes that couldn't be stopped. + local not_stopped=() + + # An array to collect information about processes that were stopped. + local stopped=() + + # Iterate over each given service name. + for server_name in "$@"; do + # Use the `pgrep` command to find process IDs related to the given service name. + local pids=$(pgrep -f "$server_name") + + # If no process was found with the name, add it to the not_stopped list + if [[ -z $pids ]]; then + not_stopped+=("$server_name") + continue + fi + local stopped_this_time=false + for pid in $pids; do + + # Exclude the PID of the current script + if [[ "$pid" == "$$" ]]; then + continue + fi + + # If there's a Process ID, it means the service with the name is running. + if [[ -n $pid ]]; then + # Try to stop the service by killing its process. + if kill -15 $pid 2>/dev/null; then + stopped_this_time=true + fi + fi + done + + if $stopped_this_time; then + stopped+=("$server_name") + else + not_stopped+=("$server_name") + fi + done +} # sleep 333333& # sleep 444444& # ps -ef | grep "sleep" diff --git a/scripts/stop-all.sh b/scripts/stop-all.sh index 2acb9cdc5..3f27820b9 100755 --- a/scripts/stop-all.sh +++ b/scripts/stop-all.sh @@ -28,12 +28,14 @@ source "${OPENIM_ROOT}/scripts/install/common.sh" openim::log::info "\n# Begin to stop all openim service" -echo "++ Ready to stop port: ${OPENIM_SERVER_PORT_LISTARIES[@]}" +echo "++ Ready to stop port: ${OPNIM_SERVER_PORT_LISTARIES[@]}" -openim::util::stop_services_on_ports ${OPENIM_SERVER_PORT_LISTARIES[@]} +openim::util::stop_services_by_signal ${OPENIM_SERVER_PORT_LISTARIES[@]} -echo -e "\n++ Stop all processes in the path ${OPENIM_OUTPUT_HOSTBIN}" +openim::util::stop_services_by_name_signal "${OPENIM_OUTPUT_HOSTBIN}" -openim::util::stop_services_with_name "${OPENIM_OUTPUT_HOSTBIN}" +echo "++ Check if the services have been stopped" + +source "${OPENIM_ROOT}/scripts/check-all-by-signal.sh" openim::log::success "✨ All processes to be killed" \ No newline at end of file