@@ -40,15 +40,18 @@ public void InstallSnapshot(
4040 IEnumerable < DependencyManifestEntry > dependencies ,
4141 string targetPath ,
4242 PowerShell pwsh ,
43- bool removeIfEquivalentToLatest ,
43+ DependencySnapshotInstallationMode installationMode ,
4444 ILogger logger )
4545 {
4646 var installingPath = CreateInstallingSnapshot ( targetPath ) ;
4747
4848 logger . Log (
4949 isUserOnlyLog : false ,
5050 LogLevel . Trace ,
51- string . Format ( PowerShellWorkerStrings . InstallingFunctionAppRequiredModules , installingPath ) ) ;
51+ string . Format (
52+ PowerShellWorkerStrings . InstallingFunctionAppRequiredModules ,
53+ installingPath ,
54+ installationMode ) ) ;
5255
5356 try
5457 {
@@ -57,18 +60,38 @@ public void InstallSnapshot(
5760 InstallModule ( module , installingPath , pwsh , logger ) ;
5861 }
5962
60- if ( removeIfEquivalentToLatest )
63+ switch ( installationMode )
6164 {
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 ) ) ;
6786 }
6887 }
6988 catch ( Exception e )
7089 {
71- var message = string . Format ( PowerShellWorkerStrings . FailedToInstallDependenciesSnapshot , targetPath ) ;
90+ var message = string . Format (
91+ PowerShellWorkerStrings . FailedToInstallDependenciesSnapshot ,
92+ targetPath ,
93+ installationMode ) ;
94+
7295 logger . Log ( isUserOnlyLog : false , LogLevel . Warning , message , e ) ;
7396 _storage . RemoveSnapshot ( installingPath ) ;
7497 throw ;
@@ -130,7 +153,11 @@ private void InstallModule(DependencyInfo module, string installingPath, PowerSh
130153 }
131154 }
132155
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 )
134161 {
135162 var latestSnapshot = _storage . GetLatestInstalledSnapshot ( ) ;
136163 if ( latestSnapshot != null && _snapshotComparer . AreEquivalent ( installingPath , latestSnapshot , logger ) )
@@ -148,18 +175,22 @@ private void PromoteToInstalledOrRemove(string installingPath, string installedP
148175 }
149176 else
150177 {
151- PromoteToInstalled ( installingPath , installedPath , logger ) ;
178+ PromoteToInstalled ( installingPath , installedPath , installationMode , logger ) ;
152179 }
153180 }
154181
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 )
156187 {
157188 _storage . PromoteInstallingSnapshotToInstalledAtomically ( installedPath ) ;
158189
159190 logger . Log (
160191 isUserOnlyLog : false ,
161192 LogLevel . Trace ,
162- string . Format ( PowerShellWorkerStrings . PromotedDependencySnapshot , installingPath , installedPath ) ) ;
193+ string . Format ( PowerShellWorkerStrings . PromotedDependencySnapshot , installingPath , installedPath , installationMode ) ) ;
163194
164195 _snapshotContentLogger . LogDependencySnapshotContent ( installedPath , logger ) ;
165196 }
0 commit comments