feat(core): dynamic settings service, IFinlyticLogger, log broadcaster, and persistent Yahoo auth
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using FinlyticCore.Models.Settings;
|
||||
using FinlyticCore.Services;
|
||||
using Microsoft.Playwright;
|
||||
|
||||
namespace FinlyticCore.Services.PlaywrightScrapper;
|
||||
|
||||
public interface IPlaywrightBrowserFactory : IAsyncDisposable
|
||||
public interface IPlaywrightBrowserFactory : IAsyncDisposable, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Stellt sicher, dass die IBrowser-Instanz verbunden ist.
|
||||
@@ -18,15 +23,15 @@ public interface IPlaywrightBrowserFactory : IAsyncDisposable
|
||||
|
||||
public class PlaywrightBrowserFactory : IPlaywrightBrowserFactory
|
||||
{
|
||||
private readonly ILogger<PlaywrightBrowserFactory> _logger;
|
||||
private readonly IFinlyticLogger<PlaywrightBrowserFactory> _finlyticLogger;
|
||||
private readonly SemaphoreSlim _browserLock = new(1, 1);
|
||||
|
||||
private IPlaywright? _playwright;
|
||||
private IBrowser? _browser;
|
||||
|
||||
public PlaywrightBrowserFactory(ILogger<PlaywrightBrowserFactory> logger)
|
||||
public PlaywrightBrowserFactory(IFinlyticLogger<PlaywrightBrowserFactory> finlyticLogger)
|
||||
{
|
||||
_logger = logger;
|
||||
_finlyticLogger = finlyticLogger;
|
||||
}
|
||||
|
||||
public async Task<IBrowser> GetBrowserAsync(CancellationToken cancellationToken = default)
|
||||
@@ -51,7 +56,7 @@ public class PlaywrightBrowserFactory : IPlaywrightBrowserFactory
|
||||
}
|
||||
});
|
||||
|
||||
_logger.LogInformation("[PlaywrightFactory] Shared Chromium Instance successfully launched.");
|
||||
await _finlyticLogger.LogInfoAsync(CoreSettingKeys.PlaywrightChannel, "[PlaywrightFactory] Shared Chromium Instance successfully launched.");
|
||||
return _browser;
|
||||
}
|
||||
finally
|
||||
@@ -79,12 +84,41 @@ public class PlaywrightBrowserFactory : IPlaywrightBrowserFactory
|
||||
}
|
||||
};
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_browser != null)
|
||||
{
|
||||
_browser.CloseAsync().GetAwaiter().GetResult();
|
||||
_browser.DisposeAsync().GetAwaiter().GetResult();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore any sync disposal timeouts
|
||||
}
|
||||
finally
|
||||
{
|
||||
_playwright?.Dispose();
|
||||
_browserLock.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
if (_browser != null)
|
||||
{
|
||||
await _browser.CloseAsync();
|
||||
await _browser.DisposeAsync();
|
||||
try
|
||||
{
|
||||
await _browser.CloseAsync();
|
||||
await _browser.DisposeAsync();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore disposal errors
|
||||
}
|
||||
}
|
||||
|
||||
_playwright?.Dispose();
|
||||
|
||||
Reference in New Issue
Block a user