mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-12-03 02:42:19 +08:00
fix: fix the make stop script
This commit is contained in:
parent
f0aa9d379c
commit
297e001f14
@ -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")
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
89
scripts/check-all-by-signal.sh
Normal file
89
scripts/check-all-by-signal.sh
Normal file
@ -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
|
||||
@ -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"
|
||||
|
||||
@ -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"
|
||||
Loading…
x
Reference in New Issue
Block a user