-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.py
More file actions
55 lines (44 loc) · 1.48 KB
/
Copy pathruntime.py
File metadata and controls
55 lines (44 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import re
import shlex
import signal
import subprocess
import time
def service_port_variable(service):
normalized_service = re.sub(r"\W+", "_", service).upper()
return f"{normalized_service}_PORT"
def start_project(command, port, allocated_ports):
env = os.environ.copy()
env["PORT"] = str(port)
for service, allocated_port in allocated_ports.items():
env[service_port_variable(service)] = str(allocated_port)
process = subprocess.Popen(
shlex.split(command),
env=env,
start_new_session=True,
)
return process
def stop_projects(processes):
running_processes = [process for process in processes.values() if process.poll() is None]
for process in running_processes:
os.killpg(process.pid, signal.SIGTERM)
for process in running_processes:
try:
process.wait(timeout=5)
except subprocess.TimeoutExpired:
os.killpg(process.pid, signal.SIGKILL)
process.wait()
def wait_for_projects(processes):
try:
while True:
for service, process in processes.items():
return_code = process.poll()
if return_code is not None:
raise RuntimeError(
f"Service '{service}' exited unexpectedly with code {return_code}."
)
time.sleep(0.2)
except KeyboardInterrupt:
print("\nStopping services...")
finally:
stop_projects(processes)