Put webui PID file in a writable location

Currently a file called running_pid.json is written in the same folder
as sioserver.py which is icky. It also doesn't work on a system where
bonsai is read-only. This fix puts this file in a platform-suitable
location:

  Linux: ~/.cache/bonsai/running_pid.json
  Windows: %LOCALAPPDATA%\bonsai\Cache\running_pid.json
  macOS: ~/Library/Caches/bonsai/running_pid.json
This commit is contained in:
Bruno Postle
2025-10-08 23:56:15 +01:00
parent 97dd37b345
commit 219b7d0c24
2 changed files with 22 additions and 5 deletions
+12 -3
View File
@@ -15,6 +15,8 @@ import pystache
import json import json
import base64 import base64
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import platformdirs
from pathlib import Path
sio_port = 8080 # default port sio_port = 8080 # default port
@@ -233,10 +235,17 @@ async def demo(request):
return web.Response(text=html_content, content_type="text/html") return web.Response(text=html_content, content_type="text/html")
async def on_startup(app): def get_pid_file_path():
pid_file = "running_pid.json" """Get the path to the PID file in the user's cache directory."""
cache_dir = Path(platformdirs.user_cache_dir("bonsai"))
cache_dir.mkdir(parents=True, exist_ok=True)
return cache_dir / "running_pid.json"
if os.path.exists(pid_file):
async def on_startup(app):
pid_file = get_pid_file_path()
if pid_file.exists():
with open(pid_file, "r") as f: with open(pid_file, "r") as f:
pids = json.load(f) pids = json.load(f)
else: else:
+10 -2
View File
@@ -41,6 +41,7 @@ import socketio
import threading import threading
import queue import queue
import json import json
import platformdirs
from time import sleep from time import sleep
from pathlib import Path from pathlib import Path
import bonsai.core.sequence import bonsai.core.sequence
@@ -65,6 +66,13 @@ IFC_TASK_ATTRIBUTE_MAP = {
} }
def get_pid_file_path():
"""Get the path to the PID file in the user's cache directory."""
cache_dir = Path(platformdirs.user_cache_dir("bonsai"))
cache_dir.mkdir(parents=True, exist_ok=True)
return cache_dir / "running_pid.json"
class Web(bonsai.core.tool.Web): class Web(bonsai.core.tool.Web):
@classmethod @classmethod
def get_web_props(cls) -> WebProperties: def get_web_props(cls) -> WebProperties:
@@ -213,7 +221,7 @@ class Web(bonsai.core.tool.Web):
cls.disconnect_websocket_server() cls.disconnect_websocket_server()
# sleep(0.5) # sleep(0.5)
pid_file = tool.Blender.get_data_dir_path("webui") / "running_pid.json" pid_file = get_pid_file_path()
with open(pid_file, "r") as f: with open(pid_file, "r") as f:
pids = json.load(f) pids = json.load(f)
@@ -250,7 +258,7 @@ class Web(bonsai.core.tool.Web):
while True: while True:
if time.time() - start > max_time: if time.time() - start > max_time:
return False return False
pid_file = tool.Blender.get_data_dir_path("webui") / "running_pid.json" pid_file = get_pid_file_path()
try: try:
with open(pid_file, "r") as f: with open(pid_file, "r") as f:
data = json.load(f) data = json.load(f)