@@ -11,6 +11,7 @@ function Update-PSRepositoryCache {
11
11
# function begin phase
12
12
$FunctionName = $MyInvocation.MyCommand.Name
13
13
Write-Log - Message " $FunctionName starting" - TimeStampFormat ' G'
14
+ $LockFileCreated = $false # not yet
14
15
15
16
16
17
#
@@ -24,43 +25,59 @@ function Update-PSRepositoryCache {
24
25
25
26
if ($Age -lt 10 ) {
26
27
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
27
30
} else {
28
31
29
32
#
30
33
# get a index.zip file to $TP.Index
31
34
#
32
35
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
40
36
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
42
73
} 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
+ }
44
79
}
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
61
80
62
- # the end
63
- RemoveTempFolder
64
81
65
82
}
66
83
0 commit comments