From a8c796b049b285b6272f4e8626e685aa98693c88 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sat, 21 Dec 2024 18:56:43 +0100 Subject: [PATCH] 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. --- .gitignore | 3 +- README.md | 4 +- helpapp-backend/manage-services.sh | 31 ------------ service.sh | 80 ++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 34 deletions(-) delete mode 100755 helpapp-backend/manage-services.sh create mode 100755 service.sh diff --git a/.gitignore b/.gitignore index 8180423..c8eee9a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target/ -**/.env \ No newline at end of file +**/.env +*.tmp \ No newline at end of file diff --git a/README.md b/README.md index 6650793..1e89de3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/helpapp-backend/manage-services.sh b/helpapp-backend/manage-services.sh deleted file mode 100755 index 0d0f4e6..0000000 --- a/helpapp-backend/manage-services.sh +++ /dev/null @@ -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 \ No newline at end of file diff --git a/service.sh b/service.sh new file mode 100755 index 0000000..353e27a --- /dev/null +++ b/service.sh @@ -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 \ No newline at end of file