Skip to content

Commit db81ea8

Browse files
authored
Merge pull request #37 from iricigor/LockFile
added lock file
2 parents 2c9c410 + fa51c45 commit db81ea8

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

PSAptGetUpdate.psm1

+2
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ if ($V) {
4747
Write-Host "'Get-Command -Module $ModName | Get-Help | Select Name, Synopsis' for explanation on all commands`n"
4848
}
4949

50+
# initial actions
51+
if (Test-Path $TP.LockFile) {Remove-Item $TP.LockFile -Force}
5052
Update-PSRepositoryCache

Private/Config.ps1

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ $Script:Config = New-Object PSObject -Property @{
2020

2121
# General names
2222
IndexFile = 'PSGalleryIndex.zip'
23+
LockFile = 'psaptgetupdate.lock'
2324

2425
# Config file names
2526
ModulesCache = 'Modules.Cache'
@@ -38,6 +39,7 @@ $Script:TP = New-Object PSObject -Property @{
3839
Modules = Join-Path $Config.TempPath $Config.ModulesCache
3940
Scripts = Join-Path $Config.TempPath $Config.ScriptsCache
4041
Commands = Join-Path $Config.TempPath $Config.CommandsCache
42+
LockFile = Join-Path $Config.TempPath $Config.LockFile
4143
}
4244

4345
# IndexPath File Locations, used like $IP.Modules

Public/Update-ModuleFromCache.ps1

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function Update-ModuleFromCache {
2626
Write-Log -Message "Reading list of all modules from the system"
2727
$SearchAll = $true
2828
$AllModules = Get-Module -ListAvailable -Verbose:$false | where {$_.RepositorySourceLocation.Host -eq 'www.powershellgallery.com'}
29+
# We do not use Get-InstalledModule although it returns smaller number of modules, because we do not spend much time on search anyway
2930
$ModuleName = $AllModules.Name | Select -Unique
3031
}
3132
foreach ($M1 in $ModuleName) {

Public/Update-PSRepositoryCache.ps1

+44-27
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function Update-PSRepositoryCache {
1111
# function begin phase
1212
$FunctionName = $MyInvocation.MyCommand.Name
1313
Write-Log -Message "$FunctionName starting" -TimeStampFormat 'G'
14+
$LockFileCreated = $false # not yet
1415

1516

1617
#
@@ -24,43 +25,59 @@ function Update-PSRepositoryCache {
2425

2526
if ($Age -lt 10) {
2627
Write-Log -Message "Skipping download, as index file is $Age seconds old"
28+
} elseif (Test-Path $TP.LockFile) {
29+
Write-Log -Message "Skipping download as another process has lock file $($TP.LockFile)" -Verbosity Warning
2730
} else {
2831

2932
#
3033
# get a index.zip file to $TP.Index
3134
#
3235

33-
CreateTempFolder
34-
Write-Log -Message "Downloading index from the Internet"
35-
# temporary remove ProgressBar, https://stackoverflow.com/questions/28682642/powershell-why-is-using-invoke-webrequest-much-slower-than-a-browser-download
36-
$OldProgressPreference = $ProgressPreference
37-
$ProgressPreference = 'SilentlyContinue'
38-
$Response = Invoke-WebRequest -Uri 'https://psgallery.blob.core.windows.net/index/PSGalleryIndex.zip' -Verbose:$false -OutFile $TP.Index -PassThru
39-
$ProgressPreference = $OldProgressPreference
4036
try {
41-
[string]$AgeString = [int](((Get-Date)-[datetime]($Response.Headers.'Last-Modified')).TotalMinutes)
37+
38+
CreateTempFolder
39+
Write-Log -Message "Creating lock file $($TP.LockFile)"
40+
New-Item $TP.LockFile -ItemType File -Force -ErrorAction Stop | Out-Null
41+
$LockFileCreated = $true
42+
Write-Log -Message "Downloading index from the Internet"
43+
# temporary remove ProgressBar, https://stackoverflow.com/questions/28682642/powershell-why-is-using-invoke-webrequest-much-slower-than-a-browser-download
44+
$OldProgressPreference = $ProgressPreference
45+
$ProgressPreference = 'SilentlyContinue'
46+
$Response = Invoke-WebRequest -Uri 'https://psgallery.blob.core.windows.net/index/PSGalleryIndex.zip' -Verbose:$false -OutFile $TP.Index -PassThru
47+
$ProgressPreference = $OldProgressPreference
48+
try {
49+
[string]$AgeString = [int](((Get-Date)-[datetime]($Response.Headers.'Last-Modified')).TotalMinutes)
50+
} catch {
51+
$AgeString = 'unknown'
52+
}
53+
Write-Log -Message "Downloading completed, index file is $(size $TP.Index)MB large and $AgeString minutes old"
54+
55+
56+
#
57+
# unzip index.zip
58+
#
59+
60+
Write-Log -Message "Expanding archive to $($Config.IndexPath)"
61+
Microsoft.PowerShell.Archive\Expand-Archive $TP.Index -DestinationPath $Config.IndexPath -Force
62+
Write-Log -Message "Expanded total $((Get-ChildItem $Config.IndexPath).Count) files" # FIXME: This lists also old files from folder
63+
64+
#
65+
# Touch one file to prevent too fast/double updates
66+
#
67+
68+
(Get-Item $IP.Commands).LastWriteTime = Get-Date
69+
70+
# the end
71+
72+
RemoveTempFolder
4273
} catch {
43-
$AgeString = 'unknown'
74+
Write-Log -Message "Cache update failed: $_"
75+
} finally {
76+
if ($LockFileCreated -and (Test-Path $TP.LockFile)) {
77+
Remove-Item $TP.LockFile -Force
78+
}
4479
}
45-
Write-Log -Message "Downloading completed, index file is $(size $TP.Index)MB large and $AgeString minutes old"
46-
47-
48-
#
49-
# unzip index.zip
50-
#
51-
52-
Write-Log -Message "Expanding archive to $($Config.IndexPath)"
53-
Microsoft.PowerShell.Archive\Expand-Archive $TP.Index -DestinationPath $Config.IndexPath -Force
54-
Write-Log -Message "expanded total $((Get-ChildItem $Config.IndexPath).Count) files" # FIXME: This lists also old files from folder
55-
56-
#
57-
# Touch one file to prevent too fast/double updates
58-
#
59-
60-
(Get-Item $IP.Commands).LastWriteTime = Get-Date
6180

62-
# the end
63-
RemoveTempFolder
6481

6582
}
6683

0 commit comments

Comments
 (0)