# ============================================================================== # ProcessChaos.psm1 - Automated Process Termination and Self-Healing Verifier # ============================================================================== function Get-ClientProcessStatus { [CmdletBinding()] param() $serviceProc = Get-Process -Name "ClassNotify.Service" -ErrorAction SilentlyContinue | Select-Object -First 1 $wpfProc = Get-Process -Name "ClassNotify.Client" -ErrorAction SilentlyContinue | Select-Object -First 1 return [PSCustomObject]@{ ServiceRunning = ($null -ne $serviceProc) ServicePid = if ($serviceProc) { $serviceProc.Id } else { $null } WpfRunning = ($null -ne $wpfProc) WpfPid = if ($wpfProc) { $wpfProc.Id } else { $null } } } function Kill-WpfClientAndMeasureRecovery { [CmdletBinding()] param( [int]$MaxAllowedRecoverySeconds = 15 ) $result = [PSCustomObject]@{ TimestampUtc = (Get-Date).ToUniversalTime().ToString("o") Target = "WPF" OldPid = $null NewPid = $null KilledSuccessfully = $false RecoveredSuccessfully = $false RecoveryDurationSeconds = 0 Error = "None" } try { $proc = Get-Process -Name "ClassNotify.Client" -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $proc) { # Graceful wait up to 10s if previous recovery was just completing $waitSw = [System.Diagnostics.Stopwatch]::StartNew() while ($waitSw.Elapsed.TotalSeconds -lt 10) { Start-Sleep -Milliseconds 500 $proc = Get-Process -Name "ClassNotify.Client" -ErrorAction SilentlyContinue | Select-Object -First 1 if ($proc) { break } } } if (-not $proc) { $result.Error = "WPF process not running prior to kill" return $result } $result.OldPid = $proc.Id Stop-Process -Id $proc.Id -Force $result.KilledSuccessfully = $true $sw = [System.Diagnostics.Stopwatch]::StartNew() $recovered = $false while ($sw.Elapsed.TotalSeconds -lt $MaxAllowedRecoverySeconds) { Start-Sleep -Milliseconds 500 $newProc = Get-Process -Name "ClassNotify.Client" -ErrorAction SilentlyContinue | Where-Object { $_.Id -ne $result.OldPid } | Select-Object -First 1 if ($newProc) { # Verify window / process is responsive $result.NewPid = $newProc.Id $recovered = $true break } } $result.RecoveryDurationSeconds = [math]::Round($sw.Elapsed.TotalSeconds, 2) $result.RecoveredSuccessfully = $recovered if (-not $recovered) { $result.Error = "WPF client failed to auto-recover within ${MaxAllowedRecoverySeconds}s" } } catch { $result.Error = $_.Exception.Message } return $result } function Kill-WindowsServiceAndMeasureRecovery { [CmdletBinding()] param( [int]$MaxAllowedRecoverySeconds = 75 ) $result = [PSCustomObject]@{ TimestampUtc = (Get-Date).ToUniversalTime().ToString("o") Target = "Service" OldPid = $null NewPid = $null KilledSuccessfully = $false RecoveredSuccessfully = $false RecoveryDurationSeconds = 0 Error = "None" } try { $proc = Get-Process -Name "ClassNotify.Service" -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $proc) { $result.Error = "ClassNotify.Service process not running prior to kill" return $result } $result.OldPid = $proc.Id Stop-Process -Id $proc.Id -Force $result.KilledSuccessfully = $true $sw = [System.Diagnostics.Stopwatch]::StartNew() $recovered = $false # SCM should trigger recovery restart while ($sw.Elapsed.TotalSeconds -lt $MaxAllowedRecoverySeconds) { Start-Sleep -Milliseconds 500 $newProc = Get-Process -Name "ClassNotify.Service" -ErrorAction SilentlyContinue | Where-Object { $_.Id -ne $result.OldPid } | Select-Object -First 1 if ($newProc) { $result.NewPid = $newProc.Id $recovered = $true break } } $result.RecoveryDurationSeconds = [math]::Round($sw.Elapsed.TotalSeconds, 2) $result.RecoveredSuccessfully = $recovered if (-not $recovered) { $result.Error = "Windows Service failed to auto-recover within ${MaxAllowedRecoverySeconds}s" } } catch { $result.Error = $_.Exception.Message } return $result } Export-ModuleMember -Function Get-ClientProcessStatus, Kill-WpfClientAndMeasureRecovery, Kill-WindowsServiceAndMeasureRecovery