Skip to content
Snippets Groups Projects
Commit 53082c0a authored by Davison, Ben J (UG - Comp Sci & Elec Eng)'s avatar Davison, Ben J (UG - Comp Sci & Elec Eng)
Browse files

Delete run_application.py

parent cc414cbe
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
# run_all_services.py - Script to run all microservices simultaneously
import subprocess
import threading
import time
import os
import sys
import webbrowser
import signal
import atexit
# Store processes to terminate them properly on exit
processes = []
def run_service(command, service_name):
"""Run a service with the given command and track its process"""
print(f"Starting {service_name}...")
try:
# Using shell=True for Windows compatibility
process = subprocess.Popen(command, shell=True)
processes.append((process, service_name))
# Wait for process to complete (should not happen unless killed)
process.wait()
except Exception as e:
print(f"Error running {service_name}: {e}")
def clean_up():
"""Terminate all running processes and stop Docker services."""
print("\nShutting down all services...")
# Stop Docker Compose services
docker_services = [
("Users Service", "microservice-users"),
("Events Service", "microservice-events"),
("Reviews Service", "microservice-reviews"),
("Recommendation Service", "microservice-recommendation"),
("API Gateway", "api-gateway"),
("Frontend", "frontend")
]
for service_name, service_dir in docker_services:
print(f"Stopping {service_name}...")
try:
subprocess.run(f"cd {service_dir} && docker-compose down", shell=True, check=True)
except Exception as e:
print(f"Error stopping {service_name}: {e}")
# Terminate Python-based services
for process, service_name in processes:
print(f"Terminating {service_name}...")
try:
process.terminate()
process.wait(timeout=5) # Ensure process is stopped
except Exception as e:
print(f"Error terminating {service_name}: {e}")
print("All services shut down.")
def open_browser():
"""Open browser to the frontend after a short delay"""
time.sleep(5) # Give services a moment to start (increased from 3 to 5)
print("Opening browser to frontend...")
webbrowser.open("http://localhost:5173/")
def get_python_command():
"""Get the appropriate python command for the current system"""
# On some systems, 'python' points to Python 2 and 'python3' to Python 3
if sys.version_info[0] >= 3:
return 'python' if sys.platform.startswith('win') else 'python3'
return 'python'
if __name__ == "__main__":
# Register cleanup function to run on exit
atexit.register(clean_up)
signal.signal(signal.SIGINT, lambda sig, frame: sys.exit(0))
python_cmd = get_python_command()
# Define the directory structure - adjust these paths to match your setup
base_dir = os.path.dirname(os.path.abspath(__file__))
users_dir = os.path.join(base_dir, "microservice-users")
events_dir = os.path.join(base_dir, "microservice-events")
gateway_dir = os.path.join(base_dir, "api-gateway")
frontend_dir = os.path.join(base_dir, "frontend")
reviews_dir = os.path.join(base_dir, "microservice-reviews")
recommendation_dir = os.path.join(base_dir, "microservice-recommendation")
# Define commands for each service
commands = [
(f"cd {users_dir} && docker-compose up -d", "Users Service"),
(f"cd {events_dir} && docker compose up -d", "Events Service"),
# (f"cd {gateway_dir} && {python_cmd} -m uvicorn main:app --reload --port 8000", "API Gateway"),
(f"cd {gateway_dir} && docker compose up -d", "API Gateway"),
(f"cd {reviews_dir} && docker-compose up -d", "Reviews Service"),
(f"cd {recommendation_dir} && docker-compose up -d", "Recommendation Service"),
(f"cd {frontend_dir} && docker compose up -d", "Frontend")
]
# Create and start threads for each service
threads = []
for command, service_name in commands:
thread = threading.Thread(target=run_service, args=(command, service_name))
thread.daemon = True # Allow the program to exit even if threads are running
threads.append(thread)
thread.start()
time.sleep(7) # Increased to give each service more time to start
# Open browser after a short delay
browser_thread = threading.Thread(target=open_browser)
browser_thread.daemon = True
browser_thread.start()
print("All services started. Press Ctrl+C to stop all services.")
try:
# Keep the main thread alive to receive keyboard interrupts
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nReceived keyboard interrupt")
# The atexit handler will clean up processes
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment