Files
live-campus-mcs-p-2027.2/Semaine_08/orderflow/start_consumer.py
T
gauvainboiche 3315cb2336 feat: Semaine 8
2026-05-11 09:25:19 +02:00

89 lines
2.8 KiB
Python

"""
start_consumers.py — Lance les 3 consumers CommandFlow dans des fenêtres séparées.
Fonctionne sur Windows, macOS et Linux sans dépendance externe.
Usage :
uv run python start_consumers.py
"""
import subprocess
import sys
import os
import platform
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONSUMERS = [
("payment_service", "payment_service/consumer.py"),
("kitchen_service", "kitchen_service/consumer.py"),
("notif_service", "notif_service/consumer.py"),
("analytics_service", "analytics_service/consumer.py"),
]
def open_in_new_terminal(title: str, script_path: str) -> None:
"""Ouvre le script dans une nouvelle fenêtre de terminal, selon l'OS."""
uv = "uv"
cmd = f"uv run python {script_path}"
system = platform.system()
if system == "Windows":
# start /WAIT ouvre une nouvelle fenêtre cmd et la garde ouverte
subprocess.Popen(
["cmd", "/c", "start", f'"{title}"', "cmd", "/k", cmd],
cwd=BASE_DIR,
shell=False,
)
elif system == "Darwin": # macOS
# osascript pilote Terminal.app
apple_script = (
f'tell application "Terminal" to activate\n'
f'tell application "Terminal" to do script '
f'"cd \\"{BASE_DIR}\\" && echo \'=== {title} ===' + "'" +
f' && {cmd}"'
)
subprocess.Popen(["osascript", "-e", apple_script])
else: # Linux
# Essaie gnome-terminal, puis xterm en fallback
if _cmd_exists("gnome-terminal"):
subprocess.Popen(
["gnome-terminal", f"--title={title}", "--",
"bash", "-c", f"cd '{BASE_DIR}' && {cmd}; exec bash"],
)
elif _cmd_exists("xterm"):
subprocess.Popen(
["xterm", "-title", title, "-e",
f"cd '{BASE_DIR}' && {cmd}; exec bash"],
)
else:
print(f" ⚠ Impossible d'ouvrir un terminal pour {title}.")
print(f" Lance manuellement : {cmd}")
def _cmd_exists(name: str) -> bool:
import shutil
return shutil.which(name) is not None
def main():
print()
print("╔══════════════════════════════════════╗")
print("║ CommandFlow — démarrage consumers ║")
print(f"║ OS détecté : {platform.system():<23}")
print("╚══════════════════════════════════════╝")
print()
for title, script in CONSUMERS:
open_in_new_terminal(title, script)
print(f"{title}")
print()
print(" Consumers actifs. Lance le seed :")
print(" uv run python seed.py --delay 1.5")
print()
if __name__ == "__main__":
main()