Harden Local Agent singleton supervisor and worker lifecycle
This commit is contained in:
@@ -26,7 +26,7 @@ ERROR_ALREADY_EXISTS = 183
|
||||
def _dashboard_mutex_name() -> str:
|
||||
normalized = str(INSTALL_ROOT.resolve()).casefold().encode("utf-8", errors="ignore")
|
||||
suffix = hashlib.sha256(normalized).hexdigest()[:20]
|
||||
return f"Local\\ARRR_ERP_Local_Agent_DASHBOARD_{suffix}"
|
||||
return f"Global\\ARRR_ERP_Local_Agent_DASHBOARD_{suffix}"
|
||||
|
||||
|
||||
def _acquire_dashboard_mutex():
|
||||
@@ -142,14 +142,35 @@ def _pythonw() -> Path:
|
||||
def _ensure_supervisor() -> None:
|
||||
if _supervisor_running():
|
||||
return
|
||||
subprocess.Popen(
|
||||
[str(_pythonw()), str(SUPERVISOR_SCRIPT), "--background"],
|
||||
cwd=str(INSTALL_ROOT),
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
creationflags=_create_no_window(),
|
||||
close_fds=True,
|
||||
)
|
||||
|
||||
# The Scheduled Task is the canonical owner/launcher. Starting it repeatedly
|
||||
# is safe because the task is configured with MultipleInstances=IgnoreNew,
|
||||
# and the supervisor itself also owns a machine-wide Global mutex.
|
||||
started_by_task = False
|
||||
if os.name == "nt":
|
||||
result = subprocess.run(
|
||||
["powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command",
|
||||
"Start-ScheduledTask -TaskName 'ERP Local Agent' -ErrorAction Stop"],
|
||||
cwd=str(INSTALL_ROOT),
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
creationflags=_create_no_window(),
|
||||
check=False,
|
||||
)
|
||||
started_by_task = result.returncode == 0
|
||||
|
||||
if not started_by_task:
|
||||
# Recovery fallback for a workstation where the scheduled task was not
|
||||
# installed yet. The Global mutex still prevents a second supervisor.
|
||||
subprocess.Popen(
|
||||
[str(_pythonw()), str(SUPERVISOR_SCRIPT), "--background"],
|
||||
cwd=str(INSTALL_ROOT),
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
creationflags=_create_no_window(),
|
||||
close_fds=True,
|
||||
)
|
||||
|
||||
deadline = time.time() + 20
|
||||
while time.time() < deadline:
|
||||
if _supervisor_running():
|
||||
|
||||
Reference in New Issue
Block a user