feat(dlp): graceful shutdown and COM cleanup for email collector

This commit is contained in:
igor04091968
2026-05-07 23:52:03 +03:00
parent ac59d44719
commit b992ad2234
+51 -31
View File
@@ -610,45 +610,65 @@ if ($useOutlook) {
# Main loop # Main loop
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
while ($true) { try {
try { while ($true) {
Flush-Wal try {
Write-CollectorHealth -Status 'running' Flush-Wal
if (-not $script:Policy.defaults.enabled) { Write-CollectorHealth -Status 'running'
Start-Sleep -Seconds $resolvedPollSeconds if (-not $script:Policy.defaults.enabled) {
continue Start-Sleep -Seconds $resolvedPollSeconds
} continue
if ($useOutlook) {
if (-not $outlookReady) {
$outlookReady = Initialize-OutlookCom
} }
if ($outlookReady) {
if ($useOutlook) {
if (-not $outlookReady) {
$outlookReady = Initialize-OutlookCom
}
if ($outlookReady) {
try {
Poll-OutlookSentItems
}
catch {
Write-CollectorLog ("outlook poll error: {0}" -f $_.Exception.Message)
$outlookReady = $false
$script:OutlookApp = $null
$script:OutlookNamespace = $null
$script:SentFolder = $null
}
}
}
if ($useSmtp) {
try { try {
Poll-OutlookSentItems Poll-SmtpConnections
} }
catch { catch {
Write-CollectorLog ("outlook poll error: {0}" -f $_.Exception.Message) Write-CollectorLog ("smtp poll error: {0}" -f $_.Exception.Message)
$outlookReady = $false
$script:OutlookApp = $null
$script:OutlookNamespace = $null
$script:SentFolder = $null
} }
} }
} }
catch {
Write-CollectorLog ("collector error: {0}" -f $_.Exception.Message)
}
if ($useSmtp) { Start-Sleep -Seconds $resolvedPollSeconds
try { }
Poll-SmtpConnections }
} finally {
catch { Write-CollectorHealth -Status 'stopped'
Write-CollectorLog ("smtp poll error: {0}" -f $_.Exception.Message) try {
} if ($null -ne $script:SentFolder) {
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($script:SentFolder)
$script:SentFolder = $null
}
if ($null -ne $script:OutlookNamespace) {
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($script:OutlookNamespace)
$script:OutlookNamespace = $null
}
if ($null -ne $script:OutlookApp) {
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($script:OutlookApp)
$script:OutlookApp = $null
} }
} }
catch { catch {}
Write-CollectorLog ("collector error: {0}" -f $_.Exception.Message)
}
Start-Sleep -Seconds $resolvedPollSeconds
} }