Check for PID file prior to connecting to newly started web socket server

This commit is contained in:
Dion Moult
2024-07-30 17:28:24 +10:00
parent 60b007d28a
commit 70bc32b3df
3 changed files with 37 additions and 16 deletions
@@ -119,15 +119,26 @@ async def gantt(request):
return web.Response(text=html_content, content_type="text/html")
async def open_web_browser(app):
webbrowser.open(f"http://127.0.0.1:{sio_port}/")
async def on_startup(app):
pid_file = "running_pid.json"
if os.path.exists(pid_file):
with open(pid_file, "r") as f:
pids = json.load(f)
else:
pids = {}
pids[str(os.getpid())] = sio_port
with open(pid_file, "w") as f:
json.dump(pids, f, indent=4)
app.router.add_get("/", index)
app.router.add_get("/gantt", gantt)
app.router.add_static("/jsgantt/", path="../gantt", name="jsgantt")
app.router.add_static("/static/", path="./static", name="static")
app.on_startup.append(open_web_browser)
app.on_startup.append(on_startup)
def main():
+4
View File
@@ -6,6 +6,10 @@ def connect_websocket_server(web, port):
# check if port already has a server listening to it
if web.is_port_available(port):
web.start_websocket_server(port)
if web.has_started(port):
web.connect_websocket_server(port)
web.open_web_browser(port)
return
web.connect_websocket_server(port)
+19 -13
View File
@@ -22,6 +22,7 @@ import blenderbim.core.tool
import blenderbim.tool as tool
import ifcopenshell.api.sequence
from typing import Any, Dict, Optional
import time
import socket
import sys
import os
@@ -130,19 +131,6 @@ class Web(blenderbim.core.tool.Web):
env=env,
)
pid_file = os.path.join(webui_path, "running_pid.json")
if os.path.exists(pid_file):
with open(pid_file, "r") as f:
pids = json.load(f)
else:
pids = {}
pids[str(ws_process.pid)] = port
with open(pid_file, "w") as f:
json.dump(pids, f, indent=4)
cls.set_is_running(True)
@classmethod
@@ -230,6 +218,24 @@ class Web(blenderbim.core.tool.Web):
cls.set_is_running(False)
print("Websocket server terminated successfully")
@classmethod
def has_started(cls, port):
max_time = 5
start = time.time()
while True:
if time.time() - start > max_time:
return False
webui_path = os.path.join(bpy.context.scene.BIMProperties.data_dir, "webui")
pid_file = os.path.join(webui_path, "running_pid.json")
with open(pid_file, "r") as f:
try:
data = json.load(f)
if port in data.values():
return True
except:
pass
time.sleep(0.1)
@classmethod
def send_webui_data(
cls,