Skip to content

Commit 4b376ec

Browse files
authored
Ensure we don't try to purge diagnostic events when table client is not available (#10548)
1 parent 4295b50 commit 4b376ec

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/WebJobs.Script.WebHost/Diagnostics/DiagnosticEventTableStorageRepository.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ await Utility.InvokeWithRetriesAsync(async () =>
167167

168168
internal virtual async Task FlushLogs(TableClient table = null)
169169
{
170-
if (_environment.IsPlaceholderModeEnabled())
170+
// TableClient is initialized lazily and it will stop the timer that schedules flush logs whenever it fails to initialize.
171+
// We need to check if the TableClient is null before proceeding. This helps when the first time the property is accessed is as part of the FlushLogs method.
172+
// We should not have any events stored pending to be written since WriteDiagnosticEvent will check for an initialized TableClient.
173+
if (_environment.IsPlaceholderModeEnabled() || TableClient is null)
171174
{
172175
return;
173176
}
@@ -237,7 +240,7 @@ internal async Task ExecuteBatchAsync(ConcurrentDictionary<string, DiagnosticEve
237240

238241
public void WriteDiagnosticEvent(DateTime timestamp, string errorCode, LogLevel level, string message, string helpLink, Exception exception)
239242
{
240-
if (TableClient == null || string.IsNullOrEmpty(HostId))
243+
if (TableClient is null || string.IsNullOrEmpty(HostId))
241244
{
242245
return;
243246
}

test/WebJobs.Script.Tests.Integration/Diagnostics/DiagnosticEventTableStorageRepositoryTests.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ await TestHelpers.Await(async () =>
255255
}
256256

257257
[Fact]
258-
public async Task FlushLogs_LogsErrorAndClearsEvents_WhenTableCreatingFails()
258+
public async Task FlushLogs_OnPrimaryHost_DoesNotTryToPurgeEvents_WhenTableClientNotInitialized()
259259
{
260260
// Arrange
261261
IEnvironment testEnvironment = new TestEnvironment();
@@ -271,22 +271,26 @@ public async Task FlushLogs_LogsErrorAndClearsEvents_WhenTableCreatingFails()
271271
.AddInMemoryCollection(testData)
272272
.Build();
273273

274+
var mockPrimaryHostStateProvider = new Mock<IPrimaryHostStateProvider>(MockBehavior.Strict);
275+
mockPrimaryHostStateProvider.Setup(p => p.IsPrimary).Returns(true);
276+
277+
var scriptHostMock = new Mock<IScriptHostManager>(MockBehavior.Strict);
278+
scriptHostMock.As<IServiceProvider>().Setup(m => m.GetService(typeof(IPrimaryHostStateProvider))).Returns(mockPrimaryHostStateProvider.Object);
279+
274280
var localStorageProvider = TestHelpers.GetAzureTableStorageProvider(configuration);
275281
DiagnosticEventTableStorageRepository repository =
276-
new DiagnosticEventTableStorageRepository(_hostIdProvider, testEnvironment, _scriptHostMock.Object, localStorageProvider, _logger);
282+
new DiagnosticEventTableStorageRepository(_hostIdProvider, testEnvironment, scriptHostMock.Object, localStorageProvider, _logger);
277283

278284
// Act
279-
repository.WriteDiagnosticEvent(DateTime.UtcNow, "eh1", LogLevel.Information, "This is the message", "https://fwlink/", new Exception("exception message"));
280285
await repository.FlushLogs();
281286

282287
// Assert
283-
var logMessage = _loggerProvider.GetAllLogMessages().SingleOrDefault(m => m.FormattedMessage.Contains("Unable to get table reference"));
284-
Assert.NotNull(logMessage);
288+
var createFailureMessagePresent = _loggerProvider.GetAllLogMessages().Any(m => m.FormattedMessage.Contains("An error occurred initializing the Table Storage Client. We are unable to record diagnostic events, so the diagnostic logging service is being stopped."));
289+
Assert.True(createFailureMessagePresent);
285290

286-
var messagePresent = _loggerProvider.GetAllLogMessages().Any(m => m.FormattedMessage.Contains("An error occurred initializing the Table Storage Client. We are unable to record diagnostic events, so the diagnostic logging service is being stopped."));
287-
Assert.True(messagePresent);
291+
var purgeEventMessagePresent = _loggerProvider.GetAllLogMessages().Any(m => m.FormattedMessage.Contains("Purging diagnostic events with versions older than"));
292+
Assert.False(purgeEventMessagePresent);
288293

289-
Assert.Equal(0, repository.Events.Values.Count());
290294
}
291295

292296
[Theory]

0 commit comments

Comments
 (0)