Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset default runspace - fixes #3860 #4472

Merged
merged 3 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions functions/Get-DbaWindowsLog.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ function Get-DbaWindowsLog {
#region Setup Runspace
[Collections.Arraylist]$RunspaceCollection = @()
$InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace
$RunspacePool = [RunspaceFactory]::CreateRunspacePool($InitialSessionState)
$RunspacePool.SetMinRunspaces(1) | Out-Null
if ($MaxThreads -gt 0) { $null = $RunspacePool.SetMaxRunspaces($MaxThreads) }
Expand All @@ -303,6 +304,6 @@ function Get-DbaWindowsLog {
Receive-Runspace -Wait
$RunspacePool.Close()
$RunspacePool.Dispose()
[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace
}
}

}
2 changes: 2 additions & 0 deletions functions/Read-DbaBackupHeader.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ function Read-DbaBackupHeader {

#Setup initial session state
$InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace
#Create Runspace pool, min - 1, max - 10 sessions: there is internal SQL Server queue for the restore operations. 10 threads seem to perform best
$runspacePool = [runspacefactory]::CreateRunspacePool(1, 10, $InitialSessionState, $Host)
$runspacePool.Open()
Expand Down Expand Up @@ -277,6 +278,7 @@ function Read-DbaBackupHeader {
}
#Close the runspace pool
$runspacePool.Close()
[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace
}
}

4 changes: 3 additions & 1 deletion internal/functions/Invoke-DbaAsync.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ function Invoke-DbaAsync {

$ds = New-Object system.Data.DataSet
$da = New-Object system.Data.SqlClient.SqlDataAdapter($cmd)

if ($MessagesToOutput) {
$defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace
$pool = [RunspaceFactory]::CreateRunspacePool(1, [int]$env:NUMBER_OF_PROCESSORS + 1)
$pool.ApartmentState = "MTA"
$pool.Open()
Expand Down Expand Up @@ -227,6 +228,7 @@ function Invoke-DbaAsync {
}
$pool.Close()
$pool.Dispose()
[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace
} else {
#Following EventHandler is used for PRINT and RAISERROR T-SQL statements. Executed when -Verbose parameter specified by caller and no -MessageToOutput
if ($PSBoundParameters.Verbose) {
Expand Down
7 changes: 4 additions & 3 deletions internal/functions/Invoke-Parallel.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ function Invoke-Parallel {
[switch] $Quiet = $false
)
begin {
# save default runspace
$defaultrunspace = [System.Management.Automation.Runspaces.Runspace]::DefaultRunspace
#No max queue specified? Estimate one.
#We use the script scope to resolve an odd PowerShell 2 issue where MaxQueue isn't seen later in the function
if ( -not $PSBoundParameters.ContainsKey('MaxQueue') ) {
Expand Down Expand Up @@ -548,7 +550,6 @@ function Invoke-Parallel {
#collect garbage
[gc]::Collect()
}
[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $defaultrunspace
}
}


}