CUB-33: Integrate Moonraker filament usage polling with UsageLog persistence
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m22s
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m22s
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Extrudex.Domain.Interfaces;
|
||||
using Extrudex.Infrastructure.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
@@ -15,25 +16,30 @@ namespace Extrudex.API.Jobs;
|
||||
/// Configuration is bound from the "FilamentUsageSync" section in
|
||||
/// appsettings.json. Set Enabled=false to disable without removing
|
||||
/// the service registration.
|
||||
///
|
||||
/// Uses an IServiceScopeFactory to resolve scoped dependencies
|
||||
/// (IFilamentUsageSyncService, IUsageLogService) on each sync cycle,
|
||||
/// avoiding captive-dependency issues from injecting scoped services
|
||||
/// into the singleton BackgroundService lifetime.
|
||||
/// </summary>
|
||||
public class FilamentUsageSyncJob : BackgroundService
|
||||
{
|
||||
private readonly IFilamentUsageSyncService _syncService;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly FilamentUsageSyncOptions _options;
|
||||
private readonly ILogger<FilamentUsageSyncJob> _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new FilamentUsageSyncJob.
|
||||
/// </summary>
|
||||
/// <param name="syncService">The service that performs the actual sync logic.</param>
|
||||
/// <param name="scopeFactory">Factory for creating DI scopes to resolve scoped services.</param>
|
||||
/// <param name="options">Configuration options for polling interval and timeouts.</param>
|
||||
/// <param name="logger">Logger for diagnostic output.</param>
|
||||
public FilamentUsageSyncJob(
|
||||
IFilamentUsageSyncService syncService,
|
||||
IServiceScopeFactory scopeFactory,
|
||||
IOptions<FilamentUsageSyncOptions> options,
|
||||
ILogger<FilamentUsageSyncJob> logger)
|
||||
{
|
||||
_syncService = syncService;
|
||||
_scopeFactory = scopeFactory;
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
@@ -57,8 +63,10 @@ public class FilamentUsageSyncJob : BackgroundService
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
var syncedCount = await _syncService.SyncAllAsync(stoppingToken);
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var syncService = scope.ServiceProvider.GetRequiredService<IFilamentUsageSyncService>();
|
||||
var syncedCount = await syncService.SyncAllAsync(stoppingToken);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Filament usage sync completed — {SyncedCount} printer(s) synced. Next sync in {Interval}",
|
||||
|
||||
Reference in New Issue
Block a user