Files
railwaysystem/scripts/qa/start_demo_clean_backend.py
T

117 lines
3.8 KiB
Python

import os
import subprocess
import sys
import time
import socket
from pathlib import Path
def is_port_listening(port, timeout=1.0):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(timeout)
return s.connect_ex(('127.0.0.1', port)) == 0
def kill_port(port):
try:
out = subprocess.check_output(["lsof", "-ti", f":{port}"], text=True).strip()
if out:
for pid_str in out.split():
print(f"Killing process {pid_str} on port {port}...")
subprocess.run(["kill", "-9", pid_str], check=False)
time.sleep(1)
except subprocess.CalledProcessError:
pass
def main():
repo_root = Path(__file__).resolve().parent.parent.parent
os.chdir(repo_root)
# 1. Kill any existing 8096 process
kill_port(8096)
# 2. Parse ~/.railwaysystem-qa.env safely
env = os.environ.copy()
env_file = Path.home() / ".railwaysystem-qa.env"
if env_file.exists():
with open(env_file, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line or line.startswith('#') or '=' not in line:
continue
k, v = line.split('=', 1)
k = k.strip()
v = v.strip().strip("'\"")
env[k] = v
print(f"Loaded credentials from {env_file.name}")
else:
print("Warning: ~/.railwaysystem-qa.env not found!")
# Explicit Demo Clean database configuration
db_url = "jdbc:kingbase8://127.0.0.1:14321/training_demo_clean?currentSchema=public,sys_catalog&useUnicode=true&characterEncoding=utf8"
db_user = "kingbase"
db_pass = "sict123456"
env["SPRING_DATASOURCE_DRUID_MASTER_URL"] = db_url
env["SPRING_DATASOURCE_DRUID_MASTER_USERNAME"] = db_user
env["SPRING_DATASOURCE_DRUID_MASTER_PASSWORD"] = db_pass
env["SERVER_PORT"] = "8096"
env["DMS_ENABLED"] = "false"
env["SPRING_REDIS_HOST"] = "127.0.0.1"
env["SPRING_REDIS_PORT"] = "6379"
log_path = repo_root / "backend-demo.log"
log_file = open(log_path, "w", encoding="utf-8")
cmd = [
"java",
"--add-opens=java.base/java.lang=ALL-UNNAMED",
"--add-opens=java.base/java.lang.reflect=ALL-UNNAMED",
"--add-opens=java.base/java.util=ALL-UNNAMED",
"-jar",
str(repo_root / "RuoYi-Vue/ruoyi-admin/target/ruoyi-admin.jar"),
"--server.port=8096",
f"--spring.datasource.druid.master.url={db_url}",
f"--spring.datasource.druid.master.username={db_user}",
f"--spring.datasource.druid.master.password={db_pass}",
"--spring.redis.host=127.0.0.1",
"--spring.redis.port=6379",
"--dms.enabled=false",
]
print("Starting RuoYi backend against training_demo_clean...")
proc = subprocess.Popen(
cmd,
cwd=repo_root,
env=env,
stdout=log_file,
stderr=subprocess.STDOUT,
start_new_session=True
)
print(f"Backend started with PID: {proc.pid}")
# Wait for port 8096 to become active
max_wait = 40
start_time = time.time()
listening = False
while time.time() - start_time < max_wait:
if proc.poll() is not None:
print(f"Backend process exited prematurely with code {proc.returncode}!")
with open(log_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
print("Last 30 lines of log:")
for l in lines[-30:]:
print(l, end='')
sys.exit(1)
if is_port_listening(8096):
listening = True
break
time.sleep(1)
if listening:
print(f"Backend successfully listening on port 8096! (PID {proc.pid})")
else:
print("Timed out waiting for port 8096.")
sys.exit(1)
if __name__ == '__main__':
main()