Fix config save (SQLAlchemy JSON mutation), auto-rename project monitors
This commit is contained in:
+14
-5
@@ -437,9 +437,14 @@ def update_instance(instance_id: int, update: InstanceUpdate, user: dict = Depen
|
|||||||
if update.status is not None:
|
if update.status is not None:
|
||||||
inst.status = update.status
|
inst.status = update.status
|
||||||
if update.config is not None:
|
if update.config is not None:
|
||||||
current = inst.config or {}
|
# Must assign a NEW dict — SQLAlchemy won't detect in-place mutations on JSON columns
|
||||||
current.update(update.config)
|
new_config = {**(inst.config or {}), **update.config}
|
||||||
inst.config = current
|
inst.config = new_config
|
||||||
|
# Auto-rename instance if project_name is set (e.g. "Project Monitor - WSIT")
|
||||||
|
if new_config.get("project_name") and inst.catalog_id == "project-monitor":
|
||||||
|
inst.name = f"Project Monitor - {new_config['project_name']}"
|
||||||
|
from sqlalchemy.orm.attributes import flag_modified
|
||||||
|
flag_modified(inst, "config")
|
||||||
db.commit()
|
db.commit()
|
||||||
return {"id": inst.id, "status": "updated"}
|
return {"id": inst.id, "status": "updated"}
|
||||||
|
|
||||||
@@ -492,11 +497,15 @@ def trigger_instance(instance_id: int, user: dict = Depends(require_auth), db: S
|
|||||||
return {"status": "triggered", "message": f"Running {catalog_id} for {u.display_name}"}
|
return {"status": "triggered", "message": f"Running {catalog_id} for {u.display_name}"}
|
||||||
|
|
||||||
if catalog_id == "project-monitor":
|
if catalog_id == "project-monitor":
|
||||||
config_json = json.dumps(inst.config or {}).replace("'", "\\'")
|
# Write config to a temp file to avoid shell escaping issues
|
||||||
|
config_path = f"/tmp/pm_config_{instance_id}.json"
|
||||||
|
with open(config_path, "w") as f:
|
||||||
|
json.dump({"config": inst.config or {}, "user_id": user["user_id"], "instance_id": instance_id}, f)
|
||||||
cmd = ["python3", "-c",
|
cmd = ["python3", "-c",
|
||||||
f"import sys, json; sys.path.insert(0, '{agent_dir}'); "
|
f"import sys, json; sys.path.insert(0, '{agent_dir}'); "
|
||||||
|
f"d = json.load(open('{config_path}')); "
|
||||||
f"from project_monitor import run; "
|
f"from project_monitor import run; "
|
||||||
f"run(json.loads('{config_json}'), user_id={user['user_id']}, instance_id={instance_id})"]
|
f"run(d['config'], user_id=d['user_id'], instance_id=d['instance_id'])"]
|
||||||
subprocess.Popen(cmd, env=env, cwd=agent_dir)
|
subprocess.Popen(cmd, env=env, cwd=agent_dir)
|
||||||
return {"status": "triggered", "message": f"Running project monitor: {inst.name}"}
|
return {"status": "triggered", "message": f"Running project monitor: {inst.name}"}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user