@@ -40,15 +40,18 @@ public void InstallSnapshot(
40
40
IEnumerable < DependencyManifestEntry > dependencies ,
41
41
string targetPath ,
42
42
PowerShell pwsh ,
43
- bool removeIfEquivalentToLatest ,
43
+ DependencySnapshotInstallationMode installationMode ,
44
44
ILogger logger )
45
45
{
46
46
var installingPath = CreateInstallingSnapshot ( targetPath ) ;
47
47
48
48
logger . Log (
49
49
isUserOnlyLog : false ,
50
50
LogLevel . Trace ,
51
- string . Format ( PowerShellWorkerStrings . InstallingFunctionAppRequiredModules , installingPath ) ) ;
51
+ string . Format (
52
+ PowerShellWorkerStrings . InstallingFunctionAppRequiredModules ,
53
+ installingPath ,
54
+ installationMode ) ) ;
52
55
53
56
try
54
57
{
@@ -57,18 +60,38 @@ public void InstallSnapshot(
57
60
InstallModule ( module , installingPath , pwsh , logger ) ;
58
61
}
59
62
60
- if ( removeIfEquivalentToLatest )
63
+ switch ( installationMode )
61
64
{
62
- PromoteToInstalledOrRemove ( installingPath , targetPath , logger ) ;
63
- }
64
- else
65
- {
66
- PromoteToInstalled ( installingPath , targetPath , logger ) ;
65
+ case DependencySnapshotInstallationMode . Optional :
66
+ // If the new snapshot turns out to be equivalent to the latest one,
67
+ // removing it helps us save storage space and avoid unnecessary worker restarts.
68
+ // It is ok to do that during background upgrade because the current
69
+ // worker already has a good enough snapshot, and nothing depends on
70
+ // the new snapshot yet.
71
+ PromoteToInstalledOrRemove ( installingPath , targetPath , installationMode , logger ) ;
72
+ break ;
73
+
74
+ case DependencySnapshotInstallationMode . Required :
75
+ // Even if the new snapshot turns out to be equivalent to the latest one,
76
+ // removing it would not be safe because the current worker already depends
77
+ // on it, as it has the path to this snapshot already added to PSModulePath.
78
+ // As opposed to the background upgrade case, this snapshot is *required* for
79
+ // this worker to run, even though it occupies some space (until the workers
80
+ // restart and the redundant snapshots are purged).
81
+ PromoteToInstalled ( installingPath , targetPath , installationMode , logger ) ;
82
+ break ;
83
+
84
+ default :
85
+ throw new ArgumentException ( $ "Unexpected installation mode: { installationMode } ", nameof ( installationMode ) ) ;
67
86
}
68
87
}
69
88
catch ( Exception e )
70
89
{
71
- var message = string . Format ( PowerShellWorkerStrings . FailedToInstallDependenciesSnapshot , targetPath ) ;
90
+ var message = string . Format (
91
+ PowerShellWorkerStrings . FailedToInstallDependenciesSnapshot ,
92
+ targetPath ,
93
+ installationMode ) ;
94
+
72
95
logger . Log ( isUserOnlyLog : false , LogLevel . Warning , message , e ) ;
73
96
_storage . RemoveSnapshot ( installingPath ) ;
74
97
throw ;
@@ -130,7 +153,11 @@ private void InstallModule(DependencyInfo module, string installingPath, PowerSh
130
153
}
131
154
}
132
155
133
- private void PromoteToInstalledOrRemove ( string installingPath , string installedPath , ILogger logger )
156
+ private void PromoteToInstalledOrRemove (
157
+ string installingPath ,
158
+ string installedPath ,
159
+ DependencySnapshotInstallationMode installationMode ,
160
+ ILogger logger )
134
161
{
135
162
var latestSnapshot = _storage . GetLatestInstalledSnapshot ( ) ;
136
163
if ( latestSnapshot != null && _snapshotComparer . AreEquivalent ( installingPath , latestSnapshot , logger ) )
@@ -148,18 +175,22 @@ private void PromoteToInstalledOrRemove(string installingPath, string installedP
148
175
}
149
176
else
150
177
{
151
- PromoteToInstalled ( installingPath , installedPath , logger ) ;
178
+ PromoteToInstalled ( installingPath , installedPath , installationMode , logger ) ;
152
179
}
153
180
}
154
181
155
- private void PromoteToInstalled ( string installingPath , string installedPath , ILogger logger )
182
+ private void PromoteToInstalled (
183
+ string installingPath ,
184
+ string installedPath ,
185
+ DependencySnapshotInstallationMode installationMode ,
186
+ ILogger logger )
156
187
{
157
188
_storage . PromoteInstallingSnapshotToInstalledAtomically ( installedPath ) ;
158
189
159
190
logger . Log (
160
191
isUserOnlyLog : false ,
161
192
LogLevel . Trace ,
162
- string . Format ( PowerShellWorkerStrings . PromotedDependencySnapshot , installingPath , installedPath ) ) ;
193
+ string . Format ( PowerShellWorkerStrings . PromotedDependencySnapshot , installingPath , installedPath , installationMode ) ) ;
163
194
164
195
_snapshotContentLogger . LogDependencySnapshotContent ( installedPath , logger ) ;
165
196
}
0 commit comments