Start-DbaDbEncryption: Fix issues with parallel processing#10083
Start-DbaDbEncryption: Fix issues with parallel processing#10083potatoqualitee merged 3 commits intodevelopmentfrom
Conversation
|
hell yeah, excited about improvements to the encryption suite |
|
on the agenda, just a bit to review |
|
Maybe you find a way to import dbatools inside of the runspace so that the custom methods like ".Query()" are imported correctly. Then we would have a blueprint for that kind of parallelism. That would be even better than my fix. I forgot to take a screenshot of the error messages. But you just need to run this part of the test: |
|
You don't even need multiple databases, should also work with just one. Only use the |
|
looks good, will talk to @claude about what we can do in our dbatools.dll in dbatools.library possibly to help with importing minimal parts of dbatools in parallel runspaces |
|
Claude finished @potatoqualitee's task in 2m 21s —— View job Analysis: Parallel Runspace Import ChallengesI've analyzed the merged PR and the parallel processing implementation to understand the runspace module import issue. Current State - The WorkaroundThe Problem:
Current Import Method (Start-DbaDbEncryption.ps1:558-562): $initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$dbatools = Get-Module -Name dbatools
if ($dbatools) {
$initialSessionState.ImportPSModule($dbatools.Path)
}This approach imports the full dbatools module, but the custom methods added by dbatools.dll aren't properly initialized in the runspace. Root Cause AnalysisThe
The runspace issue: When using Potential SolutionsOption 1: Explicit DLL Import in Runspaces (Recommended for dbatools.library)Add a helper method to dbatools.library that can be called to initialize extension methods in a runspace: // In dbatools.library
public static class RunspaceInitializer
{
public static void InitializeExtensionMethods()
{
// Re-register type extensions for current runspace
// This would need to be called once per runspace
}
}Then in PowerShell: $encryptionScript = {
# Import just the DLL and initialize extensions
Import-Module "$using:libraryRoot\lib\dbatools.dll"
[Dataplat.Dbatools.RunspaceInitializer]::InitializeExtensionMethods()
# Now extension methods work
$server.Query("SELECT 1")
}Option 2: Import DLL Directly in InitialSessionState$initialSessionState = [InitialSessionState]::CreateDefault()
$libraryPath = Get-DbatoolsLibraryPath
$dllPath = [System.IO.Path]::Combine($libraryPath, 'lib', 'dbatools.dll')
# Import the DLL as an assembly in the session state
$initialSessionState.Assemblies.Add([System.Management.Automation.Runspaces.SessionStateAssemblyEntry]::CreateFrom($dllPath))
# Then import the module
$initialSessionState.ImportPSModule("dbatools")Option 3: Use Variables to Pass Pre-Connected SMO ObjectsInstead of connecting in runspaces, pass connected server objects: $encryptionScript = {
param($ServerObject, $DatabaseName)
# Use pre-connected object that already has extensions
$db = $ServerObject.Databases[$DatabaseName]
# Work with it
}Option 4: Minimal Import Pattern (Current Workaround)Keep doing what the PR does - move any code requiring Recommendations for dbatools.libraryTo properly support parallel runspaces, I recommend adding these to dbatools.library:
Short-Term Best PracticeFor now, the current workaround is solid. When using parallel runspaces with dbatools:
This is exactly what the merged PR does, and it's a pragmatic solution until dbatools.library can provide better runspace support. References:
|
Type of Change
Invoke-ManualPester)Best fix would be fix the import of dbatools inside of the runspaces.
This is just a workaround.
The diff is maybe difficult to read as I fixed the indentation as well.