Skip to content

Commit d5e1eef

Browse files
authored
Remove partially installed snapshots (#290)
Remove partially installed snapshots on installation failures
1 parent 5b451b8 commit d5e1eef

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/DependencyManagement/DependencySnapshotInstaller.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ public void InstallSnapshot(
8484

8585
_storage.PromoteInstallingSnapshotToInstalledAtomically(targetPath);
8686
}
87+
catch (Exception e)
88+
{
89+
var message = string.Format(PowerShellWorkerStrings.FailedToInstallDependenciesSnapshot, targetPath);
90+
logger.Log(isUserOnlyLog: false, LogLevel.Warning, message, e);
91+
_storage.RemoveSnapshot(installingPath);
92+
throw;
93+
}
8794
finally
8895
{
8996
_moduleProvider.Cleanup(pwsh);

src/resources/PowerShellWorkerStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,7 @@
256256
<data name="UpdatingManagedDependencySnapshotHeartbeat" xml:space="preserve">
257257
<value>Updating dependencies folder heartbeat for '{0}''.</value>
258258
</data>
259+
<data name="FailedToInstallDependenciesSnapshot" xml:space="preserve">
260+
<value>Failed to install dependencies into '{0}', removing the folder.</value>
261+
</data>
259262
</root>

test/Unit/DependencyManagement/DependencyManagementTests.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,24 +336,28 @@ public void TestManagedDependencyRetryLogicMaxNumberOfTries()
336336

337337
var relevantLogs = _testLogger.FullLog.Where(
338338
message => message.StartsWith("Error: Fail to install module")
339-
|| message.StartsWith("Trace: Installing FunctionApp dependent modules")).ToList();
339+
|| message.StartsWith("Trace: Installing FunctionApp dependent modules")
340+
|| message.StartsWith("Warning: Failed to install dependencies")).ToList();
340341

341-
// Here we will get four logs: one that says that we are installing the
342-
// dependencies, and three for failing to install the module.
343-
Assert.Equal(4, relevantLogs.Count);
342+
// Here we will get five logs: one that says that we are installing the
343+
// dependencies, three for failing to install the module,
344+
// and one warning notifying of removing the dependencies folder.
345+
Assert.Equal(5, relevantLogs.Count);
344346

345347
// The first log should say "Installing FunctionApp dependent modules."
346348
Assert.Contains(PowerShellWorkerStrings.InstallingFunctionAppDependentModules, relevantLogs[0]);
347349

348350
// The subsequent logs should contain the following:
349-
for (int index = 1; index < relevantLogs.Count; index++)
351+
for (int index = 1; index < 4; index++)
350352
{
351353
Assert.Contains("Fail to install module", relevantLogs[index]);
352354
var currentAttempt = DependencySnapshotInstaller.GetCurrentAttemptMessage(index);
353355
Assert.Contains(currentAttempt, relevantLogs[index]);
354356
}
355357

356-
// Lastly, DependencyError should get set after unsuccessfully retyring 3 times.
358+
Assert.Matches("Warning: Failed to install dependencies into '(.+?)', removing the folder", relevantLogs[4]);
359+
360+
// Lastly, DependencyError should get set after unsuccessfully retrying 3 times.
357361
Assert.NotNull(dependencyError);
358362
Assert.Contains("Fail to install FunctionApp dependencies. Error:", dependencyError.Message);
359363
}

test/Unit/DependencyManagement/DependencySnapshotInstallerTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public void DoesNotSaveModuleIfGetLatestPublishedModuleVersionThrows()
109109
_ => _.GetLatestPublishedModuleVersion(It.IsAny<string>(), It.IsAny<string>()))
110110
.Throws(injectedException);
111111

112+
_mockStorage.Setup(_ => _.RemoveSnapshot(_targetPathInstalling));
113+
112114
_mockModuleProvider.Setup(_ => _.Cleanup(dummyPowerShell));
113115

114116
// Act
@@ -126,6 +128,7 @@ public void DoesNotSaveModuleIfGetLatestPublishedModuleVersionThrows()
126128
Times.Never);
127129

128130
_mockStorage.Verify(_ => _.PromoteInstallingSnapshotToInstalledAtomically(It.IsAny<string>()), Times.Never);
131+
_mockStorage.Verify(_ => _.RemoveSnapshot(_targetPathInstalling));
129132
_mockModuleProvider.Verify(_ => _.Cleanup(dummyPowerShell), Times.Once);
130133
}
131134

@@ -151,6 +154,8 @@ public void DoesNotPromoteDependenciesSnapshotIfSaveModuleKeepsThrowing()
151154
_ => _.SaveModule(It.IsAny<PowerShell>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
152155
.Throws(injectedException);
153156

157+
_mockStorage.Setup(_ => _.RemoveSnapshot(_targetPathInstalling));
158+
154159
_mockModuleProvider.Setup(_ => _.Cleanup(dummyPowerShell));
155160

156161
// Act
@@ -164,6 +169,7 @@ public void DoesNotPromoteDependenciesSnapshotIfSaveModuleKeepsThrowing()
164169
Assert.Contains(injectedException.Message, thrownException.Message);
165170

166171
_mockStorage.Verify(_ => _.PromoteInstallingSnapshotToInstalledAtomically(It.IsAny<string>()), Times.Never);
172+
_mockStorage.Verify(_ => _.RemoveSnapshot(_targetPathInstalling));
167173
_mockModuleProvider.Verify(_ => _.Cleanup(dummyPowerShell), Times.Once);
168174
}
169175

0 commit comments

Comments
 (0)