Refactor: Pre-process Dell catalog before parallel downloads

Moves the Dell catalog download and preparation logic from the individual driver download task to the parent function.

This prevents a race condition where multiple parallel tasks would attempt to download and extract the same catalog file simultaneously. The catalog is now prepared once before any driver downloads begin, improving efficiency and reliability.

Additionally, comments out manual garbage collection calls in the VM build UI.
This commit is contained in:
rbalsleyMSFT
2025-06-20 16:30:23 -07:00
parent d0c5ddc9c7
commit 2e497ccec8
3 changed files with 50 additions and 36 deletions
+3 -3
View File
@@ -167,9 +167,9 @@ $window.Add_Closed({
} }
} }
# Garbage collection # # Garbage collection
[System.GC]::Collect() # [System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers() # [System.GC]::WaitForPendingFinalizers()
}) })
[void]$window.ShowDialog() [void]$window.ShowDialog()
@@ -178,41 +178,10 @@ function Save-DellDriversTask {
$modelPath = Join-Path -Path $makeDriversPath -ChildPath $modelName $modelPath = Join-Path -Path $makeDriversPath -ChildPath $modelName
try { try {
# --- Dell Catalog Handling --- # Define paths for Dell catalog. The catalog is assumed to be prepared by the calling function.
$dellDriversFolder = Join-Path -Path $DriversFolder -ChildPath "Dell" $dellDriversFolder = Join-Path -Path $DriversFolder -ChildPath "Dell"
$catalogBaseName = if ($WindowsRelease -le 11) { "CatalogPC" } else { "Catalog" } $catalogBaseName = if ($WindowsRelease -le 11) { "CatalogPC" } else { "Catalog" }
$dellCabFile = Join-Path -Path $dellDriversFolder -ChildPath "$($catalogBaseName).cab"
$dellCatalogXML = Join-Path -Path $dellDriversFolder -ChildPath "$($catalogBaseName).xml" $dellCatalogXML = Join-Path -Path $dellDriversFolder -ChildPath "$($catalogBaseName).xml"
$catalogUrl = if ($WindowsRelease -le 11) { "http://downloads.dell.com/catalog/CatalogPC.cab" } else { "https://downloads.dell.com/catalog/Catalog.cab" }
$downloadCatalog = $true
if (Test-Path -Path $dellCatalogXML -PathType Leaf) {
if (((Get-Date) - (Get-Item $dellCatalogXML).LastWriteTime).TotalDays -lt 7) {
WriteLog "Using existing Dell Catalog XML (less than 7 days old): $dellCatalogXML"
$downloadCatalog = $false
}
else { WriteLog "Existing Dell Catalog XML is older than 7 days: $dellCatalogXML" }
}
else { WriteLog "Dell Catalog XML not found: $dellCatalogXML" }
if ($downloadCatalog) {
WriteLog "Downloading and extracting Dell Catalog for task..."
if (-not (Test-Path -Path $dellDriversFolder -PathType Container)) {
New-Item -Path $dellDriversFolder -ItemType Directory -Force | Out-Null
}
try {
$request = [System.Net.WebRequest]::Create($catalogUrl); $request.Method = 'HEAD'; $response = $request.GetResponse(); $response.Close()
}
catch { throw "Dell Catalog URL '$catalogUrl' not accessible: $($_.Exception.Message)" }
if (Test-Path -Path $dellCabFile) { Remove-Item -Path $dellCabFile -Force -ErrorAction SilentlyContinue }
if (Test-Path -Path $dellCatalogXML) { Remove-Item -Path $dellCatalogXML -Force -ErrorAction SilentlyContinue }
Start-BitsTransferWithRetry -Source $catalogUrl -Destination $dellCabFile
Invoke-Process -FilePath "Expand.exe" -ArgumentList """$dellCabFile"" ""$dellCatalogXML""" | Out-Null
Remove-Item -Path $dellCabFile -Force -ErrorAction SilentlyContinue
}
# --- End Dell Catalog Handling ---
# 1. Check if drivers already exist for this model (final destination) # 1. Check if drivers already exist for this model (final destination)
if (Test-Path -Path $modelPath -PathType Container) { if (Test-Path -Path $modelPath -PathType Container) {
@@ -567,7 +536,6 @@ function Save-DellDriversTask {
# Ensure return object is created even on error # Ensure return object is created even on error
return [PSCustomObject]@{ Model = $modelName; Status = $status; Success = $success } return [PSCustomObject]@{ Model = $modelName; Status = $status; Success = $success }
} }
# REMOVED: Finally block that cleaned up temp catalog files
# Enqueue the final status (success or error) before returning # Enqueue the final status (success or error) before returning
if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status } if ($null -ne $ProgressQueue) { Invoke-ProgressUpdate -ProgressQueue $ProgressQueue -Identifier $modelName -Status $status }
@@ -568,6 +568,52 @@ function Invoke-DownloadSelectedDrivers {
$State.Controls.txtStatus.Text = "Processing all selected drivers..." $State.Controls.txtStatus.Text = "Processing all selected drivers..."
WriteLog "Processing all selected drivers: $($selectedDrivers.Model -join ', ')" WriteLog "Processing all selected drivers: $($selectedDrivers.Model -join ', ')"
# Pre-process Dell Catalog if needed, so it's not done in parallel
if ($selectedDrivers | Where-Object { $_.Make -eq 'Dell' }) {
WriteLog "Dell drivers selected. Ensuring Dell Catalog is up-to-date..."
try {
$dellDriversFolder = Join-Path -Path $localDriversFolder -ChildPath "Dell"
$catalogBaseName = if ($localWindowsRelease -le 11) { "CatalogPC" } else { "Catalog" }
$dellCabFile = Join-Path -Path $dellDriversFolder -ChildPath "$($catalogBaseName).cab"
$dellCatalogXML = Join-Path -Path $dellDriversFolder -ChildPath "$($catalogBaseName).xml"
$catalogUrl = if ($localWindowsRelease -le 11) { "http://downloads.dell.com/catalog/CatalogPC.cab" } else { "https://downloads.dell.com/catalog/Catalog.cab" }
$downloadCatalog = $true
if (Test-Path -Path $dellCatalogXML -PathType Leaf) {
if (((Get-Date) - (Get-Item $dellCatalogXML).CreationTime).TotalDays -lt 7) {
WriteLog "Using existing Dell Catalog XML (less than 7 days old): $dellCatalogXML"
$downloadCatalog = $false
}
else { WriteLog "Existing Dell Catalog XML is older than 7 days: $dellCatalogXML" }
}
else { WriteLog "Dell Catalog XML not found: $dellCatalogXML" }
if ($downloadCatalog) {
WriteLog "Downloading and extracting Dell Catalog for driver download process..."
if (-not (Test-Path -Path $dellDriversFolder -PathType Container)) {
New-Item -Path $dellDriversFolder -ItemType Directory -Force | Out-Null
}
if (Test-Path -Path $dellCabFile) { Remove-Item -Path $dellCabFile -Force -ErrorAction SilentlyContinue }
if (Test-Path -Path $dellCatalogXML) { Remove-Item -Path $dellCatalogXML -Force -ErrorAction SilentlyContinue }
Start-BitsTransferWithRetry -Source $catalogUrl -Destination $dellCabFile
Invoke-Process -FilePath "Expand.exe" -ArgumentList """$dellCabFile"" ""$dellCatalogXML""" | Out-Null
Remove-Item -Path $dellCabFile -Force -ErrorAction SilentlyContinue
WriteLog "Dell Catalog prepared successfully."
}
}
catch {
$errorMessage = "Failed to prepare Dell Catalog: $($_.Exception.Message)"
WriteLog $errorMessage
[System.Windows.MessageBox]::Show($errorMessage, "Dell Catalog Error", "OK", "Error")
$Button.IsEnabled = $true
$State.Controls.pbOverallProgress.Visibility = 'Collapsed'
$State.Controls.txtStatus.Text = "Driver download cancelled due to Dell Catalog error."
return
}
}
$taskArguments = @{ $taskArguments = @{
DriversFolder = $localDriversFolder DriversFolder = $localDriversFolder
WindowsRelease = $localWindowsRelease WindowsRelease = $localWindowsRelease