CUB-33: Integrate Moonraker filament usage polling with UsageLog persistence
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m22s

This commit is contained in:
Dex
2026-04-29 13:13:12 -04:00
parent ddae95767f
commit 6e0ca7f425
4 changed files with 255 additions and 43 deletions

View File

@@ -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}",

View File

@@ -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;
@@ -19,22 +20,22 @@ namespace Extrudex.API.Jobs;
/// </summary>
public class MoonrakerPrinterSyncJob : BackgroundService
{
private readonly IMoonrakerPrinterSyncService _syncService;
private readonly IServiceScopeFactory _scopeFactory;
private readonly MoonrakerPrinterSyncOptions _options;
private readonly ILogger<MoonrakerPrinterSyncJob> _logger;
/// <summary>
/// Creates a new MoonrakerPrinterSyncJob.
/// </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 MoonrakerPrinterSyncJob(
IMoonrakerPrinterSyncService syncService,
IServiceScopeFactory scopeFactory,
IOptions<MoonrakerPrinterSyncOptions> options,
ILogger<MoonrakerPrinterSyncJob> logger)
{
_syncService = syncService;
_scopeFactory = scopeFactory;
_options = options.Value;
_logger = logger;
}
@@ -59,7 +60,9 @@ public class MoonrakerPrinterSyncJob : BackgroundService
{
try
{
var syncedCount = await _syncService.SyncAllAsync(stoppingToken);
using var scope = _scopeFactory.CreateScope();
var syncService = scope.ServiceProvider.GetRequiredService<IMoonrakerPrinterSyncService>();
var syncedCount = await syncService.SyncAllAsync(stoppingToken);
_logger.LogInformation(
"Moonraker printer sync completed — {SyncedCount} printer(s) synced. Next sync in {Interval}",