lightman-agent 1.0.16 → 1.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lightman-agent",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "LIGHTMAN Agent - System-level daemon for museum display machines",
5
5
  "private": false,
6
6
  "type": "module",
@@ -2,30 +2,88 @@
2
2
  # Runs every 5 minutes via Task Scheduler.
3
3
  # Restarts the NSSM service if it's down. Checks Chrome kiosk health.
4
4
 
5
- $LogDir = "C:\ProgramData\Lightman\logs"
6
- $LogFile = Join-Path $LogDir "guardian.log"
7
- $ServiceName = "LightmanAgent"
8
- $NssmExe = "C:\ProgramData\Lightman\nssm\nssm.exe"
9
-
10
- function Write-GuardianLog($msg) {
11
- $ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
12
- try {
13
- if (-not (Test-Path $LogDir)) { New-Item -ItemType Directory -Force -Path $LogDir | Out-Null }
5
+ $LogDir = "C:\ProgramData\Lightman\logs"
6
+ $LogFile = Join-Path $LogDir "guardian.log"
7
+ $ServiceName = "LightmanAgent"
8
+ $NssmExe = "C:\ProgramData\Lightman\nssm\nssm.exe"
9
+ $InstallDir = "C:\Program Files\Lightman\Agent"
10
+ $ChromeDataDir = "C:\ProgramData\Lightman\chrome-kiosk"
11
+
12
+ function Write-GuardianLog($msg) {
13
+ $ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
14
+ try {
15
+ if (-not (Test-Path $LogDir)) { New-Item -ItemType Directory -Force -Path $LogDir | Out-Null }
14
16
  Add-Content -Path $LogFile -Value "[$ts] $msg" -ErrorAction SilentlyContinue
15
17
  if ((Get-Item $LogFile -ErrorAction SilentlyContinue).Length -gt 1MB) {
16
18
  $rotated = "$LogFile.old"
17
19
  if (Test-Path $rotated) { Remove-Item $rotated -Force }
18
20
  Rename-Item $LogFile $rotated -Force
19
21
  }
20
- } catch { }
21
- }
22
-
23
- try {
24
- # 1. Check LIGHTMAN service
25
- $svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
26
- if (-not $svc) {
27
- $svc = Get-Service -DisplayName "LIGHTMAN*" -ErrorAction SilentlyContinue | Select-Object -First 1
28
- }
22
+ } catch { }
23
+ }
24
+
25
+ function Get-LightmanNodeProcesses {
26
+ try {
27
+ @(Get-CimInstance Win32_Process -Filter "Name = 'node.exe'" -ErrorAction SilentlyContinue | Where-Object {
28
+ $_.CommandLine -and (
29
+ $_.CommandLine -like "*$InstallDir*" -or
30
+ $_.CommandLine -match 'dist\\index\.js|src\\index\.ts|cms-agent\.js'
31
+ )
32
+ })
33
+ } catch {
34
+ @()
35
+ }
36
+ }
37
+
38
+ function Stop-LightmanNodeProcesses {
39
+ $procs = Get-LightmanNodeProcesses
40
+ if (-not $procs -or $procs.Count -eq 0) {
41
+ Write-GuardianLog "No LIGHTMAN-owned node.exe process found"
42
+ return
43
+ }
44
+
45
+ foreach ($proc in $procs) {
46
+ try {
47
+ Stop-Process -Id $proc.ProcessId -Force -ErrorAction Stop
48
+ Write-GuardianLog "Stopped LIGHTMAN node.exe PID $($proc.ProcessId)"
49
+ } catch {
50
+ Write-GuardianLog "Failed to stop LIGHTMAN node.exe PID $($proc.ProcessId): $_"
51
+ }
52
+ }
53
+ }
54
+
55
+ function Get-LightmanShellMode {
56
+ $configPath = Join-Path $InstallDir "agent.config.json"
57
+ try {
58
+ if (-not (Test-Path $configPath)) { return $false }
59
+ $config = Get-Content $configPath -Raw | ConvertFrom-Json
60
+ return [bool]($config.kiosk.shellMode)
61
+ } catch {
62
+ return $false
63
+ }
64
+ }
65
+
66
+ function Get-LightmanChromeProcesses {
67
+ try {
68
+ @(Get-CimInstance Win32_Process -Filter "Name = 'chrome.exe'" -ErrorAction SilentlyContinue | Where-Object {
69
+ $_.CommandLine -and (
70
+ $_.CommandLine -like "*$ChromeDataDir*" -or
71
+ $_.CommandLine -like "*chrome-kiosk*"
72
+ )
73
+ })
74
+ } catch {
75
+ @()
76
+ }
77
+ }
78
+
79
+ try {
80
+ $shellMode = Get-LightmanShellMode
81
+
82
+ # 1. Check LIGHTMAN service
83
+ $svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
84
+ if (-not $svc) {
85
+ $svc = Get-Service -DisplayName "LIGHTMAN*" -ErrorAction SilentlyContinue | Select-Object -First 1
86
+ }
29
87
 
30
88
  if (-not $svc) {
31
89
  Write-GuardianLog "CRITICAL: Service not found!"
@@ -41,35 +99,51 @@ try {
41
99
  } else {
42
100
  Start-Service -Name $svc.Name -ErrorAction SilentlyContinue
43
101
  }
44
- Start-Sleep -Seconds 5
45
- $svc.Refresh()
46
- Write-GuardianLog "After restart: $($svc.Status)"
47
- }
48
- elseif ($svc.Status -in @('StartPending', 'StopPending')) {
49
- Write-GuardianLog "Service stuck in $($svc.Status). Force killing node.exe..."
50
- Get-Process -Name "node" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
51
- Start-Sleep -Seconds 3
52
- if (Test-Path $NssmExe) { & $NssmExe start $ServiceName 2>$null }
53
- else { Start-Service -Name $svc.Name -ErrorAction SilentlyContinue }
54
- Start-Sleep -Seconds 5
55
- $svc.Refresh()
56
- Write-GuardianLog "After force restart: $($svc.Status)"
57
- }
58
- }
59
-
60
- # 2. Check Chrome kiosk
61
- $chrome = Get-Process -Name "chrome" -ErrorAction SilentlyContinue
62
- if (-not $chrome) {
63
- $vbsPath = "C:\Program Files\Lightman\Agent\launch-kiosk.vbs"
64
- if (Test-Path $vbsPath) {
65
- Start-Sleep -Seconds 10
66
- $chromeRecheck = Get-Process -Name "chrome" -ErrorAction SilentlyContinue
67
- if (-not $chromeRecheck) {
68
- Write-GuardianLog "Chrome not running. Launching via VBS..."
69
- Start-Process "wscript.exe" -ArgumentList """$vbsPath""" -WindowStyle Hidden
70
- }
71
- }
72
- }
73
- } catch {
74
- Write-GuardianLog "Guardian error: $_"
75
- }
102
+ Start-Sleep -Seconds 5
103
+ $svc.Refresh()
104
+ Write-GuardianLog "After restart: $($svc.Status)"
105
+ }
106
+ elseif ($svc.Status -in @('StartPending', 'StopPending')) {
107
+ Write-GuardianLog "Service is $($svc.Status). Waiting before targeted restart..."
108
+ Start-Sleep -Seconds 15
109
+ $svc.Refresh()
110
+ if ($svc.Status -in @('StartPending', 'StopPending')) {
111
+ Write-GuardianLog "Service still stuck in $($svc.Status). Restarting LIGHTMAN service only."
112
+ Stop-LightmanNodeProcesses
113
+ Start-Sleep -Seconds 3
114
+ if (Test-Path $NssmExe) {
115
+ & $NssmExe restart $ServiceName 2>$null
116
+ } else {
117
+ Stop-Service -Name $svc.Name -Force -ErrorAction SilentlyContinue
118
+ Start-Sleep -Seconds 2
119
+ Start-Service -Name $svc.Name -ErrorAction SilentlyContinue
120
+ }
121
+ Start-Sleep -Seconds 5
122
+ $svc.Refresh()
123
+ Write-GuardianLog "After targeted restart: $($svc.Status)"
124
+ } else {
125
+ Write-GuardianLog "Service recovered during wait: $($svc.Status)"
126
+ }
127
+ }
128
+ }
129
+
130
+ # 2. Check Chrome kiosk
131
+ $chrome = Get-LightmanChromeProcesses
132
+ if (-not $chrome) {
133
+ if ($shellMode) {
134
+ Write-GuardianLog "LIGHTMAN kiosk Chrome not running, but shell mode is enabled. Shell will relaunch it."
135
+ } else {
136
+ $vbsPath = Join-Path $InstallDir "launch-kiosk.vbs"
137
+ if (Test-Path $vbsPath) {
138
+ Start-Sleep -Seconds 10
139
+ $chromeRecheck = Get-LightmanChromeProcesses
140
+ if (-not $chromeRecheck) {
141
+ Write-GuardianLog "LIGHTMAN Chrome not running. Launching via VBS..."
142
+ Start-Process "wscript.exe" -ArgumentList """$vbsPath""" -WindowStyle Hidden
143
+ }
144
+ }
145
+ }
146
+ }
147
+ } catch {
148
+ Write-GuardianLog "Guardian error: $_"
149
+ }
@@ -28,11 +28,72 @@ $NssmExe = "$NssmDir\nssm.exe"
28
28
  $ServiceName = "LightmanAgent"
29
29
  $GuardianTask = "LIGHTMAN Guardian"
30
30
  $KioskTask = "LIGHTMAN Kiosk Browser"
31
- $AgentTask = "LIGHTMAN Agent"
32
- $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
33
- $AgentDir = Split-Path -Parent $ScriptDir
34
-
35
- if (-not $Username) { $Username = $env:USERNAME }
31
+ $AgentTask = "LIGHTMAN Agent"
32
+ $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
33
+ $AgentDir = Split-Path -Parent $ScriptDir
34
+
35
+ if (-not $Username) { $Username = $env:USERNAME }
36
+
37
+ function Get-LightmanNodeProcesses {
38
+ try {
39
+ @(Get-CimInstance Win32_Process -Filter "Name = 'node.exe'" -ErrorAction SilentlyContinue | Where-Object {
40
+ $_.CommandLine -and (
41
+ $_.CommandLine -like "*$InstallDir*" -or
42
+ $_.CommandLine -like "*$AgentDir*" -or
43
+ $_.CommandLine -match 'dist\\index\.js|src\\index\.ts|cms-agent\.js'
44
+ )
45
+ })
46
+ } catch {
47
+ @()
48
+ }
49
+ }
50
+
51
+ function Stop-LightmanNodeProcesses {
52
+ $procs = Get-LightmanNodeProcesses
53
+ if (-not $procs -or $procs.Count -eq 0) {
54
+ Write-Host " No LIGHTMAN-owned node.exe processes found" -ForegroundColor DarkGray
55
+ return
56
+ }
57
+
58
+ foreach ($proc in $procs) {
59
+ try {
60
+ Stop-Process -Id $proc.ProcessId -Force -ErrorAction Stop
61
+ Write-Host " Stopped LIGHTMAN node.exe PID $($proc.ProcessId)" -ForegroundColor DarkGray
62
+ } catch {
63
+ Write-Host " Failed to stop LIGHTMAN node.exe PID $($proc.ProcessId): $_" -ForegroundColor Yellow
64
+ }
65
+ }
66
+ }
67
+
68
+ function Get-LightmanChromeProcesses {
69
+ try {
70
+ @(Get-CimInstance Win32_Process -Filter "Name = 'chrome.exe'" -ErrorAction SilentlyContinue | Where-Object {
71
+ $_.CommandLine -and (
72
+ $_.CommandLine -like "*$ChromeData*" -or
73
+ $_.CommandLine -like "*chrome-kiosk*"
74
+ )
75
+ })
76
+ } catch {
77
+ @()
78
+ }
79
+ }
80
+
81
+ function Stop-LightmanChromeProcesses {
82
+ $procs = Get-LightmanChromeProcesses
83
+ if (-not $procs -or $procs.Count -eq 0) {
84
+ Write-Host " No LIGHTMAN kiosk Chrome processes found" -ForegroundColor DarkGray
85
+ return
86
+ }
87
+
88
+ foreach ($proc in $procs) {
89
+ try {
90
+ Stop-Process -Id $proc.ProcessId -Force -ErrorAction Stop
91
+ Write-Host " Stopped LIGHTMAN Chrome PID $($proc.ProcessId)" -ForegroundColor DarkGray
92
+ } catch {
93
+ Write-Host " Failed to stop LIGHTMAN Chrome PID $($proc.ProcessId): $_" -ForegroundColor Yellow
94
+ }
95
+ }
96
+ }
36
97
 
37
98
  Write-Host ""
38
99
  Write-Host "=============================================" -ForegroundColor Cyan
@@ -70,11 +131,11 @@ foreach ($tn in @($AgentTask, $KioskTask, $GuardianTask)) {
70
131
  if ($t) { Stop-ScheduledTask -TaskName $tn -ErrorAction SilentlyContinue; Unregister-ScheduledTask -TaskName $tn -Confirm:$false -ErrorAction SilentlyContinue }
71
132
  }
72
133
 
73
- # Kill processes
74
- Write-Host "[0c] Killing node.exe and Chrome..." -ForegroundColor Yellow
75
- Get-Process -Name "node" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
76
- Get-Process -Name "chrome" -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue
77
- Start-Sleep -Seconds 2
134
+ # Kill only LIGHTMAN-owned processes
135
+ Write-Host "[0c] Stopping LIGHTMAN node/chrome processes only..." -ForegroundColor Yellow
136
+ Stop-LightmanNodeProcesses
137
+ Stop-LightmanChromeProcesses
138
+ Start-Sleep -Seconds 2
78
139
 
79
140
  # Remove old files (keep NSSM and logs)
80
141
  Write-Host "[0d] Removing old agent files..." -ForegroundColor Yellow