From 5f71f27c639d337f14a1635ededf537486d76d1f Mon Sep 17 00:00:00 2001 From: luhaoling <2198702716@qq.com> Date: Thu, 22 Feb 2024 18:00:26 +0800 Subject: [PATCH] fix: refactoring scripts --- scripts/check-all.sh | 22 ++--- scripts/install/common.sh | 12 +++ scripts/install/openim-api.sh | 27 +++-- scripts/install/openim-crontask.sh | 8 +- scripts/install/openim-msggateway.sh | 9 +- scripts/install/openim-msgtransfer.sh | 37 ++++++- scripts/install/openim-push.sh | 8 +- scripts/install/openim-rpc.sh | 25 +++-- scripts/install/openim-tools.sh | 2 - scripts/lib/golang.sh | 18 ++++ scripts/lib/util.sh | 137 ++++++++++++++++++++++---- scripts/start-all.sh | 27 +++-- scripts/stop-all.sh | 18 +++- 13 files changed, 279 insertions(+), 71 deletions(-) diff --git a/scripts/check-all.sh b/scripts/check-all.sh index c37b3446d..f7e29376b 100755 --- a/scripts/check-all.sh +++ b/scripts/check-all.sh @@ -49,13 +49,6 @@ print_services_and_ports() { 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 @@ -71,7 +64,6 @@ 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 @@ -92,15 +84,17 @@ 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[@]} +echo "+++ The process being checked:" +for item in "${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]}"; do + echo "$item" +done +result=$(openim::util::check_process_names ${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]}) if [[ $? -ne 0 ]]; then echo "+++ cat openim log file >>> ${LOG_FILE}" - openim::log::error_exit "The service does not start properly, please check the port, query variable definition!" + openim::log::error "check process failed.\n $result" + return 1 else echo "++++ Check all openim service ports successfully !" + return 0 fi -set -e - -trap - ERR \ No newline at end of file diff --git a/scripts/install/common.sh b/scripts/install/common.sh index add1eebf8..32498d94c 100755 --- a/scripts/install/common.sh +++ b/scripts/install/common.sh @@ -99,6 +99,18 @@ IFS=" " read -ra OPENIM_SERVER_PORT_TARGETS <<< "$(openim::common::service_port) readonly OPENIM_SERVER_PORT_TARGETS readonly OPENIM_SERVER_PORT_LISTARIES=("${OPENIM_SERVER_PORT_TARGETS[@]##*/}") +OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER=() +for target in openim::golang::server_targets_no_transfer; do + OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER+=("${OPENIM_OUTPUT_HOSTBIN}/${target}") +done +readonly OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER + +OPENIM_ALL_SERVICE_LIBRARIES=() +for target in openim::golang::server_targets; do + OPENIM_ALL_SERVICE_LIBRARIES+=("${OPENIM_OUTPUT_HOSTBIN}/${target}") +done +readonly OPENIM_ALL_SERVICE_LIBRARIES + openim::common::dependency_name() { local targets=( redis diff --git a/scripts/install/openim-api.sh b/scripts/install/openim-api.sh index 6977b519d..933fef2ce 100755 --- a/scripts/install/openim-api.sh +++ b/scripts/install/openim-api.sh @@ -47,24 +47,37 @@ function openim::api::start() { openim::log::info "Starting ${SERVER_NAME} ..." + readonly OPENIM_API_SERVER_LIBRARIES="${OPENIM_OUTPUT_HOSTBIN}/${SERVER_NAME}" + result=$(openim::util::stop_services_with_name ${OPENIM_API_SERVER_LIBRARIES}) + if [[ $? -ne 0 ]]; then + openim::log::error "stop ${SERVER_NAME} failed" + return 1 + fi + printf "+------------------------+--------------+\n" printf "| Service Name | Port |\n" printf "+------------------------+--------------+\n" - + + local length=${#OPENIM_API_SERVICE_LISTARIES[@]} - for ((i=0; i> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & - openim::util::check_process_names ${SERVER_NAME} + return 0 } diff --git a/scripts/install/openim-msggateway.sh b/scripts/install/openim-msggateway.sh index 903f2d32e..79bdee222 100755 --- a/scripts/install/openim-msggateway.sh +++ b/scripts/install/openim-msggateway.sh @@ -32,7 +32,11 @@ function openim::msggateway::start() { openim::log::info "Start OpenIM Msggateway, binary root: ${SERVER_NAME}" openim::log::status "Start OpenIM Msggateway, path: ${OPENIM_MSGGATEWAY_BINARY}" - openim::util::stop_services_with_name ${OPENIM_MSGGATEWAY_BINARY} + result=$(openim::util::stop_services_with_name ${OPENIM_MSGGATEWAY_BINARY}) + if [[ $? -ne 0 ]]; then + openim::log::error "stop ${SERVER_NAME} failed" + return 1 + fi # OpenIM message gateway service port OPENIM_MESSAGE_GATEWAY_PORTS=$(openim::util::list-to-string ${OPENIM_MESSAGE_GATEWAY_PORT} ) @@ -66,8 +70,7 @@ function openim::msggateway::start() { nohup ${OPENIM_MSGGATEWAY_BINARY} --port ${OPENIM_MSGGATEWAY_PORTS_ARRAY[$i]} --ws_port ${OPENIM_WS_PORTS_ARRAY[$i]} $PROMETHEUS_PORT_OPTION -c ${OPENIM_MSGGATEWAY_CONFIG} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & done - - openim::util::check_process_names ${SERVER_NAME} + return 0 } ###################################### Linux Systemd ###################################### diff --git a/scripts/install/openim-msgtransfer.sh b/scripts/install/openim-msgtransfer.sh index bd3e51b0e..ca4d1086e 100755 --- a/scripts/install/openim-msgtransfer.sh +++ b/scripts/install/openim-msgtransfer.sh @@ -34,7 +34,11 @@ function openim::msgtransfer::start() { openim::log::info "Start OpenIM Msggateway, binary root: ${SERVER_NAME}" openim::log::status "Start OpenIM Msggateway, path: ${OPENIM_MSGTRANSFER_BINARY}" - openim::util::stop_services_with_name ${OPENIM_MSGTRANSFER_BINARY} + result=$(openim::util::stop_services_with_name ${OPENIM_MSGTRANSFER_BINARY}) + if [[ $? -ne 0 ]]; then + openim::log::error "stop ${SERVER_NAME} failed" + return 1 + fi # Message Transfer Prometheus port list MSG_TRANSFER_PROM_PORTS=(openim::util::list-to-string ${MSG_TRANSFER_PROM_PORT} ) @@ -63,8 +67,8 @@ function openim::msgtransfer::start() { fi nohup ${OPENIM_MSGTRANSFER_BINARY} ${PROMETHEUS_PORT_OPTION} -c ${OPENIM_MSGTRANSFER_CONFIG} -n ${i} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & done - - openim::util::check_process_names "${OPENIM_OUTPUT_HOSTBIN}/${SERVER_NAME}" + return 0 + } function openim::msgtransfer::check() { @@ -88,6 +92,33 @@ function openim::msgtransfer::check() { fi } +function openim::msgtransfer::check_for_stop() { + PIDS=$(pgrep -f "${OPENIM_OUTPUT_HOSTBIN}/openim-msgtransfer") || PIDS="0" + if [ "$PIDS" = "0" ]; then + return 0 + fi + + NUM_PROCESSES=$(echo "$PIDS" | wc -l | xargs) + + 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 + echo -e "\033[31m$(ps -p $PID -o pid,cmd)\033[0m" + elif [[ "$OSTYPE" == "darwin"* ]]; then + echo -e "\033[31m$(ps -p $PID -o pid,comm)\033[0m" + 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 + return 0 +} + + ###################################### Linux Systemd ###################################### SYSTEM_FILE_PATH="/etc/systemd/system/${SERVER_NAME}.service" diff --git a/scripts/install/openim-push.sh b/scripts/install/openim-push.sh index 6df1d7b09..23f4d72e3 100755 --- a/scripts/install/openim-push.sh +++ b/scripts/install/openim-push.sh @@ -62,7 +62,11 @@ function openim::push::start() { OPENIM_PUSH_PORTS_ARRAY=$(openim::util::list-to-string ${OPENIM_PUSH_PORT} ) PUSH_PROM_PORTS_ARRAY=$(openim::util::list-to-string ${PUSH_PROM_PORT} ) - openim::util::stop_services_with_name ${SERVER_NAME} + result=$(openim::util::stop_services_with_name ${SERVER_NAME}) + if [[ $? -ne 0 ]]; then + openim::log::error "stop ${SERVER_NAME} failed" + return 1 + fi openim::log::status "push port list: ${OPENIM_PUSH_PORTS_ARRAY[@]}" openim::log::status "prometheus port list: ${PUSH_PROM_PORTS_ARRAY[@]}" @@ -75,8 +79,8 @@ function openim::push::start() { openim::log::info "start push process, port: ${OPENIM_PUSH_PORTS_ARRAY[$i]}, prometheus port: ${PUSH_PROM_PORTS_ARRAY[$i]}" nohup ${OPENIM_PUSH_BINARY} --port ${OPENIM_PUSH_PORTS_ARRAY[$i]} -c ${OPENIM_PUSH_CONFIG} --prometheus_port ${PUSH_PROM_PORTS_ARRAY[$i]} >> ${LOG_FILE} 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & done + return 0 - openim::util::check_process_names ${SERVER_NAME} } ###################################### Linux Systemd ###################################### diff --git a/scripts/install/openim-rpc.sh b/scripts/install/openim-rpc.sh index e569cf114..95cce7ffa 100755 --- a/scripts/install/openim-rpc.sh +++ b/scripts/install/openim-rpc.sh @@ -121,11 +121,13 @@ function openim::rpc::start() { printf "+------------------------+-------+-----------------+\n" done + result=$(openim::util::stop_services_with_name ${OPENIM_API_SERVER_LIBRARIES}) + if [[ $? -ne 0 ]]; then + openim::log::error "stop ${SERVER_NAME} failed" + fi + # start all rpc services for ((i = 0; i < ${#OPENIM_RPC_SERVICE_LISTARIES[*]}; i++)); do - # openim::util::stop_services_with_name ${OPENIM_RPC_SERVICE_LISTARIES - openim::util::stop_services_on_ports ${OPENIM_RPC_PORT_LISTARIES[$i]} - openim::util::stop_services_on_ports ${OPENIM_RPC_PROM_PORT_LISTARIES[$i]} openim::log::info "OpenIM ${OPENIM_RPC_SERVICE_LISTARIES[$i]} config path: ${OPENIM_RPC_CONFIG}" @@ -138,15 +140,25 @@ function openim::rpc::start() { for ((j = 0; j < ${#OPENIM_RPC_SERVICE_PORTS_ARRAY[@]}; j++)); do openim::log::info "Starting ${OPENIM_RPC_SERVICE_LISTARIES[$i]} service, port: ${OPENIM_RPC_SERVICE_PORTS[j]}, prometheus port: ${OPENIM_RPC_PROM_PORTS[j]}, binary root: ${OPENIM_OUTPUT_HOSTBIN}/${OPENIM_RPC_SERVICE_LISTARIES[$i]}" - openim::rpc::start_service "${OPENIM_RPC_SERVICE_LISTARIES[$i]}" "${OPENIM_RPC_SERVICE_PORTS[j]}" "${OPENIM_RPC_PROM_PORTS[j]}" + result=$(openim::rpc::start_service "${OPENIM_RPC_SERVICE_LISTARIES[$i]}" "${OPENIM_RPC_SERVICE_PORTS[j]}" "${OPENIM_RPC_PROM_PORTS[j]}") + if [[ $? -ne 0 ]]; then + openim::log::error "stop ${SERVER_NAME} failed" + else + openim::log::info "$result" + fi done done sleep 5 - openim::util::check_ports ${OPENIM_RPC_PORT_TARGETS[@]} + result=$(openim::util::check_ports ${OPENIM_RPC_PORT_TARGETS[@]}) + if [[ $? -ne 0 ]]; then + openim::log::error "check ${SERVER_NAME} failed" + else + openim::log::info "$result" + fi # openim::util::check_ports ${OPENIM_RPC_PROM_PORT_TARGETS[@]} - + return 0 } function openim::rpc::start_service() { @@ -161,6 +173,7 @@ function openim::rpc::start_service() { cmd="${cmd} --prometheus_port ${prometheus_port}" fi nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) & + return 0 } ###################################### Linux Systemd ###################################### diff --git a/scripts/install/openim-tools.sh b/scripts/install/openim-tools.sh index f41ed01b9..72005ed8e 100755 --- a/scripts/install/openim-tools.sh +++ b/scripts/install/openim-tools.sh @@ -122,7 +122,6 @@ function openim::tools::pre-start() { for tool in "${OPENIM_TOOLS_PRE_START_NAME_LISTARIES[@]}"; do openim::log::info "Starting ${tool}..." openim::tools::start_service ${tool} ${OPNEIM_CONFIG} - sleep 0.2 done } @@ -131,7 +130,6 @@ function openim::tools::post-start() { for tool in "${OPENIM_TOOLS_POST_START_NAME_LISTARIES[@]}"; do openim::log::info "Starting ${tool}..." openim::tools::start_service ${tool} - sleep 0.2 done } diff --git a/scripts/lib/golang.sh b/scripts/lib/golang.sh index af04771d5..22bef6bc1 100755 --- a/scripts/lib/golang.sh +++ b/scripts/lib/golang.sh @@ -84,6 +84,24 @@ openim::golang::server_targets() { echo "${targets[@]}" } +openim::golang::server_targets_no_transfer() { + local targets=( + openim-api + openim-cmdutils + openim-crontask + openim-msggateway + openim-push + openim-rpc-auth + openim-rpc-conversation + openim-rpc-friend + openim-rpc-group + openim-rpc-msg + openim-rpc-third + openim-rpc-user + ) + echo "${targets[@]}" +} + IFS=" " read -ra OPENIM_SERVER_TARGETS <<< "$(openim::golang::server_targets)" readonly OPENIM_SERVER_TARGETS readonly OPENIM_SERVER_BINARIES=("${OPENIM_SERVER_TARGETS[@]##*/}") diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 51fc8af67..38f2a2956 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -457,6 +457,81 @@ openim::util::check_process_names() { fi } +openim::util::check_process_names_for_stop() { + # Function to get the port of a process + get_port() { + local pid=$1 + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + # Linux + ss -ltnp 2>/dev/null | grep $pid | awk '{print $4}' | cut -d ':' -f2 + elif [[ "$OSTYPE" == "darwin"* ]]; then + # macOS + lsof -nP -iTCP -sTCP:LISTEN -a -p $pid | awk 'NR>1 {print $9}' | sed 's/.*://' + else + echo "Unsupported OS" + return 1 + fi + } + + # Arrays to collect details of processes + local not_started=() + local started=() + + openim::log::info "Checking processes: $*" + # Iterate over each given process name + for process_name in "$@"; do + # Use `pgrep` to find process IDs related to the given process name + local pids=($(pgrep -f $process_name)) + + # Check if any process IDs were found + if [[ ${#pids[@]} -eq 0 ]]; then + not_started+=($process_name) + else + # If there are PIDs, loop through each one + for pid in "${pids[@]}"; do + local command=$(ps -p $pid -o cmd=) + local start_time=$(ps -p $pid -o lstart=) + local port=$(get_port $pid) + + # Check if port information was found for the PID + if [[ -z $port ]]; then + port="N/A" + fi + + started+=("Process $process_name - Command: $command, PID: $pid, Port: $port, Start time: $start_time") + done + fi + done + + # Print information + if [[ ${#not_started[@]} -ne 0 ]]; then + openim::log::info "Not started processes:" + for process_name in "${not_started[@]}"; do + openim::log::error "Process $process_name is not started." + done + fi + + if [[ ${#started[@]} -ne 0 ]]; then + echo + openim::log::info "Started processes:" + for info in "${started[@]}"; do + openim::log::info "$info" + done + fi + + # Return status + if [[ ${#not_started[@]} -ne 0 ]]; then + 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"}' + return 1 + else + echo "" + openim::log::success "All processes are running." + return 0 + fi +} + # openim::util::check_process_names docker-pr # The `openim::util::stop_services_on_ports` function stops services running on specified ports. @@ -573,26 +648,8 @@ openim::util::stop_services_with_name() { not_stopped+=("$server_name") fi done + return 0 - # Print information about services whose processes couldn't be stopped. - if [[ ${#not_stopped[@]} -ne 0 ]]; then - openim::log::info "Services that couldn't be stopped:" - for name in "${not_stopped[@]}"; do - openim::log::status "Failed to stop the $name service." - done - fi - - # Print information about services whose processes were successfully stopped. - if [[ ${#stopped[@]} -ne 0 ]]; then - echo - openim::log::info "Stopped services:" - for name in "${stopped[@]}"; do - openim::log::info "Successfully stopped the $name service." - done - fi - - openim::log::success "All specified services were stopped." - echo "" } # sleep 333333& # sleep 444444& @@ -2822,6 +2879,46 @@ function openim::util::gen_os_arch() { fi } + + +function openim::util::check_process_names_for_stop() { + local all_stopped=true + + for service in "${OPENIM_ALL_SERVICE_LIBRARIES[@]}"; do + PIDS=$(pgrep -f "${service}") || PIDS="0" + if [ "$PIDS" = "0" ]; then + continue + fi + + all_stopped=false + NUM_PROCESSES=$(echo "$PIDS" | wc -l | xargs) + + if [ "$NUM_PROCESSES" -gt 0 ]; then + openim::log::error "Found $NUM_PROCESSES processes for ${service}" + for PID in $PIDS; do + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + echo -e "\033[31m$(ps -p $PID -o pid,cmd)\033[0m" + elif [[ "$OSTYPE" == "darwin"* ]]; then + echo -e "\033[31m$(ps -p $PID -o pid,comm)\033[0m" + else + openim::log::error "Unsupported OS type: $OSTYPE" + fi + done + openim::log::error "Processes for ${service} have not been stopped properly." + fi + done + + if [ "$all_stopped" = true ]; then + openim::log::success "All processes have been stopped properly." + return 0 + fi + + return 1 +} + + + + if [[ "$*" =~ openim::util:: ]];then eval $* -fi \ No newline at end of file +fi diff --git a/scripts/start-all.sh b/scripts/start-all.sh index 2c6581208..39c5d1bf2 100755 --- a/scripts/start-all.sh +++ b/scripts/start-all.sh @@ -54,19 +54,18 @@ function execute_scripts() { openim::log::status "Starting script: ${script_path##*/}" # Log the script name. # Execute the script with the constructed argument. - "$script_path" "$arg" - - # Check if the script executed successfully. - if [[ $? -eq 0 ]]; then - openim::log::info "${script_path##*/} executed successfully." - else - openim::log::errexit "Error executing ${script_path##*/}." + result=$("$script_path" "$arg") + if [[ $? -ne 0 ]]; then + openim::log::error "Start script: ${script_path##*/} failed" + openim::log::error "$result" + return 1 fi + else openim::log::errexit "Script ${script_path##*/} is missing or not executable." + return 1 fi done - sleep 0.5 } @@ -79,6 +78,18 @@ ${TOOLS_START_SCRIPTS_PATH} openim::tools::pre-start openim::log::info "\n## Starting OpenIM services" execute_scripts +sleep 2 + +openim::log::info "\n## Check OpenIM service name" +. $(dirname ${BASH_SOURCE})/install/openim-msgtransfer.sh openim::msgtransfer::check + +echo "+++ The process being checked:" +for item in "${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]}"; do + echo "$item" +done + +openim::util::check_process_names ${OPENIM_ALL_SERVICE_LIBRARIES_NO_TRANSFER[@]} + openim::log::info "\n## Post Starting OpenIM services" ${TOOLS_START_SCRIPTS_PATH} openim::tools::post-start diff --git a/scripts/stop-all.sh b/scripts/stop-all.sh index 075418509..9b2baa23d 100755 --- a/scripts/stop-all.sh +++ b/scripts/stop-all.sh @@ -28,12 +28,20 @@ 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[@]}" - -openim::util::stop_services_on_ports ${OPENIM_SERVER_PORT_LISTARIES[@]} - echo -e "\n++ Stop all processes in the path ${OPENIM_OUTPUT_HOSTBIN}" openim::util::stop_services_with_name "${OPENIM_OUTPUT_HOSTBIN}" +# todo OPENIM_ALL_SERVICE_LIBRARIES + + + + + +result=$(openim::util::check_process_names_for_stop) +if [[ $? -ne 0 ]]; then + echo "+++ cat openim log file >>> ${LOG_FILE}" + openim::log::error "stop process failed.\n$(result)" + return 1 +fi -openim::log::success "✨ All processes to be killed" \ No newline at end of file +openim::log::success "✨ All processes to be stopped" \ No newline at end of file