Skip to content

Commit

Permalink
Optimized start script for Deployment (#425)
Browse files Browse the repository at this point in the history
* fix: fix the component check logic

* fix: add graceful_exit in admin-api

* fix: fix the graceful exit logic

* fix: add rpc graceful logic

* fix: del the http port

* fix: add graceful logic in chat-api

* fix: update the func location

* Before starting the script, first check if the program has exited

* fix: fix the error

* fix: fix the error

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* fix: test the func loc

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* fix: fix the tools version

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* Before starting the script, first check if the program has exited

* ComponentCheck timeout

* component check timeout

* fix: update the component check

* fix: update the component check logic

* component check timeout

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

* component check

---------

Co-authored-by: luhaoling <[email protected]>
Co-authored-by: Brabem <[email protected]>
  • Loading branch information
3 people authored Mar 5, 2024
1 parent c640839 commit 81f7148
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 64 deletions.
2 changes: 0 additions & 2 deletions cmd/api/admin-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (

"github.com/OpenIMSDK/chat/pkg/discovery_register"

"github.com/OpenIMSDK/chat/tools/component"
"github.com/OpenIMSDK/tools/discoveryregistry"

mw2 "github.com/OpenIMSDK/chat/pkg/common/mw"
Expand Down Expand Up @@ -69,7 +68,6 @@ func main() {
if err := config.InitConfig(configFile); err != nil {
util.ExitWithError(err)
}
err = component.ComponentCheck()
if err != nil {
util.ExitWithError(err)
}
Expand Down
3 changes: 0 additions & 3 deletions cmd/api/chat-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
"github.com/OpenIMSDK/chat/pkg/discovery_register"
"github.com/OpenIMSDK/tools/discoveryregistry"

"github.com/OpenIMSDK/chat/tools/component"

mw2 "github.com/OpenIMSDK/chat/pkg/common/mw"
"github.com/OpenIMSDK/chat/pkg/common/version"

Expand Down Expand Up @@ -72,7 +70,6 @@ func main() {
if err != nil {
util.ExitWithError(err)
}
err = component.ComponentCheck()
if err != nil {
util.ExitWithError(err)
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/rpc/admin-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/OpenIMSDK/chat/pkg/common/chatrpcstart"
"github.com/OpenIMSDK/chat/pkg/common/version"
"github.com/OpenIMSDK/chat/tools/component"
"github.com/OpenIMSDK/tools/log"

"github.com/OpenIMSDK/chat/internal/rpc/admin"
Expand Down Expand Up @@ -55,7 +54,6 @@ func main() {
if err := config.InitConfig(configFile); err != nil {
util.ExitWithError(err)
}
err = component.ComponentCheck()
if err != nil {
util.ExitWithError(err)
}
Expand Down
2 changes: 0 additions & 2 deletions cmd/rpc/chat-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"time"

"github.com/OpenIMSDK/chat/pkg/common/chatrpcstart"
"github.com/OpenIMSDK/chat/tools/component"
"github.com/OpenIMSDK/tools/log"

"github.com/OpenIMSDK/chat/internal/rpc/chat"
Expand Down Expand Up @@ -54,7 +53,6 @@ func main() {
if config.Config.Envs.Discovery == "k8s" {
rpcPort = 80
}
err = component.ComponentCheck()
if err != nil {
util.ExitWithError(err)
}
Expand Down
22 changes: 22 additions & 0 deletions deployments/templates/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,25 @@ liveKit:
liveKitUrl: "ws://172.28.0.1:7880" # LIVEKIT_URL, LiveKit server address and port
key: "APIDXJxJeCL8haY" # LIVEKIT_API_KEY
secret: "ak1qulJ3nfXeflQHWBdmQDc4js4ueMc5OnxoORVJC2xA" # LIVEKIT_API_SECRET

# MongoDB configuration

# If uri is not empty, it will be used directly for the MongoDB connection.
# This is a complete MongoDB URI string.
# Example: mongodb://user:password@host1:port1,host2:port2/dbname?options

mongo:
uri: ''

# List of MongoDB server addresses.
# Used for constructing the MongoDB URI if 'uri' above is empty.
# For a standalone setup, specify the address of the single server.
# For a sharded cluster, specify the addresses of the Mongos servers.
# Example: [ '172.28.0.1:37017', '172.28.0.2:37017' ]
# Default MongoDB database name
# Maximum connection pool size
address: [ 172.28.0.1:37017 ]
database: openim_v3
username: openIM
password: openIM123
maxPoolSize: 100
12 changes: 6 additions & 6 deletions pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ var Config struct {
SlowThreshold *int `yaml:"slowThreshold"`
} `yaml:"mysql"`
Mongo struct {
Uri *string `yaml:"uri"`
Address *[]string `yaml:"address"`
Database *string `yaml:"database"`
Username *string `yaml:"username"`
Password *string `yaml:"password"`
MaxPoolSize *int `yaml:"maxPoolSize"`
Uri string `yaml:"uri"`
Address []string `yaml:"address"`
Database string `yaml:"database"`
Username string `yaml:"username"`
Password string `yaml:"password"`
MaxPoolSize int `yaml:"maxPoolSize"`
} `yaml:"mongo"`
Log struct {
StorageLocation *string `yaml:"storageLocation"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func readConfig(configFile string) ([]byte, error) {
func InitConfig(configFile string) error {
data, err := readConfig(configFile)
if err != nil {
return fmt.Errorf("read loacl config file error: %w", err)
return errs.Wrap(fmt.Errorf("read loacl config file error: %w", err), "configFile:"+configFile)
}

if err := yaml.NewDecoder(bytes.NewReader(data)).Decode(&Config); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions scripts/check-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ done

if $all_services_running; then
# Print "Startup successful" in green
echo -e "\033[0;32mStartup successful\033[0m"
echo -e "\033[0;32m all chat services Startup successful\033[0m"
else
echo -e "\033[0;31mOne or more services are not running.\033[0m"
echo -e "\033[0;31mOne or more chat services are not running.\033[0m"
fi


6 changes: 6 additions & 0 deletions scripts/path-info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ service_source_root=(
$OPENIM_ROOT/cmd/rpc/chat-rpc/
)

component_binary_full_path="${BIN_DIR}/component"

mysql2mongo_full_path="${BIN_DIR}/mysql2mongo"

#service filename
service_names=(
Expand All @@ -83,6 +86,7 @@ service_names=(




# Define the array to hold full paths
binary_full_paths=()

Expand All @@ -92,3 +96,5 @@ for service_name in "${service_names[@]}"; do
done




137 changes: 120 additions & 17 deletions scripts/start-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ OPENIM_ROOT=$(dirname "${SCRIPTS_ROOT}")/..
source $SCRIPTS_ROOT/style-info.sh
source $SCRIPTS_ROOT/path-info.sh
source $SCRIPTS_ROOT/function.sh

export SUPPRESS_OUTPUT=1
source $SCRIPTS_ROOT/util.sh

# if [ ! -d "${OPENIM_ROOT}/_output/bin/platforms" ]; then
Expand Down Expand Up @@ -63,7 +65,6 @@ service_filename=(
service_port_name=(
openImChatApiPort
openImAdminApiPort
#api port name
openImAdminPort
openImChatPort
)
Expand All @@ -72,26 +73,97 @@ service_prometheus_port_name=(

)


#!/bin/bash

# Reusing stop_services_with_name and check_services_with_name functions as provided

check_and_stop_services() {
local services=("$@")
local service_stopped=0
local attempts=0

# Step 1: Check and stop each service if running
for service in "${services[@]}"; do
stop_services_with_name "$service" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Service running: $service. Attempting to stop."
stop_services_with_name "$service"
fi
done


# Step 2: Verify all services are stopped, retry up to 15 times if necessary
while [ $attempts -lt 15 ]; do
service_stopped=1

for service in "${services[@]}"; do
result=$(check_services_with_name "$service")
if [ $? -eq 0 ]; then
service_stopped=0
break
fi
done
if [ $service_stopped -eq 1 ]; then
echo "All services have been successfully stopped."
return 0
fi

sleep 1
((attempts++))
done

if [ $service_stopped -eq 0 ]; then
echo "Failed to stop all services after 15 seconds."
return 1
fi
}



# Call the function with your full binary paths
check_and_stop_services "${binary_full_paths[@]}"
exit_status=$?

# Check the exit status and proceed accordingly
if [ $exit_status -eq 0 ]; then
echo "Execution can continue."
else
echo "Exiting due to failure in stopping services."
exit 1
fi







# Automatically created when there is no bin, logs folder
if [ ! -d $logs_dir ]; then
mkdir -p $logs_dir
fi
cd $SCRIPTS_ROOT

rm -rf ${logs_dir}/chat_tmp_$(date '+%Y%m%d').log
LOG_FILE=${logs_dir}/chat_$(date '+%Y%m%d').log
STDERR_LOG_FILE=${logs_dir}/chat_err_$(date '+%Y%m%d').log
TMP_LOG_FILE=${logs_dir}/chat_tmp_$(date '+%Y%m%d').log
cmd="${component_binary_full_path} --config_folder_path ${config_path}"
${cmd} >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2)
if [ $? -eq 0 ]; then
echo -e "\033[32mAll components checked successfully\033[0m"
# Add the commands that should be executed next if the binary component was successful
else
echo -e "\033[31mComponent check failed, program exiting\033[0m"
exit 1
fi

cmd=cmd="${mysql2mongo_full_path} -c ${config_path}"
${cmd} >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2)

for ((i = 0; i < ${#service_filename[*]}; i++)); do
rm -rf ${logs_dir}/chat_tmp_$(date '+%Y%m%d').log
#Check whether the service exists
# service_name="ps |grep -w ${service_filename[$i]} |grep -v grep"
# count="${service_name}| wc -l"
#
# if [ $(eval ${count}) -gt 0 ]; then
# pid="${service_name}| awk '{print \$2}'"
# echo "${service_filename[$i]} service has been started,pid:$(eval $pid)"
# echo "killing the service ${service_filename[$i]} pid:$(eval $pid)"
# #kill the service that existed
# kill -9 $(eval $pid)
# sleep 0.5
# fi

cd $SCRIPTS_ROOT

#Get the rpc port in the configuration file
Expand All @@ -113,12 +185,16 @@ for ((i = 0; i < ${#service_filename[*]}; i++)); do
cmd="$bin_dir/${service_filename[$i]} -port ${service_ports[$j]} --config_folder_path ${config_path}"
fi
echo $cmd
nohup $cmd >> ${logs_dir}/chat_$(date '+%Y%m%d').log 2> >(tee -a ${logs_dir}/chat_err_$(date '+%Y%m%d').log ${logs_dir}/chat_tmp_$(date '+%Y%m%d').log) &


nohup ${cmd} >> "${LOG_FILE}" 2> >(tee -a "${STDERR_LOG_FILE}" "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2) &


done
done


sleep 1


all_services_running=true

Expand All @@ -133,9 +209,36 @@ done

if $all_services_running; then
# Print "Startup successful" in green
echo -e "\033[0;32mStartup successful\033[0m"
echo -e "\033[0;32mAll chat services startup successful\033[0m"
fi

all_ports_listening=true


ports=(
$(sed -n 's/.*openImChatApiPort: \[\(.*\)\].*/\1/p' ${config_path})
$(sed -n 's/.*openImAdminApiPort: \[\(.*\)\].*/\1/p' ${config_path})
$(sed -n 's/.*openImAdminPort: \[\(.*\)\].*/\1/p' ${config_path})
$(sed -n 's/.*openImChatPort: \[\(.*\)\].*/\1/p' ${config_path})
)




for port in "${ports[@]}"; do
if ! check_services_with_port "$port"; then
all_ports_listening=false
break
fi
done

if $all_ports_listening; then
echo "successful"
else
echo "failed"
fi





Expand Down
4 changes: 2 additions & 2 deletions scripts/stop-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ done

if $all_services_stopped; then
# Print "All services stopped" in green to indicate success
echo -e "\033[0;32mAll services stopped\033[0m"
echo -e "\033[0;32mAll chat services stopped\033[0m"
else
# Print error message indicating not all services are stopped
echo -e "\033[0;31mError: Not all services have been stopped.\033[0m"
echo -e "\033[0;31mError: Not all chat services have been stopped.\033[0m"
fi


Loading

0 comments on commit 81f7148

Please sign in to comment.