lightman-agent 1.0.16 → 1.0.18

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.18",
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,100 @@ $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 Get-InstallerParentNodePid {
52
+ try {
53
+ $self = Get-CimInstance Win32_Process -Filter "ProcessId=$PID" -ErrorAction SilentlyContinue
54
+ if (-not $self) { return $null }
55
+
56
+ $parentPid = $self.ParentProcessId
57
+ if (-not $parentPid) { return $null }
58
+
59
+ $parent = Get-CimInstance Win32_Process -Filter "ProcessId=$parentPid" -ErrorAction SilentlyContinue
60
+ if ($parent -and $parent.Name -eq "node.exe") {
61
+ return [int]$parent.ProcessId
62
+ }
63
+ } catch {
64
+ return $null
65
+ }
66
+
67
+ return $null
68
+ }
69
+
70
+ function Stop-LightmanNodeProcesses {
71
+ param(
72
+ [int]$ExcludePid = 0
73
+ )
74
+
75
+ $procs = Get-LightmanNodeProcesses
76
+ if (-not $procs -or $procs.Count -eq 0) {
77
+ Write-Host " No LIGHTMAN-owned node.exe processes found" -ForegroundColor DarkGray
78
+ return
79
+ }
80
+
81
+ foreach ($proc in $procs) {
82
+ if ($ExcludePid -gt 0 -and $proc.ProcessId -eq $ExcludePid) {
83
+ Write-Host " Keeping installer parent node.exe PID $($proc.ProcessId)" -ForegroundColor DarkGray
84
+ continue
85
+ }
86
+
87
+ try {
88
+ Stop-Process -Id $proc.ProcessId -Force -ErrorAction Stop
89
+ Write-Host " Stopped LIGHTMAN node.exe PID $($proc.ProcessId)" -ForegroundColor DarkGray
90
+ } catch {
91
+ Write-Host " Failed to stop LIGHTMAN node.exe PID $($proc.ProcessId): $_" -ForegroundColor Yellow
92
+ }
93
+ }
94
+ }
95
+
96
+ function Get-LightmanChromeProcesses {
97
+ try {
98
+ @(Get-CimInstance Win32_Process -Filter "Name = 'chrome.exe'" -ErrorAction SilentlyContinue | Where-Object {
99
+ $_.CommandLine -and (
100
+ $_.CommandLine -like "*$ChromeData*" -or
101
+ $_.CommandLine -like "*chrome-kiosk*"
102
+ )
103
+ })
104
+ } catch {
105
+ @()
106
+ }
107
+ }
108
+
109
+ function Stop-LightmanChromeProcesses {
110
+ $procs = Get-LightmanChromeProcesses
111
+ if (-not $procs -or $procs.Count -eq 0) {
112
+ Write-Host " No LIGHTMAN kiosk Chrome processes found" -ForegroundColor DarkGray
113
+ return
114
+ }
115
+
116
+ foreach ($proc in $procs) {
117
+ try {
118
+ Stop-Process -Id $proc.ProcessId -Force -ErrorAction Stop
119
+ Write-Host " Stopped LIGHTMAN Chrome PID $($proc.ProcessId)" -ForegroundColor DarkGray
120
+ } catch {
121
+ Write-Host " Failed to stop LIGHTMAN Chrome PID $($proc.ProcessId): $_" -ForegroundColor Yellow
122
+ }
123
+ }
124
+ }
36
125
 
37
126
  Write-Host ""
38
127
  Write-Host "=============================================" -ForegroundColor Cyan
@@ -69,12 +158,13 @@ foreach ($tn in @($AgentTask, $KioskTask, $GuardianTask)) {
69
158
  $t = Get-ScheduledTask -TaskName $tn -ErrorAction SilentlyContinue
70
159
  if ($t) { Stop-ScheduledTask -TaskName $tn -ErrorAction SilentlyContinue; Unregister-ScheduledTask -TaskName $tn -Confirm:$false -ErrorAction SilentlyContinue }
71
160
  }
72
-
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
161
+
162
+ # Kill only LIGHTMAN-owned processes
163
+ Write-Host "[0c] Stopping LIGHTMAN node/chrome processes only..." -ForegroundColor Yellow
164
+ $installerParentNodePid = Get-InstallerParentNodePid
165
+ Stop-LightmanNodeProcesses -ExcludePid $installerParentNodePid
166
+ Stop-LightmanChromeProcesses
167
+ Start-Sleep -Seconds 2
78
168
 
79
169
  # Remove old files (keep NSSM and logs)
80
170
  Write-Host "[0d] Removing old agent files..." -ForegroundColor Yellow