Skip to content

Commit

Permalink
Optimize logs, standardize uniformly (#439)
Browse files Browse the repository at this point in the history
* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script

* Optimize the script
  • Loading branch information
skiffer-git authored Mar 6, 2024
1 parent 18d5dde commit 59d8835
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 45 deletions.
2 changes: 1 addition & 1 deletion cmd/api/admin-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func main() {
go func() {
err = server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
netErr = errs.Wrap(err, fmt.Sprintf("api start err: %s", server.Addr))
netErr = errs.Wrap(err, fmt.Sprintf("server addr: %s", server.Addr))
netDone <- struct{}{}
}
}()
Expand Down
2 changes: 1 addition & 1 deletion cmd/api/chat-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func main() {
go func() {
err = server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
netErr = errs.Wrap(err, fmt.Sprintf("api start err: %s", server.Addr))
netErr = errs.Wrap(err, fmt.Sprintf("Server address: %s", server.Addr))
netDone <- struct{}{}
}
}()
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/OpenIMSDK/chat

go 1.21
toolchain go1.21.2


require (
github.com/gin-gonic/gin v1.9.1
Expand All @@ -26,7 +28,7 @@ require (

require (
github.com/OpenIMSDK/protocol v0.0.21
github.com/OpenIMSDK/tools v0.0.36
github.com/OpenIMSDK/tools v0.0.37
github.com/livekit/protocol v1.10.1
github.com/redis/go-redis/v9 v9.5.1
github.com/xuri/excelize/v2 v2.8.0
Expand Down
50 changes: 48 additions & 2 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/common/chatrpcstart/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(c
var zkClient discoveryregistry.SvcDiscoveryRegistry
zkClient, err := discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery)
if err != nil {
return errs.Wrap(err, fmt.Sprintf(";the addr is:%v", &config.Config.Zookeeper.ZkAddr))
return errs.Wrap(err, fmt.Sprintf("zookeeper addr: %v", &config.Config.Zookeeper.ZkAddr))
}
// defer zkClient.CloseZK()
defer zkClient.Close()
Expand All @@ -57,7 +57,7 @@ func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(c
rpcTcpAddr := net.JoinHostPort(network.GetListenIP(config.Config.Rpc.ListenIP), strconv.Itoa(rpcPort))
listener, err := net.Listen("tcp", rpcTcpAddr)
if err != nil {
return errs.Wrap(err)
return errs.Wrap(err, fmt.Sprintf("Server address: %s", rpcTcpAddr))
}
defer listener.Close()

Expand Down
8 changes: 4 additions & 4 deletions pkg/util/genutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func OutDir(path string) (string, error) {
}

func ExitWithError(err error) {
progName := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "%s exit -1: %+v\n", progName, err)
programName := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "%s exit -1: %+v\n\n", programName, err)
os.Exit(-1)
}

func SIGTERMExit() {
progName := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "Warning %s receive process terminal SIGTERM exit 0\n", progName)
programName := filepath.Base(os.Args[0])
fmt.Fprintf(os.Stderr, "Warning %s receive process terminal SIGTERM exit 0\n", programName)
}
11 changes: 6 additions & 5 deletions scripts/check-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ OPENIM_ROOT=$(dirname "${SCRIPTS_ROOT}")/..
DATA="$(date +%H:%M:%S)"
echo "# Start Chat check-all.sh ${DATA}, For local deployments, use ./check-all.sh --print-screen"

# 检查第一个参数是否为 --print-screen
# --print-screen
if [ "$1" == "--print-screen" ]; then
PRINT_SCREEN=1
fi
Expand All @@ -44,21 +44,22 @@ source $SCRIPTS_ROOT/util.sh


all_services_running=true
not_running_count=0 # Initialize a counter for not running services

for binary_path in "${binary_full_paths[@]}"; do
result=$(check_services_with_name "$binary_path")
if [ $? -ne 0 ]; then
all_services_running=false
not_running_count=$((not_running_count + 1)) # Increment the counter
# Print the binary path in red for not running services
echo -e "\033[0;31mService not running: $binary_path\033[0m"
fi
done

if $all_services_running; then
# Print "Startup successful" in green
echo -e "\033[0;32m all chat services Startup successful\033[0m"
echo -e "\033[0;32mAll chat services startup successful\033[0m"
else
echo -e "\033[0;31mOne or more chat services are not running.\033[0m"
# Print the number of services that are not running
echo -e "\033[0;31m$not_running_count chat service(s) are not running.\033[0m"
fi


51 changes: 34 additions & 17 deletions scripts/start-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ check_and_stop_services() {

# Step 1: Check and stop each service if running
for service in "${services[@]}"; do
stop_services_with_name "$service" >/dev/null 2>&1
check_services_with_name "$service" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Service running: $service. Attempting to stop."
stop_services_with_name "$service"
Expand Down Expand Up @@ -127,7 +127,7 @@ exit_status=$?

# Check the exit status and proceed accordingly
if [ $exit_status -eq 0 ]; then
echo "Execution can continue."
print_blue "Start component check for Chat services."
else
echo "Exiting due to failure in stopping services."
exit 1
Expand All @@ -136,9 +136,6 @@ fi






# Automatically created when there is no bin, logs folder
if [ ! -d $logs_dir ]; then
mkdir -p $logs_dir
Expand All @@ -150,17 +147,30 @@ 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)
${cmd} >> "${LOG_FILE}" 2> >(tee -a "$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"
echo -e "\033[32mAll components checked successful\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



print_blue "Starting MySQL to MongoDB data conversion"
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)
${cmd} >> "${LOG_FILE}" 2> >(tee -a "$TMP_LOG_FILE" | while read line; do echo -e "\e[31m${line}\e[0m"; done >&2)
if [ $? -eq 0 ]; then
print_green "conversion successful"
else
print_red -e "Data conversion failed, program exiting"
exit 1
fi

print_blue "Starting Chat API and RPC services."


for ((i = 0; i < ${#service_filename[*]}; i++)); do

Expand All @@ -187,13 +197,13 @@ for ((i = 0; i < ${#service_filename[*]}; i++)); do
echo $cmd


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) &

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

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


sleep 1


all_services_running=true
Expand All @@ -207,9 +217,14 @@ for binary_path in "${binary_full_paths[@]}"; do
fi
done

is_all_running=false
if $all_services_running; then
# Print "Startup successful" in green
echo -e "\033[0;32mAll chat services startup successful\033[0m"
is_all_running=true
print_blue "All chat services have been started, now beginning to check if the ports are listening properly."
else
# Print "all stop" and exit with status code 1
exit 1
fi

all_ports_listening=true
Expand All @@ -223,19 +238,21 @@ ports=(
)



declare -a no_listen_ports=()

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

if $all_ports_listening; then
echo "successful"
if $all_ports_listening && $is_all_running; then
print_green "All chat services have started normally and the ports are listening properly"
else
echo "failed"
if [ ${#no_listen_ports[@]} -gt 0 ]; then
echo -e "\033[31mThe following ports are not listening: ${no_listen_ports[*]}\033[0m"
fi
fi


Expand Down
14 changes: 14 additions & 0 deletions scripts/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ check_services_with_port() {
fi
}

# Function to print text in yellow
print_blue() {
echo -e "\033[0;36m$1\033[0m"
}

# Function to print text in green
print_green() {
echo -e "\033[0;32m$1\033[0m"
}

# Function to print text in red
print_red() {
echo -e "\033[0;31m$1\033[0m"
}


# Example usage of check_services_with_name
Expand Down
11 changes: 5 additions & 6 deletions tools/mysql2mongo/internal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
gormmysql "gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -80,7 +79,7 @@ func NewTask[A interface{ TableName() string }, B any, C any](gormDB *gorm.DB, m
}
var count int
defer func() {
log.Printf("completed convert chat %s total %d\n", tableName, count)
fmt.Printf("completed convert chat %s total %d\n", tableName, count)
}()
const batch = 100
for page := 0; ; page++ {
Expand All @@ -105,7 +104,7 @@ func NewTask[A interface{ TableName() string }, B any, C any](gormDB *gorm.DB, m
if len(res) < batch {
return nil
}
log.Printf("current convert chat %s completed %d\n", tableName, count)
fmt.Printf("current convert chat %s completed %d\n", tableName, count)
}
}

Expand All @@ -128,12 +127,12 @@ func insertMany(coll *mongo.Collection, objs []any) error {
}

func Main(path string) error {
defer log.Println("mysql2mongo return")
defer fmt.Println("mysql2mongo return")
if err := InitConfig(path); err != nil {
return err
}
if config.Config.Mysql == nil {
log.Println("mysql config is nil")
fmt.Println("mysql config is nil")
return nil
}
mongoDB, err := dbconn.NewMongo()
Expand All @@ -159,7 +158,7 @@ func Main(path string) error {
if err := SetMongoDataVersion(mongoDB, version.Value); err != nil {
return err
}
log.Println("set chat version config")
fmt.Println("set chat version config")
return nil // database not exist
}
return err
Expand Down
10 changes: 4 additions & 6 deletions tools/mysql2mongo/mysql2mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package main

import (
"flag"
"log"

"fmt"
"github.com/OpenIMSDK/chat/pkg/util"
"github.com/OpenIMSDK/chat/tools/mysql2mongo/internal"
)

func main() {
var path string
flag.StringVar(&path, "c", "", "path config file")
flag.Parse()
log.SetFlags(log.Llongfile | log.Ldate | log.Ltime)
if err := internal.Main(path); err != nil {
log.Fatal("chat mysql2mongo error", err)
return
util.ExitWithError(err)
}
log.Println("chat mysql2mongo success!")
fmt.Println("chat mysql2mongo success!")
return
}

0 comments on commit 59d8835

Please sign in to comment.