Skip to content

Commit

Permalink
feat: add options to configure page reuse and try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
mu88 committed Jul 8, 2024
1 parent 32a4d63 commit df65611
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/ScreenshotCreator.Api/BackgroundScreenshotCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ScreenshotCreator.Logic/PlaywrightHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async ValueTask DisposeAsync()
/// <inheritdoc />
public async ValueTask<IPage> InitializePlaywrightAsync()
{
if (_page != null)
if (!_screenshotOptions.DoNotReusePlaywrightPage && _page != null)
{
logger.ReusingPlaywrightPage();
return _page;
Expand Down
17 changes: 11 additions & 6 deletions src/ScreenshotCreator.Logic/ScreenshotOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() =>
Expand All @@ -52,9 +56,10 @@ public enum UrlType
OpenHab
}

public record Activity([Required]
TimeOnly ActiveFrom,
[Required]
TimeOnly ActiveTo,
[Range(1, uint.MaxValue)]
uint RefreshIntervalWhenInactiveInSeconds);
public record Activity(
[Required]
TimeOnly ActiveFrom,
[Required]
TimeOnly ActiveTo,
[Range(1, uint.MaxValue)]
uint RefreshIntervalWhenInactiveInSeconds);
6 changes: 4 additions & 2 deletions tests/Tests/Unit/Api/BackgroundScreenshotCreatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IScreenshotCreator>();
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<uint>(), Arg.Any<uint>()).ThrowsAsync(invalidOperationException);
var loggerMock = Substitute.For<ILogger<BackgroundScreenshotCreator>>();
var testee = new BackgroundScreenshotCreator(screenshotCreatorMock,
Options.Create(screenshotOptions),
Expand Down

0 comments on commit df65611

Please sign in to comment.