diff --git a/AutoTx-Updater/Update-Service.ps1 b/AutoTx-Updater/Update-Service.ps1 index 6bea491fe379113ed946f0247298bf69d857bb7f..33da4c604507d186eed7ec81901f86c9b0ff7066 100644 --- a/AutoTx-Updater/Update-Service.ps1 +++ b/AutoTx-Updater/Update-Service.ps1 @@ -46,6 +46,28 @@ function Ensure-ServiceRunning([string]$ServiceName) { } +function ServiceIsBusy { + $StatusXml = "$($InstallationPath)\status.xml" + try { + [xml]$XML = Get-Content $StatusXml -ErrorAction Stop + if ($XML.ServiceStatus.TransferInProgress) { + Return $True + } else { + Write-Verbose "Service is idle, shutdown possible." + Return $False + } + } + catch { + $ex = $_.Exception.Message + $msg = "Trying to read the service status from [$($StatusXml)] failed! " + $msg += "The reported error message was:`n$($ex)" + Send-MailReport -Subject "Error parsing status of $($ServiceName)!" ` + -Body $msg + Exit + } +} + + function Exit-IfDirMissing([string]$DirName, [string]$Desc) { if (Test-Path -PathType Container $DirName) { Return @@ -75,6 +97,12 @@ function Stop-MyService([string]$Message) { Log-Info "$($Message) (Service already in state 'Stopped')" Return } + if (ServiceIsBusy) { + $msg = "*DENYING* to stop the service $($ServiceName) as it is " + $msg += "currently busy.`nShutdown reason was '$($Message)'." + Log-Info $msg + Exit + } try { Log-Info "$($Message) Attempting service $($ServiceName) shutdown..." Stop-Service "$($ServiceName)" -ErrorAction Stop