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

feat: check port use by another process or not when startup #4656

Merged
merged 10 commits into from
Apr 23, 2023
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Apollo 2.2.0
* [[Multi-Database Support][pg] Make JdbcUserDetailsManager compat with postgre](https://github.com/apolloconfig/apollo/pull/4790)
* [refactor(apollo logging): Simplify the default log path to `/opt/logs`](https://github.com/apolloconfig/apollo/pull/4833)
* [Add a configuration config-service.cache.key.ignore-case to control whether the cache key is case-sensitive](https://github.com/apolloconfig/apollo/pull/4820)
* [feat: check port use by another process or not when startup](https://github.com/apolloconfig/apollo/pull/4656)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/13?closed=1)
68 changes: 49 additions & 19 deletions apollo-adminservice/src/main/scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export APP_NAME=$SERVICE_NAME
PATH_TO_JAR=$SERVICE_NAME".jar"
SERVER_URL="http://localhost:$SERVER_PORT"

function checkPidAlive {
function getPid() {
pgrep -f $SERVICE_NAME
}

function checkPidAlive() {
for i in `ls -t $APP_NAME/$APP_NAME.pid 2>/dev/null`
do
read pid < $i
Expand All @@ -63,6 +67,22 @@ function checkPidAlive {
exit 1;
}

function existProcessUsePort() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]; then
nobodyiam marked this conversation as resolved.
Show resolved Hide resolved
true
else
false
fi
}

function isServiceRunning() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 $SERVER_URL/health | grep "UP")" != "" ]; then
true
else
false
fi
}

if [ "$(uname)" == "Darwin" ]; then
windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
Expand Down Expand Up @@ -136,12 +156,27 @@ fi
if [[ -n "$APOLLO_RUN_MODE" ]] && [[ "$APOLLO_RUN_MODE" == "Docker" ]]; then
exec $javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $PATH_TO_JAR
else
if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
# before running check there is another process use port or not
if existProcessUsePort; then
if isServiceRunning; then
echo "$(date) ==== $SERVICE_NAME is running already with port $SERVER_PORT, pid $(getPid)"
exit 0
else
echo "$(date) ==== $SERVICE_NAME failed to start. The port $SERVER_PORT already be in use by another process"
echo "maybe you can figure out which process use port $SERVER_PORT by following ways:"
echo "1. access http://change-to-this-machine-ip:$SERVER_PORT by browser"
echo "2. run command 'curl $SERVER_URL'"
echo "3. run command 'sudo netstat -tunlp | grep :$SERVER_PORT'"
echo "4. run command 'sudo lsof -nP -iTCP:$SERVER_PORT -sTCP:LISTEN'"
exit 1
fi
fi

printf "$(date) ==== Starting ==== \n"
printf "$(date) ==== $SERVICE_NAME Starting ==== \n"

if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
fi
ln $PATH_TO_JAR $SERVICE_NAME".jar"
chmod a+x $SERVICE_NAME".jar"
./$SERVICE_NAME".jar" start
Expand All @@ -159,24 +194,19 @@ else
declare -i total_time=0

printf "Waiting for server startup"
until [[ (( counter -ge max_counter )) || "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]];
until [[ (( counter -ge max_counter )) ]];
do
printf "."
counter+=1
sleep 5
counter+=1
total_time=$((counter*5))

checkPidAlive
if isServiceRunning; then
printf "\n$(date) Server started in $total_time seconds!\n"
exit 0;
fi
done

total_time=counter*5

if [[ (( counter -ge max_counter )) ]];
then
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi

printf "\n$(date) Server started in $total_time seconds!\n"

exit 0;
fi
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi
68 changes: 49 additions & 19 deletions apollo-configservice/src/main/scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export APP_NAME=$SERVICE_NAME
PATH_TO_JAR=$SERVICE_NAME".jar"
SERVER_URL="http://localhost:$SERVER_PORT"

function checkPidAlive {
function getPid() {
pgrep -f $SERVICE_NAME
}

function checkPidAlive() {
for i in `ls -t $APP_NAME/$APP_NAME.pid 2>/dev/null`
do
read pid < $i
Expand All @@ -63,6 +67,22 @@ function checkPidAlive {
exit 1;
}

function existProcessUsePort() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]; then
true
else
false
fi
}

function isServiceRunning() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 $SERVER_URL/health | grep "UP")" != "" ]; then
true
else
false
fi
}

if [ "$(uname)" == "Darwin" ]; then
windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
Expand Down Expand Up @@ -136,12 +156,27 @@ fi
if [[ -n "$APOLLO_RUN_MODE" ]] && [[ "$APOLLO_RUN_MODE" == "Docker" ]]; then
exec $javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $PATH_TO_JAR
else
if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
# before running check there is another process use port or not
if existProcessUsePort; then
if isServiceRunning; then
echo "$(date) ==== $SERVICE_NAME is running already with port $SERVER_PORT, pid $(getPid)"
exit 0
else
echo "$(date) ==== $SERVICE_NAME failed to start. The port $SERVER_PORT already be in use by another process"
echo "maybe you can figure out which process use port $SERVER_PORT by following ways:"
echo "1. access http://change-to-this-machine-ip:$SERVER_PORT by browser"
echo "2. run command 'curl $SERVER_URL'"
echo "3. run command 'sudo netstat -tunlp | grep :$SERVER_PORT'"
echo "4. run command 'sudo lsof -nP -iTCP:$SERVER_PORT -sTCP:LISTEN'"
exit 1
fi
fi

printf "$(date) ==== Starting ==== \n"
printf "$(date) ==== $SERVICE_NAME Starting ==== \n"

if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
fi
ln $PATH_TO_JAR $SERVICE_NAME".jar"
chmod a+x $SERVICE_NAME".jar"
./$SERVICE_NAME".jar" start
Expand All @@ -159,24 +194,19 @@ else
declare -i total_time=0

printf "Waiting for server startup"
until [[ (( counter -ge max_counter )) || "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]];
until [[ (( counter -ge max_counter )) ]];
do
printf "."
counter+=1
sleep 5
counter+=1
total_time=$((counter*5))

checkPidAlive
if isServiceRunning; then
printf "\n$(date) Server started in $total_time seconds!\n"
exit 0;
fi
done

total_time=counter*5

if [[ (( counter -ge max_counter )) ]];
then
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi

printf "\n$(date) Server started in $total_time seconds!\n"

exit 0;
fi
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi
68 changes: 49 additions & 19 deletions apollo-portal/src/main/scripts/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export APP_NAME=$SERVICE_NAME
PATH_TO_JAR=$SERVICE_NAME".jar"
SERVER_URL="http://localhost:$SERVER_PORT"

function checkPidAlive {
function getPid() {
pgrep -f $SERVICE_NAME
}

function checkPidAlive() {
for i in `ls -t $APP_NAME/$APP_NAME.pid 2>/dev/null`
do
read pid < $i
Expand All @@ -63,6 +67,22 @@ function checkPidAlive {
exit 1;
}

function existProcessUsePort() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]; then
true
else
false
fi
}

function isServiceRunning() {
if [ "$(curl -X GET --silent --connect-timeout 1 --max-time 2 $SERVER_URL/health | grep "UP")" != "" ]; then
true
else
false
fi
}

if [ "$(uname)" == "Darwin" ]; then
windows="0"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
Expand Down Expand Up @@ -136,12 +156,27 @@ fi
if [[ -n "$APOLLO_RUN_MODE" ]] && [[ "$APOLLO_RUN_MODE" == "Docker" ]]; then
exec $javaexe -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar $PATH_TO_JAR
else
if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
# before running check there is another process use port or not
if existProcessUsePort; then
if isServiceRunning; then
echo "$(date) ==== $SERVICE_NAME is running already with port $SERVER_PORT, pid $(getPid)"
exit 0
else
echo "$(date) ==== $SERVICE_NAME failed to start. The port $SERVER_PORT already be in use by another process"
echo "maybe you can figure out which process use port $SERVER_PORT by following ways:"
echo "1. access http://change-to-this-machine-ip:$SERVER_PORT by browser"
echo "2. run command 'curl $SERVER_URL'"
echo "3. run command 'sudo netstat -tunlp | grep :$SERVER_PORT'"
echo "4. run command 'sudo lsof -nP -iTCP:$SERVER_PORT -sTCP:LISTEN'"
exit 1
fi
fi

printf "$(date) ==== Starting ==== \n"
printf "$(date) ==== $SERVICE_NAME Starting ==== \n"

if [[ -f $SERVICE_NAME".jar" ]]; then
rm -rf $SERVICE_NAME".jar"
fi
ln $PATH_TO_JAR $SERVICE_NAME".jar"
chmod a+x $SERVICE_NAME".jar"
./$SERVICE_NAME".jar" start
Expand All @@ -159,24 +194,19 @@ else
declare -i total_time=0

printf "Waiting for server startup"
until [[ (( counter -ge max_counter )) || "$(curl -X GET --silent --connect-timeout 1 --max-time 2 --head $SERVER_URL | grep "HTTP")" != "" ]];
until [[ (( counter -ge max_counter )) ]];
do
printf "."
counter+=1
sleep 5
counter+=1
total_time=$((counter*5))

checkPidAlive
if isServiceRunning; then
printf "\n$(date) Server started in $total_time seconds!\n"
exit 0;
fi
done

total_time=counter*5

if [[ (( counter -ge max_counter )) ]];
then
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi

printf "\n$(date) Server started in $total_time seconds!\n"

exit 0;
fi
printf "\n$(date) Server failed to start in $total_time seconds!\n"
exit 1;
fi