Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update the make stop script logic and fix the make stop don't stop all process's bug #1933

Closed
wants to merge 47 commits into from
Closed
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
cc9d460
fix: del the error output
luhaoling Feb 19, 2024
f0aa9d3
fix: fix the make stop output format error
luhaoling Feb 19, 2024
297e001
fix: fix the make stop script
luhaoling Feb 19, 2024
eec0a5b
fix: fix the script
luhaoling Feb 19, 2024
2711029
fix: add signal Exit code
luhaoling Feb 19, 2024
04ec141
fix: signal output update
luhaoling Feb 19, 2024
64a3666
fix: fix the script
luhaoling Feb 19, 2024
42ad29b
fix: fix the msgtranfer script
luhaoling Feb 19, 2024
50dec7b
fix: fix the port check script
luhaoling Feb 19, 2024
92743f7
fix: fix the script name
luhaoling Feb 19, 2024
d02dc7c
fix: del the unuser code
luhaoling Feb 19, 2024
ee30529
fix: fix the port script
luhaoling Feb 19, 2024
132f3b8
fix: fix the out format
luhaoling Feb 19, 2024
078ed80
fix: fix the error
luhaoling Feb 20, 2024
34ffcb5
fix: add check-all-by-signal1
luhaoling Feb 20, 2024
247743f
fix: add stop-all
luhaoling Feb 20, 2024
6aec72a
fix: del the error judge
luhaoling Feb 20, 2024
83ee01a
fix: fix the transfer output
luhaoling Feb 20, 2024
5fb5a1f
fix: fix the script
luhaoling Feb 20, 2024
5479c39
fix: fix the output format
luhaoling Feb 20, 2024
df20f98
fix: update the logs output format
luhaoling Feb 20, 2024
5888cf7
fix: del the unuse func
luhaoling Feb 20, 2024
d670230
fix: output red
luhaoling Feb 20, 2024
5724158
fix: update the for loop
luhaoling Feb 20, 2024
4c7fcb3
fix: fix the output error
luhaoling Feb 20, 2024
8b28fd6
fix: logs
luhaoling Feb 20, 2024
9cea45b
fix: logs
luhaoling Feb 20, 2024
53dbb1f
Merge branch 'openimsdk:main' into fix/make_stop
luhaoling Feb 20, 2024
7fff4ae
fix: fix the scripts
luhaoling Feb 20, 2024
8a45baf
fix: fix the script
luhaoling Feb 20, 2024
39042e2
Merge branch 'fix/make_stop' of github.com:luhaoling/open-im-server i…
luhaoling Feb 20, 2024
76c9767
fix: fix the stop script
luhaoling Feb 20, 2024
8e4836f
fix: fix the make stop script
luhaoling Feb 20, 2024
87c79c8
Merge branch 'openimsdk:main' into fix/make_stop
luhaoling Feb 21, 2024
6c22ccd
fix: add kill by name in script
luhaoling Feb 22, 2024
2ddf5d3
Merge branch 'openimsdk:main' into fix/make_stop
luhaoling Feb 22, 2024
07bd7e9
Merge branch 'fix/make_stop' of github.com:luhaoling/open-im-server i…
luhaoling Feb 22, 2024
e5983c0
fix: del the error chat
luhaoling Feb 22, 2024
bbb086f
fix: update the server name
luhaoling Feb 22, 2024
9c3cc64
fix: add check logic script
luhaoling Feb 22, 2024
5ec08d7
fix: del the repeat func
luhaoling Feb 22, 2024
6c4f914
fix: del the repeat func
luhaoling Feb 22, 2024
c2c8810
fix: fix the param error
luhaoling Feb 22, 2024
9318322
fix: fix the error
luhaoling Feb 22, 2024
93f2034
fix: fix the error
luhaoling Feb 22, 2024
d423fca
fix: move the check_by_signal to util
luhaoling Feb 22, 2024
1a6d500
fix: fix the error output
luhaoling Feb 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/openim-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 1 addition & 3 deletions internal/msggateway/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this err not handled?

这个错误一定会发生吗,为什么不需要做处理

}()
return hubServer.LongConnServer.Run(netDone)
}
5 changes: 2 additions & 3 deletions internal/msgtransfer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ import (
"context"
"errors"
"fmt"
util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"

"github.com/OpenIMSDK/tools/errs"
"github.com/OpenIMSDK/tools/log"

util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil"

"net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -144,7 +143,7 @@ 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()
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/startrpc/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions pkg/util/genutil/genutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package genutil

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -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)
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)
}
1 change: 1 addition & 0 deletions scripts/install/openim-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function openim::api::start_service() {
echo "Starting service with command: $cmd"

nohup $cmd >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" >&2) &


if [ $? -ne 0 ]; then
openim::log::error_exit "Failed to start ${binary_name} on port ${service_port}."
Expand Down
27 changes: 26 additions & 1 deletion scripts/install/openim-msgtransfer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ 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"

Expand Down Expand Up @@ -88,6 +87,32 @@ function openim::msgtransfer::check() {
fi
}

function openim::msgtransfer::check_by_signal() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain the purpose of adding this function?

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"

Expand Down
203 changes: 172 additions & 31 deletions scripts/lib/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,81 @@ openim::util::check_ports() {
fi
}


openim::util::check_ports_by_signal() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这也是

this ones too

# 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=()

# 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::error "\n### No stop ports:"
for info in "${started[@]}"; do
openim::log::error "$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."
else
openim::log::error "Have processes no stop."
fi
}

# set +o errexit
# Sample call for testing:
# openim::util::check_ports 10002 1004 12345 13306
Expand Down Expand Up @@ -476,24 +551,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
Expand All @@ -519,6 +592,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"
Expand Down Expand Up @@ -594,6 +696,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"
Expand Down Expand Up @@ -1708,25 +1852,22 @@ openim::util::stop_services_on_ports() {
local stopped=()

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.
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 -10 $pid; then
stopped+=($port)
else
not_stopped+=($port)
fi
done <<< "$info"
local info=$(lsof -i :$port -n -P | grep LISTEN || true)
if [[ -n $info ]]; then
local stopped_this_port=false
while read -r line; do
local pid=$(echo $line | awk '{print $2}')
if kill -15 "$pid" &> /dev/null; then
stopped+=("$port")
stopped_this_port=true
break # Jumping out of cycle m after successfully sending SIGTERM
fi
done <<< "$info"
if ! $stopped_this_port; then
not_stopped+=("$port")
fi
fi
done

# Print information about ports whose processes couldn't be stopped.
Expand Down
Loading
Loading