Skip to content

Commit 27eac58

Browse files
author
Matt Wilkinson
committed
Convert timeout processing to TimeSpan/Stopwatch
1 parent 45d9510 commit 27eac58

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

Microsoft.Xrm.Data.PowerShell/Microsoft.Xrm.Data.PowerShell.psm1

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,8 @@ function Import-CrmSolution{
21542154
throw LastCrmConnectorException($conn)
21552155
}
21562156
$isProcessing = $true
2157-
$secondsSpentPolling = 0
2157+
$timeSpentPolling = [System.Diagnostics.Stopwatch]::StartNew()
2158+
$timeout = New-TimeSpan -Seconds $MaxWaitTimeInSeconds
21582159
$pollingDelaySeconds = 5
21592160
$TopPrevProcPercent = [double]0
21602161
$isProcPercentReduced = $false
@@ -2163,7 +2164,8 @@ function Import-CrmSolution{
21632164
Write-Host "Import of file completed, waiting on completion of importId: $importId"
21642165
try{
21652166
Write-Progress -Id 1 -Activity "Importing Solution" -CurrentOperation "Solution file uploaded, import initiated" -PercentComplete -1
2166-
while($isProcessing -and $secondsSpentPolling -lt $MaxWaitTimeInSeconds){
2167+
2168+
while($isProcessing -and $timeSpentPolling.Elapsed -lt $timeout){
21672169

21682170
#check the import job for success/fail/inProgress
21692171
try{
@@ -2245,18 +2247,19 @@ function Import-CrmSolution{
22452247
break
22462248
}
22472249

2248-
Write-Progress -Id 2 -ParentId 1 -Activity "Maximum Wait Time ($(New-TimeSpan -Seconds $MaxWaitTimeInSeconds))" -CurrentOperation "Remaining Time: $(New-TimeSpan -Seconds ($MaxWaitTimeInSeconds - $secondsSpentPolling))" -PercentComplete ([double]($MaxWaitTimeInSeconds - $secondsSpentPolling) * 100 / $MaxWaitTimeInSeconds)
2250+
Write-Progress -Id 2 -ParentId 1 -Activity "Maximum Wait Time ($timeout)" -CurrentOperation "Remaining Time: $($timeout - $timeSpentPolling.Elapsed)" -PercentComplete ([double]($timeout - $timeSpentPolling.Elapsed).TotalSeconds * 100 / $timeout.TotalSeconds)
22492251
Start-Sleep -Seconds $pollingDelaySeconds
22502252
}
22512253
} Catch {
22522254
Write-Error "ImportJob with ID: $importId has encountered an exception: $_ "
22532255
} Finally{
22542256
$ProcPercent = ([double](Coalesce $ProcPercent 0))
22552257
Write-Progress -Id 2 -Completed -Activity "_"
2258+
$timeSpentPolling.Stop()
22562259
}
22572260
#User provided timeout and exit function with an error
2258-
if($secondsSpentPolling -gt $MaxWaitTimeInSeconds){
2259-
throw "Import-CrmSolution halted due to exceeding the maximum timeout of $MaxWaitTimeInSeconds."
2261+
if($timeSpentPolling.Elapsed -gt $timeout){
2262+
throw "Import-CrmSolution halted due to exceeding the maximum timeout of $timeout."
22602263
}
22612264
#detect a failure by a failure result OR the percent being less than 100%
22622265
if(($importManifest.result.result -eq "failure") -or ($ProcPercent -lt 100) -or $anyFailuresInImport) #Must look at %age instead of this result as the result is usually wrong!

0 commit comments

Comments
 (0)