Skip to content

Commit 1a35bff

Browse files
authored
Improve Managed Dependencies logs (#558) (#563)
1 parent be0ad7f commit 1a35bff

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement
7+
{
8+
using System;
9+
using System.Threading;
10+
using Microsoft.Azure.Functions.PowerShellWorker.Utility;
11+
12+
internal class BackgroundDependencySnapshotContentLogger : IBackgroundDependencySnapshotContentLogger, IDisposable
13+
{
14+
private readonly IDependencySnapshotContentLogger _snapshotContentLogger;
15+
16+
private Timer _timer;
17+
18+
public BackgroundDependencySnapshotContentLogger(IDependencySnapshotContentLogger snapshotContentLogger)
19+
{
20+
_snapshotContentLogger = snapshotContentLogger ?? throw new ArgumentNullException(nameof(snapshotContentLogger));
21+
}
22+
23+
public void Start(string currentSnapshotPath, ILogger logger)
24+
{
25+
var period = PowerShellWorkerConfiguration.GetTimeSpan("MDCurrentSnapshotContentLogPeriod") ?? TimeSpan.FromDays(1);
26+
27+
_timer = new Timer(
28+
_ => { _snapshotContentLogger.LogDependencySnapshotContent(currentSnapshotPath, logger); },
29+
state: null,
30+
dueTime: period,
31+
period: period);
32+
}
33+
34+
public void Dispose()
35+
{
36+
_timer?.Dispose();
37+
}
38+
}
39+
}

src/DependencyManagement/DependencyManager.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ internal class DependencyManager : IDisposable
2929

3030
private readonly IBackgroundDependencySnapshotMaintainer _backgroundSnapshotMaintainer;
3131

32+
private readonly IBackgroundDependencySnapshotContentLogger _currentSnapshotContentLogger;
33+
3234
private DependencyManifestEntry[] _dependenciesFromManifest;
3335

3436
private string _currentSnapshotPath;
@@ -48,21 +50,25 @@ public DependencyManager(
4850
IInstalledDependenciesLocator installedDependenciesLocator = null,
4951
IDependencySnapshotInstaller installer = null,
5052
INewerDependencySnapshotDetector newerSnapshotDetector = null,
51-
IBackgroundDependencySnapshotMaintainer maintainer = null)
53+
IBackgroundDependencySnapshotMaintainer maintainer = null,
54+
IBackgroundDependencySnapshotContentLogger currentSnapshotContentLogger = null)
5255
{
5356
_storage = storage ?? new DependencyManagerStorage(GetFunctionAppRootPath(requestMetadataDirectory));
5457
_installedDependenciesLocator = installedDependenciesLocator ?? new InstalledDependenciesLocator(_storage);
58+
var snapshotContentLogger = new PowerShellModuleSnapshotLogger();
5559
_installer = installer ?? new DependencySnapshotInstaller(
5660
moduleProvider ?? new PowerShellGalleryModuleProvider(),
5761
_storage,
5862
new PowerShellModuleSnapshotComparer(),
59-
new PowerShellModuleSnapshotLogger());
63+
snapshotContentLogger);
6064
_newerSnapshotDetector = newerSnapshotDetector ?? new NewerDependencySnapshotDetector(_storage, new WorkerRestarter());
6165
_backgroundSnapshotMaintainer =
6266
maintainer ?? new BackgroundDependencySnapshotMaintainer(
6367
_storage,
6468
_installer,
6569
new DependencySnapshotPurger(_storage));
70+
_currentSnapshotContentLogger =
71+
currentSnapshotContentLogger ?? new BackgroundDependencySnapshotContentLogger(snapshotContentLogger);
6672
}
6773

6874
/// <summary>
@@ -103,6 +109,7 @@ internal string Initialize(ILogger logger)
103109

104110
_backgroundSnapshotMaintainer.Start(_currentSnapshotPath, _dependenciesFromManifest, logger);
105111
_newerSnapshotDetector.Start(_currentSnapshotPath, logger);
112+
_currentSnapshotContentLogger.Start(_currentSnapshotPath, logger);
106113

107114
return _currentSnapshotPath;
108115
}
@@ -175,6 +182,7 @@ public void Dispose()
175182
{
176183
(_backgroundSnapshotMaintainer as IDisposable)?.Dispose();
177184
(_newerSnapshotDetector as IDisposable)?.Dispose();
185+
(_currentSnapshotContentLogger as IDisposable)?.Dispose();
178186
_dependencyInstallationTask?.Dispose();
179187
}
180188

src/DependencyManagement/DependencySnapshotInstaller.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public void InstallSnapshot(
6060
InstallModule(module, installingPath, pwsh, logger);
6161
}
6262

63+
_snapshotContentLogger.LogDependencySnapshotContent(installingPath, logger);
64+
6365
switch (installationMode)
6466
{
6567
case DependencySnapshotInstallationMode.Optional:
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.Azure.Functions.PowerShellWorker.DependencyManagement
7+
{
8+
using Utility;
9+
10+
internal interface IBackgroundDependencySnapshotContentLogger
11+
{
12+
void Start(string currentSnapshotPath, ILogger logger);
13+
}
14+
}

test/Unit/DependencyManagement/DependencyManagerTests.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class DependencyManagerTests
2424
private readonly Mock<IDependencySnapshotInstaller> _mockInstaller = new Mock<IDependencySnapshotInstaller>(MockBehavior.Strict);
2525
private readonly Mock<INewerDependencySnapshotDetector> _mockNewerDependencySnapshotDetector = new Mock<INewerDependencySnapshotDetector>();
2626
private readonly Mock<IBackgroundDependencySnapshotMaintainer> _mockBackgroundDependencySnapshotMaintainer = new Mock<IBackgroundDependencySnapshotMaintainer>();
27+
private readonly Mock<IBackgroundDependencySnapshotContentLogger> _mockBackgroundDependencySnapshotContentLogger = new Mock<IBackgroundDependencySnapshotContentLogger>();
2728
private readonly Mock<ILogger> _mockLogger = new Mock<ILogger>();
2829

2930
[Fact]
@@ -82,7 +83,7 @@ public void Initialize_DoesNotTryToCheckOrMaintainDependencies_WhenNoDependencie
8283
}
8384

8485
[Fact]
85-
public void Initialize_StartsBackgroundSnapshotMaintainerAndNewerSnapshotDetector()
86+
public void Initialize_StartsBackgroundActivities()
8687
{
8788
var dependencyManifest = GetAnyNonEmptyDependencyManifestEntries();
8889
_mockStorage.Setup(_ => _.GetDependencies()).Returns(dependencyManifest);
@@ -100,6 +101,10 @@ public void Initialize_StartsBackgroundSnapshotMaintainerAndNewerSnapshotDetecto
100101
_mockNewerDependencySnapshotDetector.Verify(
101102
_ => _.Start("InstalledSnapshot", _mockLogger.Object),
102103
Times.Once);
104+
105+
_mockBackgroundDependencySnapshotContentLogger.Verify(
106+
_ => _.Start("InstalledSnapshot", _mockLogger.Object),
107+
Times.Once);
103108
}
104109
}
105110

@@ -307,7 +312,8 @@ private DependencyManager CreateDependencyManagerWithMocks()
307312
installedDependenciesLocator: _mockInstalledDependenciesLocator.Object,
308313
installer: _mockInstaller.Object,
309314
newerSnapshotDetector: _mockNewerDependencySnapshotDetector.Object,
310-
maintainer: _mockBackgroundDependencySnapshotMaintainer.Object);
315+
maintainer: _mockBackgroundDependencySnapshotMaintainer.Object,
316+
currentSnapshotContentLogger: _mockBackgroundDependencySnapshotContentLogger.Object);
311317
}
312318
}
313319
}

test/Unit/DependencyManagement/DependencySnapshotInstallerTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public void PromotesInstallingSnapshotToInstalledIfNotEquivalentToLatest()
118118

119119
_mockStorage.Verify(_ => _.CreateInstallingSnapshot(_targetPathInstalled), Times.Once);
120120
_mockStorage.Verify(_ => _.PromoteInstallingSnapshotToInstalledAtomically(_targetPathInstalled), Times.Once);
121+
122+
_mockSnapshotContentLogger.Verify(_ => _.LogDependencySnapshotContent(_targetPathInstalling, _mockLogger.Object), Times.Once);
121123
_mockSnapshotContentLogger.Verify(_ => _.LogDependencySnapshotContent(_targetPathInstalled, _mockLogger.Object), Times.Once);
122124
}
123125

@@ -260,6 +262,9 @@ public void DoesNotPromoteSnapshotIfItIsEquivalentToLatest()
260262
_mockStorage.Verify(_ => _.PromoteInstallingSnapshotToInstalledAtomically(It.IsAny<string>()), Times.Never);
261263
_mockStorage.Verify(_ => _.RemoveSnapshot(_targetPathInstalling), Times.Once);
262264
_mockStorage.Verify(_ => _.SetSnapshotCreationTimeToUtcNow("snapshot"), Times.Once);
265+
266+
_mockSnapshotContentLogger.Verify(_ => _.LogDependencySnapshotContent(_targetPathInstalling, _mockLogger.Object), Times.Once);
267+
_mockSnapshotContentLogger.Verify(_ => _.LogDependencySnapshotContent(_targetPathInstalled, _mockLogger.Object), Times.Never);
263268
}
264269

265270
private DependencySnapshotInstaller CreateDependenciesSnapshotInstallerWithMocks()

0 commit comments

Comments
 (0)