From ae538b48cfa1107f377af5793499fed00be165b9 Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 12 Jan 2018 21:30:49 +0100 Subject: [PATCH] Changes to SqlServerDsc - Now the helper function Restart-SqlService, after restarting the SQL Server service, does not return until it can connect to the SQL Server instance, and the instance returns status 'Online' (issue #1008). If it fails to connect within the timeout period (defaults to 120 seconds) it throws an error. --- CHANGELOG.md | 10 ++++++++ SqlServerDscHelper.psm1 | 52 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 353cfa40ce..52e6af47dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,11 @@ - Updated the security token for AppVeyor status badge in README.md. When we renamed the repository the security token was changed ([issue #1012](https://github.com/PowerShell/SqlServerDsc/issues/1012)). + - Now the helper function Restart-SqlService, after restarting the SQL Server + service, does not return until it can connect to the SQL Server instance, and + the instance returns status 'Online' ([issue #1008](https://github.com/PowerShell/SqlServerDsc/issues/1008)). + If it fails to connect within the timeout period (defaults to 120 seconds) it + throws an error. - Changes to SqlAlias - Fixed issue where exception was thrown if reg keys did not exist ([issue #949](https://github.com/PowerShell/SqlServerDsc/issues/949)). @@ -83,6 +88,11 @@ - Added a read-only parameter ServiceAccountName so that the service account name is correctly returned as a string ([issue #982](https://github.com/PowerShell/SqlServerDsc/issues/982)). - Added integration tests ([issue #980](https://github.com/PowerShell/SqlServerDsc/issues/980)). + - The timing issue that the resource returned before SQL Server service was + actually restarted has been solved by a change in the helper function + Restart-SqlService ([issue #1008](https://github.com/PowerShell/SqlServerDsc/issues/1008)). + Now Restart-SqlService waits for the instance to return status 'Online' or + throws an error saying it failed to connect within the timeout period. - Changes to SqlSetup - Added parameter `ASServerMode` to support installing Analysis Services in Multidimensional mode, Tabular mode and PowerPivot mode diff --git a/SqlServerDscHelper.psm1 b/SqlServerDscHelper.psm1 index 52c4015abf..3a289c62e6 100644 --- a/SqlServerDscHelper.psm1 +++ b/SqlServerDscHelper.psm1 @@ -842,6 +842,58 @@ function Restart-SqlService $_ | Start-Service } } + + Write-Verbose -Message ($script:localizedData.WaitingInstanceTimeout -f $SQLServer, $SQLInstanceName, $Timeout) + + $startJobScriptBlock = { + param + ( + [Parameter()] + [System.String] + $ServerName, + + [Parameter()] + [System.String] + $InstanceName, + + [Parameter()] + [System.String] + $ScriptRoot + ) + + Import-Module -Name (Join-Path -Path $ScriptRoot -ChildPath 'SqlServerDscHelper.psm1') + + do + { + # This call, if it fails, will take between ~9-10 seconds to return. + $serverObject = Connect-SQL -SQLServer $ServerName -SQLInstanceName $InstanceName -ErrorAction SilentlyContinue + if ($serverObject.Status -ne 'Online') + { + # Waiting 2 seconds to not hammer the SQL Server instance. + Start-Sleep -Seconds 2 + } + } until ($serverObject.Status -eq 'Online') + } + + $startJobResult = Start-Job -ScriptBlock $startJobScriptBlock -ArgumentList @( + $SQLServer + $SQLInstanceName + $PSScriptRoot + ) + + Wait-Job -Job $startJobResult -Timeout $Timeout + + if ($startJobResult.JobStateInfo.State -ne 'Completed') + { + # Output any verbose messages and error messages. + Receive-Job -Job $startJobResult + + # Make sure the job is stopped. + Stop-Job -Job $startJobResult + + $errorMessage = $script:localizedData.FailedToConnectToInstanceTimeout -f $SQLServer, $SQLInstanceName, $Timeout + New-InvalidOperationException -Message $errorMessage + } } <#