Skip to content

Commit

Permalink
SqlServerDsc: Fixes the timing issue with SqlServiceAccount (#1019)
Browse files Browse the repository at this point in the history
- 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.
- Changes to SqlServiceAccount
  - 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).
    Now Restart-SqlService waits for the instance to return status 'Online' or
    throws an error saying it failed to connect within the timeout period.
  • Loading branch information
johlju authored Jan 18, 2018
1 parent 54df342 commit 761d7d8
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 109 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- Fixed typo in comment-base help for helper function Test-AvailabilityReplicaSeedingModeAutomatic.
- Style cleanup in helper function tests.
- Changes to SqlAG
Expand Down Expand Up @@ -127,6 +132,11 @@
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)).
- Minor code style cleanup.
- 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
Expand Down
28 changes: 28 additions & 0 deletions SqlServerDscHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,34 @@ function Restart-SqlService
$_ | Start-Service
}
}

Write-Verbose -Message ($script:localizedData.WaitingInstanceTimeout -f $SQLServer, $SQLInstanceName, $Timeout) -Verbose

$connectTimer = [System.Diagnostics.StopWatch]::StartNew()

do
{
# This call, if it fails, will take between ~9-10 seconds to return.
$testConnectionServerObject = Connect-SQL -SQLServer $SQLServer -SQLInstanceName $SQLInstanceName -ErrorAction SilentlyContinue
if ($testConnectionServerObject -and $testConnectionServerObject.Status -ne 'Online')
{
# Waiting 2 seconds to not hammer the SQL Server instance.
Start-Sleep -Seconds 2
}
else
{
break
}
} until ($connectTimer.Elapsed.Seconds -ge $Timeout)

$connectTimer.Stop()

# Was the timeout period reach before able to connect to the SQL Server instance?
if (-not $testConnectionServerObject -or $testConnectionServerObject.Status -ne 'Online')
{
$errorMessage = $script:localizedData.FailedToConnectToInstanceTimeout -f $SQLServer, $SQLInstanceName, $Timeout
New-InvalidOperationException -Message $errorMessage
}
}

<#
Expand Down
Loading

0 comments on commit 761d7d8

Please sign in to comment.