Spaces:
Running
Running
Normalize start script line endings
Browse files
start.py
CHANGED
|
@@ -1,55 +1,55 @@
|
|
| 1 |
-
"""Run the public Node API and private Python worker as one service."""
|
| 2 |
-
import os
|
| 3 |
-
from pathlib import Path
|
| 4 |
-
import signal
|
| 5 |
-
import subprocess
|
| 6 |
-
import sys
|
| 7 |
-
import time
|
| 8 |
-
|
| 9 |
-
from dotenv import load_dotenv
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def main():
|
| 13 |
-
root = Path(__file__).resolve().parent
|
| 14 |
-
load_dotenv(root / ".env")
|
| 15 |
-
port = int(os.environ.get("PORT", "4000"))
|
| 16 |
-
if not 1 <= port <= 65535 or port == 8000:
|
| 17 |
-
raise ValueError("PORT must be between 1 and 65535 and differ from AI port 8000")
|
| 18 |
-
env = dict(os.environ, PORT=str(port),
|
| 19 |
-
ANALYSIS_API_URL="http://127.0.0.1:8000",
|
| 20 |
-
BACKEND_INTERNAL_URL=f"http://127.0.0.1:{port}")
|
| 21 |
-
children = []
|
| 22 |
-
stopping = False
|
| 23 |
-
|
| 24 |
-
def stop(signum, frame):
|
| 25 |
-
nonlocal stopping
|
| 26 |
-
stopping = True
|
| 27 |
-
|
| 28 |
-
for sig in (signal.SIGINT, signal.SIGTERM):
|
| 29 |
-
signal.signal(sig, stop)
|
| 30 |
-
try:
|
| 31 |
-
children.append(subprocess.Popen([sys.executable, "-m", "uvicorn", "app:app",
|
| 32 |
-
"--host", "127.0.0.1", "--port", "8000", "--workers", "1"], cwd=root, env=env))
|
| 33 |
-
children.append(subprocess.Popen(["node", "dist/server.js"], cwd=root, env=env))
|
| 34 |
-
while not stopping:
|
| 35 |
-
for child in children:
|
| 36 |
-
if child.poll() is not None:
|
| 37 |
-
print(f"Backend process {child.pid} exited ({child.returncode}); stopping service", flush=True)
|
| 38 |
-
return child.returncode or 1
|
| 39 |
-
time.sleep(0.25)
|
| 40 |
-
return 0
|
| 41 |
-
finally:
|
| 42 |
-
for child in children:
|
| 43 |
-
if child.poll() is None:
|
| 44 |
-
child.terminate()
|
| 45 |
-
deadline = time.monotonic() + 15
|
| 46 |
-
for child in children:
|
| 47 |
-
try:
|
| 48 |
-
child.wait(timeout=max(0, deadline - time.monotonic()))
|
| 49 |
-
except subprocess.TimeoutExpired:
|
| 50 |
-
child.kill()
|
| 51 |
-
child.wait()
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
if __name__ == "__main__":
|
| 55 |
-
sys.exit(main())
|
|
|
|
| 1 |
+
"""Run the public Node API and private Python worker as one service."""
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import signal
|
| 5 |
+
import subprocess
|
| 6 |
+
import sys
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def main():
|
| 13 |
+
root = Path(__file__).resolve().parent
|
| 14 |
+
load_dotenv(root / ".env")
|
| 15 |
+
port = int(os.environ.get("PORT", "4000"))
|
| 16 |
+
if not 1 <= port <= 65535 or port == 8000:
|
| 17 |
+
raise ValueError("PORT must be between 1 and 65535 and differ from AI port 8000")
|
| 18 |
+
env = dict(os.environ, PORT=str(port),
|
| 19 |
+
ANALYSIS_API_URL="http://127.0.0.1:8000",
|
| 20 |
+
BACKEND_INTERNAL_URL=f"http://127.0.0.1:{port}")
|
| 21 |
+
children = []
|
| 22 |
+
stopping = False
|
| 23 |
+
|
| 24 |
+
def stop(signum, frame):
|
| 25 |
+
nonlocal stopping
|
| 26 |
+
stopping = True
|
| 27 |
+
|
| 28 |
+
for sig in (signal.SIGINT, signal.SIGTERM):
|
| 29 |
+
signal.signal(sig, stop)
|
| 30 |
+
try:
|
| 31 |
+
children.append(subprocess.Popen([sys.executable, "-m", "uvicorn", "app:app",
|
| 32 |
+
"--host", "127.0.0.1", "--port", "8000", "--workers", "1"], cwd=root, env=env))
|
| 33 |
+
children.append(subprocess.Popen(["node", "dist/server.js"], cwd=root, env=env))
|
| 34 |
+
while not stopping:
|
| 35 |
+
for child in children:
|
| 36 |
+
if child.poll() is not None:
|
| 37 |
+
print(f"Backend process {child.pid} exited ({child.returncode}); stopping service", flush=True)
|
| 38 |
+
return child.returncode or 1
|
| 39 |
+
time.sleep(0.25)
|
| 40 |
+
return 0
|
| 41 |
+
finally:
|
| 42 |
+
for child in children:
|
| 43 |
+
if child.poll() is None:
|
| 44 |
+
child.terminate()
|
| 45 |
+
deadline = time.monotonic() + 15
|
| 46 |
+
for child in children:
|
| 47 |
+
try:
|
| 48 |
+
child.wait(timeout=max(0, deadline - time.monotonic()))
|
| 49 |
+
except subprocess.TimeoutExpired:
|
| 50 |
+
child.kill()
|
| 51 |
+
child.wait()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
if __name__ == "__main__":
|
| 55 |
+
sys.exit(main())
|