diff --git a/cmd/openim-api/main.go b/cmd/openim-api/main.go index 7a7e06293..34ada1d57 100644 --- a/cmd/openim-api/main.go +++ b/cmd/openim-api/main.go @@ -119,7 +119,7 @@ func run(port int, proPort int) error { defer cancel() select { case <-sigs: - util.SIGUSR1Exit() + util.SIGTERMExit() err := server.Shutdown(ctx) if err != nil { return errs.Wrap(err, "shutdown err") diff --git a/install.sh b/install.sh index 7ff0f8739..4b8e74c55 100755 --- a/install.sh +++ b/install.sh @@ -18,7 +18,7 @@ # set -e -set -o pipefail + ############################## OpenIM Github ############################## # ... rest of the script ... diff --git a/internal/msggateway/init.go b/internal/msggateway/init.go index 321407f7e..b18efcd50 100644 --- a/internal/msggateway/init.go +++ b/internal/msggateway/init.go @@ -46,9 +46,7 @@ func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error { netDone := make(chan error) go func() { err = hubServer.Start() - if err != nil { - netDone <- err - } + netDone <- err }() return hubServer.LongConnServer.Run(netDone) } diff --git a/internal/msgtransfer/init.go b/internal/msgtransfer/init.go index 1766a5419..df06ec0f9 100644 --- a/internal/msgtransfer/init.go +++ b/internal/msgtransfer/init.go @@ -139,12 +139,12 @@ func (m *MsgTransfer) Start(prometheusPort int) error { signal.Notify(sigs, syscall.SIGTERM) select { case <-sigs: - util.SIGUSR1Exit() + util.SIGTERMExit() // graceful close kafka client. m.cancel() m.historyCH.historyConsumerGroup.Close() m.historyMongoCH.historyConsumerGroup.Close() - + return nil case <-netDone: m.cancel() m.historyCH.historyConsumerGroup.Close() @@ -152,6 +152,4 @@ func (m *MsgTransfer) Start(prometheusPort int) error { close(netDone) return netErr } - - return nil } diff --git a/pkg/common/startrpc/start.go b/pkg/common/startrpc/start.go index c5105ec51..be9fbd25b 100644 --- a/pkg/common/startrpc/start.go +++ b/pkg/common/startrpc/start.go @@ -140,7 +140,7 @@ func Start( signal.Notify(sigs, syscall.SIGTERM) select { case <-sigs: - util.SIGUSR1Exit() + util.SIGTERMExit() ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() if err := gracefulStopWithCtx(ctx, srv.GracefulStop); err != nil { @@ -152,7 +152,7 @@ func Start( if err != nil { return errs.Wrap(err, "shutdown err") } - return errors.New("SIGTERM EXIT") + return nil case <-netDone: close(netDone) return netErr diff --git a/pkg/util/genutil/genutil.go b/pkg/util/genutil/genutil.go index f97b803f6..bd9376d58 100644 --- a/pkg/util/genutil/genutil.go +++ b/pkg/util/genutil/genutil.go @@ -15,7 +15,6 @@ package genutil import ( - "errors" "fmt" "os" "path/filepath" @@ -42,15 +41,12 @@ func OutDir(path string) (string, error) { } func ExitWithError(err error) { - if errors.Is(err, errors.New("SIGTERM EXIT")) { - os.Exit(-1) - } progName := filepath.Base(os.Args[0]) - fmt.Fprintf(os.Stderr, "\n\n%s exit -1: \n%+v\n\n", progName, err) + fmt.Fprintf(os.Stderr, "%s exit -1: %+v\n", progName, err) os.Exit(-1) } -func SIGUSR1Exit() { +func SIGTERMExit() { progName := filepath.Base(os.Args[0]) - fmt.Printf("\n\n%s receive process terminal SIGTERM exit 0\n\n", progName) + fmt.Fprintf(os.Stderr, "Warning %s receive process terminal SIGTERM exit 0\n", progName) } diff --git a/scripts/advertise.sh b/scripts/advertise.sh index 3effc4f2b..958b4e3ae 100755 --- a/scripts/advertise.sh +++ b/scripts/advertise.sh @@ -14,7 +14,7 @@ # limitations under the License. set -e -set -o pipefail + . $(dirname ${BASH_SOURCE})/lib/init.sh diff --git a/scripts/build-all-service.sh b/scripts/build-all-service.sh index b5578fca6..6335b0e08 100755 --- a/scripts/build-all-service.sh +++ b/scripts/build-all-service.sh @@ -22,9 +22,9 @@ # Usage: `scripts/build-all-service.sh`. # Example: `scripts/build-go.sh WHAT=cmd/kubelet`. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh" diff --git a/scripts/check-all.sh b/scripts/check-all.sh index 062605ae1..a9b07d65b 100755 --- a/scripts/check-all.sh +++ b/scripts/check-all.sh @@ -19,9 +19,9 @@ # 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" @@ -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 @@ -85,22 +77,29 @@ 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 !" + openim::log::success "All components depended on by openim are running normally! " 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[@]} +openim::log::info "\n## Check openim service name:\n${OPENIM_OUTPUT_HOSTBIN}/openim-msgtransfer" +result=$(. $(dirname ${BASH_SOURCE})/install/openim-msgtransfer.sh openim::msgtransfer::check) 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!" -else - echo "++++ Check all openim service ports successfully !" + openim::log::error "check process failed.\n $result" fi -set -e -trap - ERR \ No newline at end of file +echo "Check openim service name:" +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 "check process failed.\n " + echo "$result" +else + openim::log::success "All openim services are running normally! " +fi + diff --git a/scripts/cherry-pick.sh b/scripts/cherry-pick.sh index 8a1f8dd79..f8d7912f8 100755 --- a/scripts/cherry-pick.sh +++ b/scripts/cherry-pick.sh @@ -21,9 +21,9 @@ # checks them out to a branch named: # automated-cherry-pick-of--- -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh" diff --git a/scripts/common.sh b/scripts/common.sh index d67389d56..702f55588 100755 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -18,9 +18,9 @@ # shellcheck disable=SC2034 # Variables sourced in other scripts. # Common utilities, variables and checks for all build scripts. -set -o errexit -set +o nounset -set -o pipefail + + + # Unset CDPATH, having it set messes up with script import paths unset CDPATH diff --git a/scripts/docker-start-all.sh b/scripts/docker-start-all.sh index 162655553..c47069e3b 100755 --- a/scripts/docker-start-all.sh +++ b/scripts/docker-start-all.sh @@ -14,9 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -o errexit -set -o nounset -set -o pipefail + + + #fixme This scripts is the total startup scripts #fixme The full name of the shell scripts that needs to be started is placed in the need_to_start_server_shell array diff --git a/scripts/gen-swagger-docs.sh b/scripts/gen-swagger-docs.sh index 68410e79c..e768f9106 100755 --- a/scripts/gen-swagger-docs.sh +++ b/scripts/gen-swagger-docs.sh @@ -15,9 +15,9 @@ # Script to generate docs from the latest swagger spec. -set -o errexit -set -o nounset -set -o pipefail + + + # The root of the build/dist directory OPENIM_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" diff --git a/scripts/init-config.sh b/scripts/init-config.sh index 82eefbb54..c7cf16320 100755 --- a/scripts/init-config.sh +++ b/scripts/init-config.sh @@ -15,9 +15,9 @@ # This script automatically initializes various configuration files and can generate example files. -set -o errexit -set -o nounset -set -o pipefail + + + # Root directory of the OpenIM project OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. diff --git a/scripts/init-env.sh b/scripts/init-env.sh index 75b871b08..1e802c3be 100755 --- a/scripts/init-env.sh +++ b/scripts/init-env.sh @@ -16,8 +16,8 @@ #FIXME This script is the startup script for multiple servers. #FIXME The full names of the shell scripts that need to be started are placed in the `need_to_start_server_shell` array. -set -o nounset -set -o pipefail + + OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd -P) source "${OPENIM_ROOT}/scripts/install/common.sh" diff --git a/scripts/install-im-server.sh b/scripts/install-im-server.sh index c1224e30c..d11a49dc8 100755 --- a/scripts/install-im-server.sh +++ b/scripts/install-im-server.sh @@ -27,9 +27,9 @@ # Usage: # SERVER_IMAGE_VERSION=latest IMAGE_REGISTRY=myregistry ./this_script.sh -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh" diff --git a/scripts/install/common.sh b/scripts/install/common.sh index f6ee5d3ad..ed1ba1a4b 100755 --- a/scripts/install/common.sh +++ b/scripts/install/common.sh @@ -15,9 +15,9 @@ # Common utilities, variables and checks for all build scripts. -set -o errexit -set +o nounset -set -o pipefail + + + # Sourced flag COMMON_SOURCED=true @@ -99,6 +99,22 @@ 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_SERVER_BINARIES_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_SERVER_BINARIES_NO_CMDUTILS[@]}"; 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/dependency.sh b/scripts/install/dependency.sh index e7c7eb426..bad1cb6f9 100755 --- a/scripts/install/dependency.sh +++ b/scripts/install/dependency.sh @@ -15,9 +15,9 @@ # This script will install the dependencies required for openim -set -o errexit -set +o nounset -set -o pipefail + + + OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) [[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh diff --git a/scripts/install/install.sh b/scripts/install/install.sh index d5ec5b7f7..bb09675bf 100755 --- a/scripts/install/install.sh +++ b/scripts/install/install.sh @@ -109,7 +109,7 @@ function openim::uninstall::uninstall_openim() { openim::common::sudo "systemctl stop openim.target" openim::common::sudo "systemctl disable openim.target" openim::common::sudo "rm -f /etc/systemd/system/openim.target" - set -o errexit + openim::log::success "openim uninstall success" } diff --git a/scripts/install/openim-api.sh b/scripts/install/openim-api.sh index be2a2d33b..8a7b8d1f1 100755 --- a/scripts/install/openim-api.sh +++ b/scripts/install/openim-api.sh @@ -14,9 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -o errexit -set +o nounset -set -o pipefail + + + OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) [[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh @@ -47,24 +47,31 @@ function openim::api::start() { openim::log::info "Starting ${SERVER_NAME} ..." + readonly OPENIM_API_SERVER_LIBRARIES="${OPENIM_OUTPUT_HOSTBIN}/${SERVER_NAME}" + + 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 } @@ -107,7 +105,7 @@ function openim::crontask::uninstall() { openim::common::sudo "rm -f ${OPENIM_INSTALL_DIR}/${SERVER_NAME}" openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/${SERVER_NAME}.yaml" openim::common::sudo "rm -f /etc/systemd/system/${SERVER_NAME}.service" - set -o errexit + openim::log::info "uninstall ${SERVER_NAME} successfully" } diff --git a/scripts/install/openim-man.sh b/scripts/install/openim-man.sh index fac5cebea..642588a28 100755 --- a/scripts/install/openim-man.sh +++ b/scripts/install/openim-man.sh @@ -76,7 +76,7 @@ function openim::man::uninstall() { # Turn off exit-on-error temporarily to handle non-existing files gracefully set +o errexit openim::common::sudo "rm -f /usr/share/man/man1/openim-*" - set -o errexit + openim::log::info "Uninstalled openim man pages successfully" } diff --git a/scripts/install/openim-msggateway.sh b/scripts/install/openim-msggateway.sh index a2316c784..3eb9bb349 100755 --- a/scripts/install/openim-msggateway.sh +++ b/scripts/install/openim-msggateway.sh @@ -14,14 +14,13 @@ # limitations under the License. # Common utilities, variables and checks for all build scripts. -set -o errexit -set +o nounset -set -o pipefail + + + OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) [[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh -openim::util::set_max_fd 200000 SERVER_NAME="openim-msggateway" @@ -31,8 +30,7 @@ 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} + # OpenIM message gateway service port OPENIM_MESSAGE_GATEWAY_PORTS=$(openim::util::list-to-string ${OPENIM_MESSAGE_GATEWAY_PORT} ) @@ -66,8 +64,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 ###################################### @@ -117,7 +114,7 @@ function openim::msggateway::uninstall() { openim::common::sudo "rm -f ${OPENIM_INSTALL_DIR}/${SERVER_NAME}" openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/${SERVER_NAME}.yaml" openim::common::sudo "rm -f /etc/systemd/system/${SERVER_NAME}.service" - set -o errexit + openim::log::info "uninstall ${SERVER_NAME} successfully" } diff --git a/scripts/install/openim-msgtransfer.sh b/scripts/install/openim-msgtransfer.sh index 783e06729..9fdf07fe2 100755 --- a/scripts/install/openim-msgtransfer.sh +++ b/scripts/install/openim-msgtransfer.sh @@ -16,15 +16,13 @@ # ./scripts/install/openim-msgtransfer.sh openim::msgtransfer::start # Common utilities, variables and checks for all build scripts. -set -o errexit -set +o nounset -set -o pipefail + + + OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) [[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh -openim::util::set_max_fd 200000 - SERVER_NAME="openim-msgtransfer" function openim::msgtransfer::start() { @@ -33,9 +31,7 @@ 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} - + # Message Transfer Prometheus port list MSG_TRANSFER_PROM_PORTS=(openim::util::list-to-string ${MSG_TRANSFER_PROM_PORT} ) @@ -46,11 +42,11 @@ function openim::msgtransfer::start() { openim::log::info "openim maggateway num: ${OPENIM_MSGGATEWAY_NUM}" if [ "${OPENIM_MSGGATEWAY_NUM}" -lt 1 ]; then - opeim::log::error_exit "OPENIM_MSGGATEWAY_NUM must be greater than 0" + opeim::log::error "OPENIM_MSGGATEWAY_NUM must be greater than 0" fi if [ ${OPENIM_MSGGATEWAY_NUM} -ne $((${#MSG_TRANSFER_PROM_PORTS[@]} - 1)) ]; then - openim::log::error_exit "OPENIM_MSGGATEWAY_NUM must be equal to the number of MSG_TRANSFER_PROM_PORTS" + openim::log::error "OPENIM_MSGGATEWAY_NUM must be equal to the number of MSG_TRANSFER_PROM_PORTS" fi for (( i=0; i<$OPENIM_MSGGATEWAY_NUM; i++ )) do @@ -63,17 +59,18 @@ 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() { PIDS=$(pgrep -f "${OPENIM_OUTPUT_HOSTBIN}/openim-msgtransfer") - - NUM_PROCESSES=$(echo "$PIDS" | wc -l) - + if [ -z "$PIDS" ]; then + NUM_PROCESSES=0 + else + NUM_PROCESSES=$(echo "$PIDS" | wc -l) + fi 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 @@ -84,10 +81,39 @@ function openim::msgtransfer::check() { fi done else - openim::log::error_exit "Expected $OPENIM_MSGGATEWAY_NUM openim msgtransfer processes, but found $NUM_PROCESSES msgtransfer processes." + openim::log::error "Expected $OPENIM_MSGGATEWAY_NUM openim msgtransfer processes, but found $NUM_PROCESSES msgtransfer processes." + return 1 fi + return 0 } +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" @@ -138,7 +164,7 @@ function openim::msgtransfer::uninstall() { openim::common::sudo "rm -f ${OPENIM_INSTALL_DIR}/${SERVER_NAME}" openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/${SERVER_NAME}.yaml" openim::common::sudo "rm -f /etc/systemd/system/${SERVER_NAME}.service" - set -o errexit + openim::log::info "uninstall ${SERVER_NAME} successfully" } diff --git a/scripts/install/openim-push.sh b/scripts/install/openim-push.sh index 95da16c8a..8684f185c 100755 --- a/scripts/install/openim-push.sh +++ b/scripts/install/openim-push.sh @@ -40,9 +40,9 @@ # # Note: Ensure that the appropriate permissions and environmental variables are set prior to script execution. # -set -o errexit -set +o nounset -set -o pipefail + + + OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) [[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh @@ -61,22 +61,20 @@ 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} - + openim::log::status "push port list: ${OPENIM_PUSH_PORTS_ARRAY[@]}" openim::log::status "prometheus port list: ${PUSH_PROM_PORTS_ARRAY[@]}" if [ ${#OPENIM_PUSH_PORTS_ARRAY[@]} -ne ${#PUSH_PROM_PORTS_ARRAY[@]} ]; then - openim::log::error_exit "The length of the two port lists is different!" + openim::log::error "The length of the two port lists is different!" fi for (( i=0; i<${#OPENIM_PUSH_PORTS_ARRAY[@]}; i++ )); do 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 ###################################### @@ -125,7 +123,7 @@ function openim::push::uninstall() { openim::common::sudo "rm -f ${OPENIM_INSTALL_DIR}/${SERVER_NAME}" openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/${SERVER_NAME}.yaml" openim::common::sudo "rm -f /etc/systemd/system/${SERVER_NAME}.service" - set -o errexit + openim::log::info "uninstall ${SERVER_NAME} successfully" } diff --git a/scripts/install/openim-rpc.sh b/scripts/install/openim-rpc.sh index f8feaaa57..023cb6594 100755 --- a/scripts/install/openim-rpc.sh +++ b/scripts/install/openim-rpc.sh @@ -38,9 +38,9 @@ # Note: Before executing this script, ensure that the necessary permissions are granted and relevant environmental variables are set. # -set -o errexit -set +o nounset -set -o pipefail + + + OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) [[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.sh @@ -64,6 +64,16 @@ IFS=" " read -ra OPENIM_RPC_SERVICE_TARGETS <<< "$(openim::rpc::service_name)" readonly OPENIM_RPC_SERVICE_TARGETS readonly OPENIM_RPC_SERVICE_LISTARIES=("${OPENIM_RPC_SERVICE_TARGETS[@]##*/}") + +OPENIM_ALL_RPC_FULL_PATH=() +for target in openim::rpc::service_name; do + OPENIM_ALL_RPC_FULL_PATH+=("${OPENIM_OUTPUT_HOSTBIN}/${target}") +done +readonly OPENIM_ALL_RPC_FULL_PATH + + + + # Make sure the environment is only called via common to avoid too much nesting openim::rpc::service_port() { local targets=( @@ -121,14 +131,11 @@ function openim::rpc::start() { printf "+------------------------+-------+-----------------+\n" done + # 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}" - # Get the service and Prometheus ports. OPENIM_RPC_SERVICE_PORTS=( $(openim::util::list-to-string ${OPENIM_RPC_PORT_LISTARIES[$i]}) ) read -a OPENIM_RPC_SERVICE_PORTS_ARRAY <<< ${OPENIM_RPC_SERVICE_PORTS} @@ -138,15 +145,17 @@ 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 "start ${SERVER_NAME} failed" + else + openim::log::info "$result" + fi done done - sleep 5 - - openim::util::check_ports ${OPENIM_RPC_PORT_TARGETS[@]} - # openim::util::check_ports ${OPENIM_RPC_PROM_PORT_TARGETS[@]} + return 0 } function openim::rpc::start_service() { @@ -161,6 +170,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 ###################################### @@ -220,7 +230,7 @@ function openim::rpc::uninstall() { openim::common::sudo "rm -f ${OPENIM_CONFIG_DIR}/${service}.yaml" openim::common::sudo "rm -f ${SYSTEM_FILE_PATHS[$service]}" done - set -o errexit + openim::log::info "uninstall openim-rpc successfully" } diff --git a/scripts/install/openim-tools.sh b/scripts/install/openim-tools.sh index ac60a5f45..72005ed8e 100755 --- a/scripts/install/openim-tools.sh +++ b/scripts/install/openim-tools.sh @@ -38,9 +38,9 @@ # Example: ./openim-tools.sh openim::tools::install # -set -o errexit -set +o nounset -set -o pipefail + + + OPENIM_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")"/../.. && pwd -P) [[ -z ${COMMON_SOURCED} ]] && source "${OPENIM_ROOT}"/scripts/install/common.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..7b9d7e60c 100755 --- a/scripts/lib/golang.sh +++ b/scripts/lib/golang.sh @@ -84,6 +84,57 @@ openim::golang::server_targets() { echo "${targets[@]}" } +openim::golang::server_targets_no_transfer() { + local targets=( + openim-api + 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[@]}" +} + +openim::golang::server_targets_no_cmdutils() { + local targets=( + openim-api + openim-crontask + openim-msggateway + openim-msgtransfer + 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_NO_CMDUTILS <<< "$(openim::golang::server_targets_no_cmdutils)" +readonly OPENIM_SERVER_TARGETS_NO_CMDUTILS +readonly OPENIM_SERVER_BINARIES_NO_CMDUTILS=("${OPENIM_SERVER_TARGETS_NO_CMDUTILS[@]##*/}") + + + + + +IFS=" " read -ra OPENIM_SERVER_TARGETS_NO_TRANSFER <<< "$(openim::golang::server_targets_no_transfer)" +readonly OPENIM_SERVER_TARGETS_NO_TRANSFER +readonly OPENIM_SERVER_BINARIES_NO_TRANSFER=("${OPENIM_SERVER_TARGETS_NO_TRANSFER[@]##*/}") + + + + 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/init.sh b/scripts/lib/init.sh index be8e9f8aa..4cd6d9fb8 100755 --- a/scripts/lib/init.sh +++ b/scripts/lib/init.sh @@ -13,9 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -o errexit -set +o nounset -set -o pipefail + + + # Short-circuit if init.sh has already been sourced [[ $(type -t openim::init::loaded) == function ]] && return 0 diff --git a/scripts/lib/logging.sh b/scripts/lib/logging.sh index 8f2bb33cf..7afb6bfce 100755 --- a/scripts/lib/logging.sh +++ b/scripts/lib/logging.sh @@ -75,12 +75,13 @@ openim::log::errexit() { openim::log::install_errexit() { # trap ERR to provide an error handler whenever a command exits nonzero this - # is a more verbose version of set -o errexit - trap 'openim::log::errexit' ERR + # is a more verbose version of + # trap 'openim::log::errexit' ERR # setting errtrace allows our ERR trap handler to be propagated to functions, # expansions and subshells - set -o errtrace + #set -o errtrace + return 0 } # Print out the stack trace @@ -205,22 +206,27 @@ openim::log::status() { fi timestamp=$(date +"[%Y-%m-%d %H:%M:%S %Z]") - echo_log "+++ ${timestamp} ${1}" + echo_log "${timestamp} ${1}" shift for message; do echo_log " ${message}" done } + openim::log::success() { local V="${V:-0}" if [[ ${OPENIM_VERBOSE} < ${V} ]]; then return fi - timestamp=$(date +"%m%d %H:%M:%S") - echo_log -e "${COLOR_GREEN}[success ${timestamp}] ${COLOR_SUFFIX}==> " "$@" + local timestamp=$(date +"%m%d %H:%M:%S") + local reset_color='\033[0m' + echo_log -e "${COLOR_GREEN}[success ${timestamp}]${COLOR_SUFFIX}==> ${COLOR_GREEN}$@${reset_color}" } + + + function openim::log::test_log() { echo_log "test log" openim::log::info "openim::log::info" diff --git a/scripts/lib/util.sh b/scripts/lib/util.sh index 1bdb7f640..4322255bb 100755 --- a/scripts/lib/util.sh +++ b/scripts/lib/util.sh @@ -298,7 +298,7 @@ openim::util::check_ports() { # An array to collect information about processes that are running. local started=() - openim::log::info "Checking ports: $*" + echo "Checking ports: $*" # Iterate over each given port. for port in "$@"; do # Initialize variables @@ -344,7 +344,7 @@ openim::util::check_ports() { # Print information about ports whose processes are not running. if [[ ${#not_started[@]} -ne 0 ]]; then - openim::log::info "\n### Not started ports:" + echo "### Not started ports:" for port in "${not_started[@]}"; do openim::log::error "Port $port is not started." done @@ -352,20 +352,20 @@ openim::util::check_ports() { # Print information about ports whose processes are running. if [[ ${#started[@]} -ne 0 ]]; then - openim::log::info "\n### Started ports:" + echo "### Started ports:" for info in "${started[@]}"; do - openim::log::info "$info" + echo "$info" done fi # If any of the processes is not running, return a status of 1. 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}" + 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 - openim::log::success "All specified processes are running." + #openim::log::success "All specified ports are running." return 0 fi } @@ -373,7 +373,7 @@ openim::util::check_ports() { # set +o errexit # Sample call for testing: # openim::util::check_ports 10002 1004 12345 13306 -# set -o errexit +# # The `openim::util::check_process_names` function analyzes the state of processes based on given names. # It accepts multiple process names as arguments and prints: @@ -402,7 +402,6 @@ openim::util::check_process_names() { 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 @@ -430,24 +429,24 @@ openim::util::check_process_names() { # Print information if [[ ${#not_started[@]} -ne 0 ]]; then - openim::log::info "Not started processes:" + echo "Not started processes:" for process_name in "${not_started[@]}"; do - openim::log::error "Process $process_name is not started." + echo "Process $process_name is not started." done fi if [[ ${#started[@]} -ne 0 ]]; then echo - openim::log::info "Started processes:" + echo "Started processes:" for info in "${started[@]}"; do - openim::log::info "$info" + echo "$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}" + 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 @@ -457,6 +456,66 @@ 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=() + + + # 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 + + + if [[ ${#started[@]} -ne 0 ]]; then + echo + echo "The programs that have not exited are:" + for info in "${started[@]}"; do + echo "$info " + done + return 1 + fi + + return 0 + +} + # openim::util::check_process_names docker-pr # The `openim::util::stop_services_on_ports` function stops services running on specified ports. @@ -473,7 +532,7 @@ openim::util::stop_services_on_ports() { # An array to collect information about processes that were stopped. local stopped=() - openim::log::info "Stopping services on ports: $*" + echo "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. @@ -497,7 +556,7 @@ openim::util::stop_services_on_ports() { # Print information about ports whose processes couldn't be stopped. if [[ ${#not_stopped[@]} -ne 0 ]]; then - openim::log::info "Ports that couldn't be stopped:" + echo "Ports that couldn't be stopped:" for port in "${not_stopped[@]}"; do openim::log::status "Failed to stop service on port $port." done @@ -506,7 +565,7 @@ openim::util::stop_services_on_ports() { # Print information about ports whose processes were successfully stopped. if [[ ${#stopped[@]} -ne 0 ]]; then for port in "${stopped[@]}"; do - openim::log::info "Successfully stopped service on port $port." + echo "Successfully stopped service on port $port." done fi @@ -539,7 +598,7 @@ openim::util::stop_services_with_name() { # An array to collect information about processes that were stopped. local stopped=() - openim::log::info "Stopping services with names: $*" + echo "Stopping services with names: $*" # 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. @@ -573,26 +632,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& @@ -1531,7 +1572,7 @@ openim::util::check_ports() { # An array to collect information about processes that are running. local started=() - openim::log::info "Checking ports: $*" + echo "Checking ports: $*" # Iterate over each given port. for port in "$@"; do # Initialize variables @@ -1577,7 +1618,7 @@ openim::util::check_ports() { # Print information about ports whose processes are not running. if [[ ${#not_started[@]} -ne 0 ]]; then - openim::log::info "\n### Not started ports:" + printf "\n### Not started ports:" for port in "${not_started[@]}"; do openim::log::error "Port $port is not started." done @@ -1585,16 +1626,16 @@ openim::util::check_ports() { # Print information about ports whose processes are running. if [[ ${#started[@]} -ne 0 ]]; then - openim::log::info "\n### Started ports:" + printf "\n### Started ports:" for info in "${started[@]}"; do - openim::log::info "$info" + echo "$info" done fi # If any of the processes is not running, return a status of 1. 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}" + openim::color::echo $COLOR_RED "OpenIM Stdout Log >> cat ${LOG_FILE}" + openim::color::echo $COLOR_RED "OpenIM Stderr Log >> cat ${STDERR_LOG_FILE}" echo "" cat "$TMP_LOG_FILE" | awk '{print "\033[31m" $0 "\033[0m"}' return 1 @@ -1607,7 +1648,7 @@ openim::util::check_ports() { # set +o errexit # Sample call for testing: # openim::util::check_ports 10002 1004 12345 13306 -# set -o errexit +# # The `openim::util::check_process_names` function analyzes the state of processes based on given names. # It accepts multiple process names as arguments and prints: @@ -1636,7 +1677,7 @@ openim::util::check_process_names() { local not_started=() local started=() - openim::log::info "Checking processes: $*" + echo "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 @@ -1664,7 +1705,7 @@ openim::util::check_process_names() { # Print information if [[ ${#not_started[@]} -ne 0 ]]; then - openim::log::info "Not started processes:" + echo "Not started processes:" for process_name in "${not_started[@]}"; do openim::log::error "Process $process_name is not started." done @@ -1672,9 +1713,9 @@ openim::util::check_process_names() { if [[ ${#started[@]} -ne 0 ]]; then echo - openim::log::info "Started processes:" + echo "Started processes:" for info in "${started[@]}"; do - openim::log::info "$info" + echo "$info" done fi @@ -1707,7 +1748,7 @@ openim::util::stop_services_on_ports() { # An array to collect information about processes that were stopped. local stopped=() - openim::log::info "Stopping services on ports: $*" + echo "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. @@ -1731,7 +1772,7 @@ openim::util::stop_services_on_ports() { # Print information about ports whose processes couldn't be stopped. if [[ ${#not_stopped[@]} -ne 0 ]]; then - openim::log::info "Ports that couldn't be stopped:" + echo "Ports that couldn't be stopped:" for port in "${not_stopped[@]}"; do openim::log::status "Failed to stop service on port $port." done @@ -1740,7 +1781,7 @@ openim::util::stop_services_on_ports() { # Print information about ports whose processes were successfully stopped. if [[ ${#stopped[@]} -ne 0 ]]; then for port in "${stopped[@]}"; do - openim::log::info "Successfully stopped service on port $port." + echo "Successfully stopped service on port $port." done fi @@ -1773,7 +1814,7 @@ openim::util::stop_services_with_name() { # An array to collect information about processes that were stopped. local stopped=() - openim::log::info "Stopping services with names: $*" + echo "Stopping services with names: $*" # 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. @@ -1810,7 +1851,7 @@ openim::util::stop_services_with_name() { # 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:" + echo "Services that couldn't be stopped:" for name in "${not_stopped[@]}"; do openim::log::status "Failed to stop the $name service." done @@ -1819,9 +1860,9 @@ openim::util::stop_services_with_name() { # Print information about services whose processes were successfully stopped. if [[ ${#stopped[@]} -ne 0 ]]; then echo - openim::log::info "Stopped services:" + echo "Stopped services:" for name in "${stopped[@]}"; do - openim::log::info "Successfully stopped the $name service." + echo "Successfully stopped the $name service." done fi @@ -2822,6 +2863,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 + + + NUM_PROCESSES=$(echo "$PIDS" | wc -l | xargs) + if [ "$NUM_PROCESSES" -gt 0 ]; then + all_stopped=false + echo "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 + echo "Processes for ${service} have not been stopped properly. " "$NUM_PROCESSES" + 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/list-feature-tests.sh b/scripts/list-feature-tests.sh index 438474bae..d6eaa4873 100755 --- a/scripts/list-feature-tests.sh +++ b/scripts/list-feature-tests.sh @@ -18,9 +18,9 @@ # # Usage: `scripts/list-feature-tests.sh`. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. grep "\[Feature:\w+\]" "${OPENIM_ROOT}"/test/e2e/**/*.go -Eoh | LC_ALL=C sort -u \ No newline at end of file diff --git a/scripts/release.sh b/scripts/release.sh index a34d5ee22..eb8b04359 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -47,9 +47,9 @@ # images and other build artifacts. # Build a OpenIM release. This script supports various flags for flexible execution control. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/common.sh" source "${OPENIM_ROOT}/scripts/lib/release.sh" diff --git a/scripts/run-in-gopath.sh b/scripts/run-in-gopath.sh index 9bb8ec882..6af986975 100755 --- a/scripts/run-in-gopath.sh +++ b/scripts/run-in-gopath.sh @@ -20,9 +20,9 @@ # the project. # Usage: `scripts/run-in-gopath.sh `. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh" diff --git a/scripts/start-all.sh b/scripts/start-all.sh index ca03f0c3c..9a98bfca3 100755 --- a/scripts/start-all.sh +++ b/scripts/start-all.sh @@ -16,70 +16,100 @@ #FIXME This script is the startup script for multiple servers. #FIXME The full names of the shell scripts that need to be started are placed in the `need_to_start_server_shell` array. -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 start all openim service scripts" -set +o errexit -openim::golang::check_openim_binaries -if [[ $? -ne 0 ]]; then - openim::log::error "OpenIM binaries are not found. Please run 'make build' to build binaries." - "${OPENIM_ROOT}"/scripts/build-all-service.sh -fi -set -o errexit - -"${OPENIM_ROOT}"/scripts/init-config.sh --skip +OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. +source "${OPENIM_ROOT}/scripts/install/common.sh" -echo "You need to start the following scripts in order: ${OPENIM_SERVER_SCRIPTARIES[@]}" -openim::log::install_errexit # Function to execute the scripts. -function execute_scripts() { +function execute_start_scripts() { for script_path in "${OPENIM_SERVER_SCRIPT_START_LIST[@]}"; do # Extract the script name without extension for argument generation. script_name_with_prefix=$(basename "$script_path" .sh) - + # Remove the "openim-" prefix. script_name=${script_name_with_prefix#openim-} - + # Construct the argument based on the script name. arg="openim::${script_name}::start" - + # Check if the script file exists and is executable. if [[ -x "$script_path" ]]; then - openim::log::status "Starting script: ${script_path##*/}" # Log the script name. - + openim::log::info "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 } + + + + + +openim::golang::check_openim_binaries +if [[ $? -ne 0 ]]; then + openim::log::error "OpenIM binaries are not found. Please run 'make build' to build binaries." + "${OPENIM_ROOT}"/scripts/build-all-service.sh +fi + + +"${OPENIM_ROOT}"/scripts/init-config.sh --skip + +echo "You need to start the following scripts in order: ${OPENIM_SERVER_SCRIPTARIES[@]}" + + # TODO Prelaunch tools, simple for now, can abstract functions later TOOLS_START_SCRIPTS_PATH=${START_SCRIPTS_PATH}/openim-tools.sh -openim::log::info "\n## Pre Starting OpenIM services" +openim::log::status "\n## Pre Starting OpenIM services" ${TOOLS_START_SCRIPTS_PATH} openim::tools::pre-start -openim::log::info "\n## Starting OpenIM services" -execute_scripts -openim::log::info "\n## Post Starting OpenIM services" +result=$("${OPENIM_ROOT}"/scripts/stop-all.sh) +if [[ $? -ne 0 ]]; then + echo "+++ cat openim log file >>> ${LOG_FILE}" + openim::log::error "Some programs have not exited; the start process is aborted .\n $result" + exit 1 +fi + + + +openim::log::status "\n## Starting openim scripts: " +execute_start_scripts + +sleep 2 + +result=$(. $(dirname ${BASH_SOURCE})/install/openim-msgtransfer.sh openim::msgtransfer::check) +if [[ $? -ne 0 ]]; then + echo "+++ cat openim log file >>> ${LOG_FILE}" + openim::log::error "The program may fail to start.\n $result" + exit 1 +fi + + +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 "The program may fail to start.\n $result" + exit 1 +fi + + +openim::log::info "\n## Post Starting openim services" ${TOOLS_START_SCRIPTS_PATH} openim::tools::post-start -openim::color::echo $COLOR_BLUE "✨ All OpenIM services have been successfully started!" \ No newline at end of file +openim::log::success "All openim services have been successfully started!" \ No newline at end of file diff --git a/scripts/stop-all.sh b/scripts/stop-all.sh index 2acb9cdc5..b2572f7d5 100755 --- a/scripts/stop-all.sh +++ b/scripts/stop-all.sh @@ -18,22 +18,43 @@ # 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" +openim::log::status "Begin to stop all openim service" -echo "++ Ready to stop port: ${OPENIM_SERVER_PORT_LISTARIES[@]}" +openim::log::status "Stop all processes in the path ${OPENIM_OUTPUT_HOSTBIN}" -openim::util::stop_services_on_ports ${OPENIM_SERVER_PORT_LISTARIES[@]} +openim::util::stop_services_with_name "${OPENIM_OUTPUT_HOSTBIN}" +# todo OPENIM_ALL_SERVICE_LIBRARIES -echo -e "\n++ Stop all processes in the path ${OPENIM_OUTPUT_HOSTBIN}" -openim::util::stop_services_with_name "${OPENIM_OUTPUT_HOSTBIN}" -openim::log::success "✨ All processes to be killed" \ No newline at end of file + +max_retries=15 +attempt=0 + +while [[ $attempt -lt $max_retries ]] +do + result=$(openim::util::check_process_names_for_stop) + + if [[ $? -ne 0 ]]; then + if [[ $attempt -ne 0 ]] ; then + echo "+++ cat openim log file >>> ${LOG_FILE} " $attempt + openim::log::error "stop process failed. continue waiting\n" "${result}" + fi + sleep 1 + ((attempt++)) + else + openim::log::success " All openim processes to be stopped" + exit 0 + fi +done + +openim::log::error "openim processes stopped failed" +exit 1 diff --git a/scripts/update-generated-docs.sh b/scripts/update-generated-docs.sh index c37c4a1f9..d48a4067b 100755 --- a/scripts/update-generated-docs.sh +++ b/scripts/update-generated-docs.sh @@ -18,9 +18,9 @@ # immediately before exporting docs. We do not want to check these documents in # by default. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh" diff --git a/scripts/update-yamlfmt.sh b/scripts/update-yamlfmt.sh index 90ec8aa62..24ec60de9 100755 --- a/scripts/update-yamlfmt.sh +++ b/scripts/update-yamlfmt.sh @@ -14,9 +14,9 @@ # limitations under the License. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh" diff --git a/scripts/verify-pkg-names.sh b/scripts/verify-pkg-names.sh index 1459992e9..7fce3d7ad 100755 --- a/scripts/verify-pkg-names.sh +++ b/scripts/verify-pkg-names.sh @@ -17,9 +17,9 @@ # This script verifies whether codes follow golang convention. # Usage: `scripts/verify-pkg-names.sh`. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh" diff --git a/scripts/verify-shellcheck.sh b/scripts/verify-shellcheck.sh index 8a5ad7321..c7e713d09 100755 --- a/scripts/verify-shellcheck.sh +++ b/scripts/verify-shellcheck.sh @@ -17,9 +17,9 @@ # This script lints each shell script by `shellcheck`. # Usage: `scripts/verify-shellcheck.sh`. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh" diff --git a/scripts/verify-spelling.sh b/scripts/verify-spelling.sh index 2c02dccf7..fa0852866 100755 --- a/scripts/verify-spelling.sh +++ b/scripts/verify-spelling.sh @@ -17,9 +17,9 @@ # working directory by client9/misspell package. # Usage: `scripts/verify-spelling.sh`. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. export OPENIM_ROOT diff --git a/scripts/verify-typecheck.sh b/scripts/verify-typecheck.sh index 62fca4049..c9b2aaf30 100755 --- a/scripts/verify-typecheck.sh +++ b/scripts/verify-typecheck.sh @@ -16,9 +16,9 @@ # This script does a fast type check of script srnetes code for all platforms. # Usage: `scripts/verify-typecheck.sh`. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh" diff --git a/scripts/verify-yamlfmt.sh b/scripts/verify-yamlfmt.sh index 3d0a0180d..3acbf457c 100755 --- a/scripts/verify-yamlfmt.sh +++ b/scripts/verify-yamlfmt.sh @@ -19,9 +19,9 @@ # # Usage: `scripts/verify-yamlfmt.sh`. -set -o errexit -set -o nounset -set -o pipefail + + + OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. source "${OPENIM_ROOT}/scripts/lib/init.sh"