< Summary - Syki

Information
Class: Syki.Back.Database.Interceptors.CommandsDelayInterceptor
Assembly: Back
File(s): /home/runner/work/syki/syki/Back/Database/Interceptors/CommandsDelayInterceptor.cs
Tag: 4_16869239191
Line coverage
47%
Covered lines: 9
Uncovered lines: 10
Coverable lines: 19
Total lines: 48
Line coverage: 47.3%
Branch coverage
50%
Covered branches: 3
Total branches: 6
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
SavingChangesAsync()100%11100%
SavedChangesAsync()25%13418.18%

File(s)

/home/runner/work/syki/syki/Back/Database/Interceptors/CommandsDelayInterceptor.cs

#LineLine coverage
 1using Quartz;
 2using Microsoft.EntityFrameworkCore.Diagnostics;
 3using Microsoft.EntityFrameworkCore.Infrastructure;
 4
 5namespace Syki.Back.Database.Interceptors;
 6
 7public sealed class CommandsDelayInterceptor : SaveChangesInterceptor
 8{
 137769    private List<DateTime> _delays = [];
 10
 11  public override async ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, Interception
 12  {
 1532013        _delays = eventData.Context.ChangeTracker
 1532014            .Entries<Command>()
 537615            .Where(entry => entry.State == EntityState.Added && entry.Entity.NotBefore != null)
 016            .Select(x => x.Entity.NotBefore!.Value)
 1532017            .ToList();
 18
 3064019        return await Task.Run(() => result);
 1532020  }
 21
 22    public override async ValueTask<int> SavedChangesAsync(SaveChangesCompletedEventData eventData, int result, Cancella
 23    {
 3064024        if (_delays.Count == 0) return result;
 25
 026        var schedulerFactory = eventData.Context.GetService<ISchedulerFactory>();
 027        var scheduler = await schedulerFactory.GetScheduler(cancellationToken);
 28
 029        foreach (var delay in _delays)
 30        {
 031            var job = JobBuilder.Create<NotifyNewCommandJob>().Build();
 032            var trigger = Quartz.TriggerBuilder.Create()
 033                .StartAt(delay)
 034                .Build();
 035            await scheduler.ScheduleJob(job, trigger, cancellationToken);
 36        }
 37
 038        return result;
 1532039    }
 40}
 41
 42public class NotifyNewCommandJob(SykiDbContext ctx) : IJob
 43{
 44    public async Task Execute(IJobExecutionContext context)
 45    {
 46        await ctx.Database.ExecuteSqlRawAsync($"NOTIFY new_command", context.CancellationToken);
 47    }
 48}