Browse Source

Desktop shortcut (VBS launcher), supervisor PID lock, log rotation

master
Serge Shutov 2 weeks ago
parent
commit
af3cb231bc
  1. 2
      .gitignore
  2. 26
      scripts/create-desktop-shortcut.ps1
  3. 31
      scripts/launch-dsh-memory.vbs
  4. 21
      scripts/start-dsh-memory.cmd
  5. 27
      scripts/supervisor.js

2
.gitignore vendored

@ -4,6 +4,8 @@ node_modules/
bridge/config.json bridge/config.json
.vscode/ .vscode/
*.log *.log
*.pid
scripts/supervisor.old.log
memory/chat-log/ memory/chat-log/
memory/state.json memory/state.json
memory/state.json.tmp memory/state.json.tmp

26
scripts/create-desktop-shortcut.ps1

@ -0,0 +1,26 @@
# Creates a desktop shortcut "DSH Memory" that starts the bridge hidden and idempotently.
# Run once: powershell -NoProfile -ExecutionPolicy Bypass -File V:\Site\dsh-memory\scripts\create-desktop-shortcut.ps1
$ErrorActionPreference = 'Stop'
$scriptsDir = $PSScriptRoot
$root = Split-Path -Parent $scriptsDir
$vbs = Join-Path $scriptsDir 'launch-dsh-memory.vbs'
if (-not (Test-Path $vbs)) { throw "launch-dsh-memory.vbs not found: $vbs" }
$desktop = [Environment]::GetFolderPath('Desktop')
$lnk = Join-Path $desktop 'DSH Memory.lnk'
$node = Get-Command node -ErrorAction SilentlyContinue
$iconPath = if ($node) { $node.Source } else { "$env:SystemRoot\System32\shell32.dll" }
$ws = New-Object -ComObject WScript.Shell
$s = $ws.CreateShortcut($lnk)
$s.TargetPath = 'wscript.exe'
$s.Arguments = '"' + $vbs + '"'
$s.WorkingDirectory = $root
$s.IconLocation = "$iconPath,0"
$s.Description = 'Start DSH Memory bridge (silent, no duplicates)'
$s.WindowStyle = 1
$s.Save()
Write-Host "Shortcut created: $lnk"
Write-Host "Double-click it to start the bridge. If the bridge is already running, nothing happens."

31
scripts/launch-dsh-memory.vbs

@ -0,0 +1,31 @@
Option Explicit
' Hidden launcher for the DSH memory bridge. Double-click friendly: no params, no window.
' If the bridge is already listening on 8766 it exits silently. Otherwise it starts the
' supervisor hidden and redirects output to scripts\supervisor.log.
Dim fso, sh, scriptDir, root, logPath
Set fso = CreateObject("Scripting.FileSystemObject")
Set sh = CreateObject("WScript.Shell")
scriptDir = fso.GetParentFolderName(WScript.ScriptFullName)
root = fso.GetParentFolderName(scriptDir)
logPath = scriptDir & "\supervisor.log"
Function PortOpen()
Dim code
code = sh.Run("powershell -NoProfile -ExecutionPolicy Bypass -Command ""if (Get-NetTCPConnection -LocalPort 8766 -State Listen -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }""", 0, True)
PortOpen = (code = 0)
End Function
If PortOpen() Then
WScript.Quit 0
End If
sh.CurrentDirectory = root
sh.Run "cmd /c node """ & scriptDir & "\supervisor.js"" >> """ & logPath & """ 2>&1", 0, False
WScript.Sleep 5000
If Not PortOpen() Then
MsgBox "DSH Memory bridge did not start within 5 seconds." & vbCrLf & vbCrLf & _
"Log: " & logPath & vbCrLf & vbCrLf & _
"Possible causes: node not in PATH, port 8766 blocked, config missing.", 48, "DSH Memory"
End If

21
scripts/start-dsh-memory.cmd

@ -1,7 +1,9 @@
@echo off @echo off
rem dsh-memory - idempotent bridge starter rem dsh-memory - idempotent bridge starter (foreground, for manual/debug use)
rem For a silent desktop shortcut use launch-dsh-memory.vbs instead.
setlocal setlocal
set "ROOT=%~dp0.." set "ROOT=%~dp0.."
set "LOG=%ROOT%\scripts\supervisor.log"
if not exist "%ROOT%\bridge\config.json" ( if not exist "%ROOT%\bridge\config.json" (
echo [dsh-memory] bridge\config.json not found. Run scripts\pair.ps1 first. echo [dsh-memory] bridge\config.json not found. Run scripts\pair.ps1 first.
@ -12,10 +14,17 @@ if not exist "%ROOT%\bridge\config.json" (
powershell -NoProfile -Command "if (Get-NetTCPConnection -LocalPort 8766 -State Listen -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }" powershell -NoProfile -Command "if (Get-NetTCPConnection -LocalPort 8766 -State Listen -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
if %errorlevel%==0 ( if %errorlevel%==0 (
echo [dsh-memory] bridge already running on port 8766. echo [dsh-memory] bridge already running on port 8766.
) else ( exit /b 0
echo [dsh-memory] starting bridge...
start "dsh-memory bridge" /D "%ROOT%" cmd /c "title dsh-memory bridge && node scripts\supervisor.js & pause"
ping -n 3 127.0.0.1 >nul
powershell -NoProfile -Command "if (Get-NetTCPConnection -LocalPort 8766 -State Listen -ErrorAction SilentlyContinue) { Write-Host '[dsh-memory] bridge started.' } else { Write-Host '[dsh-memory] bridge NOT started. Is node in PATH? Check the bridge window.' }"
) )
rem Rotate log if it grew past 5 MB
if exist "%LOG%" (
for %%A in ("%LOG%") do if %%~zA GTR 5242880 (
if exist "%ROOT%\scripts\supervisor.old.log" del "%ROOT%\scripts\supervisor.old.log"
ren "%LOG%" supervisor.old.log
)
)
echo [dsh-memory] starting bridge (log: %LOG%)
node "%ROOT%\scripts\supervisor.js"
endlocal endlocal

27
scripts/supervisor.js

@ -38,6 +38,31 @@ function portOpen(port) {
const ts = () => new Date().toISOString(); const ts = () => new Date().toISOString();
let fails = 0; let fails = 0;
// PID lock: prevents a second supervisor from starting when the first is alive.
const LOCK = path.join(__dirname, '.supervisor.pid');
function pidAlive(pid) {
try { process.kill(pid, 0); return true; }
catch (e) { return e.code === 'EPERM'; }
}
function acquireLock() {
try {
const raw = fs.readFileSync(LOCK, 'utf8').trim();
const old = parseInt(raw, 10);
if (old && old !== process.pid && pidAlive(old)) {
console.log(ts() + ' [supervisor] another instance running (pid ' + old + '), exiting');
process.exit(0);
}
} catch {}
try { fs.writeFileSync(LOCK, String(process.pid), 'utf8'); }
catch (e) { console.warn(ts() + ' [supervisor] lock write failed: ' + (e && e.message)); }
}
function releaseLock() {
try {
const raw = fs.readFileSync(LOCK, 'utf8').trim();
if (parseInt(raw, 10) === process.pid) fs.unlinkSync(LOCK);
} catch {}
}
async function runOnce() { async function runOnce() {
const port = readPort(); const port = readPort();
const waitStart = Date.now(); const waitStart = Date.now();
@ -93,6 +118,8 @@ async function runOnce() {
setTimeout(runOnce, DELAY_MS); setTimeout(runOnce, DELAY_MS);
} }
process.on('exit', releaseLock);
process.on('SIGINT', () => { console.log(ts() + ' [supervisor] interrupted'); process.exit(0); }); process.on('SIGINT', () => { console.log(ts() + ' [supervisor] interrupted'); process.exit(0); });
process.on('SIGTERM', () => { console.log(ts() + ' [supervisor] terminated'); process.exit(0); }); process.on('SIGTERM', () => { console.log(ts() + ' [supervisor] terminated'); process.exit(0); });
acquireLock();
runOnce().catch((e) => { console.error(ts() + ' [supervisor] fatal: ' + (e && e.message)); process.exit(1); }); runOnce().catch((e) => { console.error(ts() + ' [supervisor] fatal: ' + (e && e.message)); process.exit(1); });

Loading…
Cancel
Save