From df65611b39c0d6969383b55dd323ff422afae753 Mon Sep 17 00:00:00 2001 From: mu88 <4560672+mu88@users.noreply.github.com> Date: Mon, 8 Jul 2024 18:30:07 +0200 Subject: [PATCH] feat: add options to configure page reuse and try-catch --- .../BackgroundScreenshotCreator.cs | 9 +++++++-- src/ScreenshotCreator.Logic/PlaywrightHelper.cs | 2 +- .../ScreenshotOptions.cs | 17 +++++++++++------ .../Api/BackgroundScreenshotCreatorTests.cs | 6 ++++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/ScreenshotCreator.Api/BackgroundScreenshotCreator.cs b/src/ScreenshotCreator.Api/BackgroundScreenshotCreator.cs index a3f503e..88c9460 100644 --- a/src/ScreenshotCreator.Api/BackgroundScreenshotCreator.cs +++ b/src/ScreenshotCreator.Api/BackgroundScreenshotCreator.cs @@ -36,8 +36,13 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) _logger.BackgroundServiceTriggered(); if (_screenshotOptions.Activity.DisplayShouldBeActive()) { - try { await _screenshotCreator.CreateScreenshotAsync(_screenshotOptions.Width, _screenshotOptions.Height); } - catch (Exception e) { _logger.LogError(e, "Background processing failed"); } + if (_screenshotOptions.BackgroundProcessingWithTryCatch) + { + try { await _screenshotCreator.CreateScreenshotAsync(_screenshotOptions.Width, _screenshotOptions.Height); } + catch (Exception e) { _logger.LogError(e, "Background processing failed"); } + } + else + await _screenshotCreator.CreateScreenshotAsync(_screenshotOptions.Width, _screenshotOptions.Height); } } } diff --git a/src/ScreenshotCreator.Logic/PlaywrightHelper.cs b/src/ScreenshotCreator.Logic/PlaywrightHelper.cs index 09b7154..c097116 100644 --- a/src/ScreenshotCreator.Logic/PlaywrightHelper.cs +++ b/src/ScreenshotCreator.Logic/PlaywrightHelper.cs @@ -27,7 +27,7 @@ public async ValueTask DisposeAsync() /// public async ValueTask InitializePlaywrightAsync() { - if (_page != null) + if (!_screenshotOptions.DoNotReusePlaywrightPage && _page != null) { logger.ReusingPlaywrightPage(); return _page; diff --git a/src/ScreenshotCreator.Logic/ScreenshotOptions.cs b/src/ScreenshotCreator.Logic/ScreenshotOptions.cs index 4e64d4c..74ff221 100644 --- a/src/ScreenshotCreator.Logic/ScreenshotOptions.cs +++ b/src/ScreenshotCreator.Logic/ScreenshotOptions.cs @@ -38,6 +38,10 @@ public class ScreenshotOptions [Required] public bool BackgroundProcessingEnabled { get; set; } + public bool BackgroundProcessingWithTryCatch { get; set; } = false; + + public bool DoNotReusePlaywrightPage { get; set; } = false; + public Activity? Activity { get; set; } public string CalculateSleepBetweenUpdates() => @@ -52,9 +56,10 @@ public enum UrlType OpenHab } -public record Activity([Required] - TimeOnly ActiveFrom, - [Required] - TimeOnly ActiveTo, - [Range(1, uint.MaxValue)] - uint RefreshIntervalWhenInactiveInSeconds); \ No newline at end of file +public record Activity( + [Required] + TimeOnly ActiveFrom, + [Required] + TimeOnly ActiveTo, + [Range(1, uint.MaxValue)] + uint RefreshIntervalWhenInactiveInSeconds); \ No newline at end of file diff --git a/tests/Tests/Unit/Api/BackgroundScreenshotCreatorTests.cs b/tests/Tests/Unit/Api/BackgroundScreenshotCreatorTests.cs index 4fda73b..5d8ef4a 100644 --- a/tests/Tests/Unit/Api/BackgroundScreenshotCreatorTests.cs +++ b/tests/Tests/Unit/Api/BackgroundScreenshotCreatorTests.cs @@ -84,14 +84,16 @@ public async Task ProcessInBackground_ShouldDoNothing_IfNotActive() public async Task ProcessInBackground_ShouldLogExceptionButContinueWorking_WhenExceptionOccurs() { // Arrange - var screenshotOptions = new ScreenshotOptions { RefreshIntervalInSeconds = 1, BackgroundProcessingEnabled = true, Width = 800, Height = 600 }; + var screenshotOptions = new ScreenshotOptions + { + RefreshIntervalInSeconds = 1, BackgroundProcessingEnabled = true, BackgroundProcessingWithTryCatch = true, Width = 800, Height = 600 + }; var cancellationTokenSource = new CancellationTokenSource(); var screenshotCreatorMock = Substitute.For(); var invalidOperationException = new InvalidOperationException("Something went wrong"); var counter = 0; // not relevant, just here to make xUnit in CallBack.First happy screenshotCreatorMock.When(creator => creator.CreateScreenshotAsync(800, 600)) .Do(Callback.First(_ => counter++).ThenKeepThrowing(invalidOperationException)); - // screenshotCreatorMock.CreateScreenshotAsync(Arg.Any(), Arg.Any()).ThrowsAsync(invalidOperationException); var loggerMock = Substitute.For>(); var testee = new BackgroundScreenshotCreator(screenshotCreatorMock, Options.Create(screenshotOptions),