< Summary

Information
Class: Elsa.AI.Persistence.EFCore.Services.EFCoreAIConversationCleanupService
Assembly: Elsa.AI.Persistence.EFCore
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.AI.Persistence.EFCore/Services/EFCoreAIConversationCleanupService.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 62
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.cctor()100%210%
ExecuteAsync()0%620%
LogCleanupFailure(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.AI.Persistence.EFCore/Services/EFCoreAIConversationCleanupService.cs

#LineLine coverage
 1using System.Data.Common;
 2using Elsa.AI.Persistence.EFCore.Helpers;
 3using Microsoft.EntityFrameworkCore;
 4using Microsoft.Extensions.DependencyInjection;
 5using Microsoft.Extensions.Hosting;
 6using Microsoft.Extensions.Logging;
 7
 8namespace Elsa.AI.Persistence.EFCore.Services;
 9
 010public class EFCoreAIConversationCleanupService(
 011    IServiceScopeFactory scopeFactory,
 012    ILogger<EFCoreAIConversationCleanupService> logger) : BackgroundService
 13{
 014    private static readonly TimeSpan CleanupInterval = TimeSpan.FromHours(1);
 15
 16    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 17    {
 018        while (!stoppingToken.IsCancellationRequested)
 19        {
 20            try
 21            {
 022                await using var scope = scopeFactory.CreateAsyncScope();
 023                var dbContext = scope.ServiceProvider.GetRequiredService<AIDbContext>();
 024                await EFCoreAIConversationCleanup.DeleteExpiredAsync(dbContext, DateTimeOffset.UtcNow, stoppingToken);
 025            }
 026            catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
 27            {
 028                return;
 29            }
 030            catch (InvalidOperationException e)
 31            {
 032                LogCleanupFailure(e);
 033            }
 034            catch (DbUpdateException e)
 35            {
 036                LogCleanupFailure(e);
 037            }
 038            catch (DbException e)
 39            {
 040                LogCleanupFailure(e);
 041            }
 042            catch (Exception e) when (ExceptionFilters.IsNonFatal(e))
 43            {
 044                LogCleanupFailure(e);
 045            }
 46
 47            try
 48            {
 049                await Task.Delay(CleanupInterval, stoppingToken);
 050            }
 051            catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested)
 52            {
 053                return;
 54            }
 55        }
 056    }
 57
 58    private void LogCleanupFailure(Exception e)
 59    {
 060        logger.LogWarning(e, "Failed to clean up expired AI conversations.");
 061    }
 62}