service.sh: moved and renamed the script to run any service. Now run any services dynamically, does not output spring and checks for every connexion.

This commit is contained in:
Yohan Boujon 2024-12-21 18:56:43 +01:00
parent 22355b8b64
commit a8c796b049
4 changed files with 84 additions and 34 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
target/
**/.env
*.tmp

View file

@ -3,8 +3,8 @@ Course Exercice : Application to help others
```bash
./init.sh
mvn compile
mvn spring-boot:run
./service.sh compile
./service.sh start
```
## Roadmap

View file

@ -1,31 +0,0 @@
#!/bin/bash
# List of services
services=("user-service" "request-service" "volunteer-service" "feedback-service" "administration-service")
# Function to start services
start_services() {
for service in "${services[@]}"; do
echo "Starting $service..."
mvn spring-boot:run -pl $service &
done
}
# Function to stop services
stop_services() {
echo "Stopping all services..."
pkill -f 'mvn spring-boot:run'
for port in {8080..8085}; do
echo "Killing process on port $port..."
fuser -k $port/tcp
done
}
# Check command line arguments
if [ "$1" == "start" ]; then
start_services
elif [ "$1" == "stop" ]; then
stop_services
else
echo "Usage: $0 {start|stop}"
fi

80
service.sh Executable file
View file

@ -0,0 +1,80 @@
#!/bin/bash
source ./db/db.sh
SCRIPT_PATH=$(realpath "$0")
SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")
services=()
ports=()
list_services() {
base_port=8081
cd "$SCRIPT_DIR/helpapp-backend"
for dir in *-service; do
if [[ -d "$dir" ]]; then
services+=("$dir")
ports+=("$base_port")
((base_port++))
fi
done
if [[ ${#services[@]} -eq 0 ]]; then
echo -e $RED"Error: Found no microservices:\nAre you in the right directory ?"$RESET
exit 1
fi
}
check_services() {
while true; do
all_ports_ready=true
for port in "${ports[@]}"; do
if ! nc -z localhost "$port"; then
all_ports_ready=false
break
fi
done
if $all_ports_ready; then
break
fi
sleep 1
done
}
# Function to start services
start_services() {
echo "" > ../process.tmp
cd "$SCRIPT_DIR/helpapp-backend" || exit 1
for service in "${services[@]}"; do
echo "> Starting $service..."
mvn spring-boot:run -pl "$service" > /dev/null 2>&1 &
echo -e "$!" >> ../process.tmp
done
echo "> Waiting for the services to establish connection..."
check_services
echo -e $GREEN"All services are now running!"$RESET
}
# Function to stop services
stop_services() {
echo "> Stopping all services..."
pkill -f 'mvn spring-boot:run'
for port in "${ports[@]}"; do
echo -e "\t> Killing process on port $port..."
fuser -k $port/tcp
done
}
# Check command line arguments
if [ "$1" == "start" ]; then
list_services
start_services
elif [ "$1" == "stop" ]; then
list_services
stop_services
elif [ "$1" == "compile" ]; then
cd "$SCRIPT_DIR/helpapp-backend" || exit 1
mvn clean install
else
echo -e $RED"Usage: $0 {compile|start|stop}"$RESET
fi